Thank you everyone, but im kind of afraid to update the app though as it may cause bugs, no?
Anyway, I got this done today, tell me what you think and what I should improve ^^ (I did find a couple of bugs myself but I don’t know if it’s worth fixing them haha)
--# Main
-- Isn2.4
Case = class()
function Case:init(i,j,etat)
-- you can accept and set parameters here
self.i = i
self.j = j
self.etat = etat
couleurs={color(255),color(255,0,0,255),color(9, 255, 0, 255),color(238, 0, 255, 255)}
end
function Case:draw()
strokeWidth(6)
stroke(couleurs[self.etat])
fill(0)
rect(cotedam*self.i,cotedam*self.j,cotedam,cotedam)
end
function Case:touched(touch)
-- Codea does not automatically call this method
end
Colon = class()
function Colon:init(i,j,id)
-- you can accept and set parameters here
self.i = i
self.j=j
self.x=cote*self.i
self.y=cote*self.j
self.id=id
self.i0=self.i
self.j0=self.j
end
function Colon:draw()
spriteMode(CORNER)
sprite("Planet Cute:Character Princess Girl",self.x+3,self.y+25,100,100)
end
function Colon:touched(touch)
if vec2(self.x,self.y):dist(vec2(touch.x,touch.y)) < 50 then
self.x=touch.x
self.y=touch.y
end
if touch.state==BEGAN or touch.state==MOVING then
if (touch.x>cotedam*self.i and touch.x<cotedam*(self.i+1)) and (touch.y>cotedam*self.j and touch.y<cotedam*(self.j+1)) then
self.x=touch.x-cote/2
self.y=touch.y-cote/2
end
elseif touch.state==ENDED then
if (touch.x>cotedam*(self.i-1) and touch.x<cotedam*(self.i+2)) and (touch.y>cotedam*(self.j-1) and touch.y<cotedam*(self.j+2)) then
self.i=math.ceil(touch.x/cotedam)-1
self.j=math.ceil(touch.y/cotedam)-1
if cases[6*(self.i)+(self.j+1)].etat==1 then
cases[6*(self.i)+(self.j+1)].etat=4
if self.i0~=self.i or self.j0~=self.j then
cases[6*(self.i0)+(self.j0+1)].etat=1
self.i0=self.i
self.j0=self.j
end
else
self.i=self.i0
self.j=self.j0
end
end
-- limite du damier
if self.i<0 then
self.i=0
end
if self.j<0 then
self.j=0
end
if self.i>nb_l then
self.i=nb_l
end
if self.j>nb_h then
self.j=nb_h
end
self.x=cotedam*self.i
self.y=cotedam*self.j
-- Codea does not automatically call this method
end
end
Exploreur = class()
function Exploreur:init(i,j,id)
-- you can accept and set parameters here
self.i = i
self.j=j
self.x=cote*self.i
self.y=cote*self.j
self.id=id
self.i0=self.i
self.j0=self.j
end
function Exploreur:draw()
spriteMode(CORNER)
sprite("Planet Cute:Character Boy",self.x+3,self.y+25,100,100)
end
-- Codea does not automatically call this method
function Exploreur:cdde(p)
q=math.random()
if q<p then
self.ph=self.ph
else
self.ph=math.random(1,2)
end
exploreur = class()
end
function Exploreur:touched(touch)
if vec2(self.x,self.y):dist(vec2(touch.x,touch.y)) < 50 then
self.x=touch.x
self.y=touch.y
end
if touch.state==BEGAN or touch.state==MOVING then
if (touch.x>cotedam*self.i and touch.x<cotedam*(self.i+1)) and (touch.y>cotedam*self.j and touch.y<cotedam*(self.j+1)) then
self.x=touch.x-cote/2
self.y=touch.y-cote/2
end
elseif touch.state==ENDED then
if (touch.x>cotedam*(self.i-1) and touch.x<cotedam*(self.i+2)) and (touch.y>cotedam*(self.j-1) and touch.y<cotedam*(self.j+2)) then
self.i=math.ceil(touch.x/cotedam)-1
self.j=math.ceil(touch.y/cotedam)-1
if cases[6*(self.i)+(self.j+1)].etat==1 then
cases[6*(self.i)+(self.j+1)].etat=3
if self.i0~=self.i or self.j0~=self.j then
cases[6*(self.i0) + (self.j0+1)].etat=1
self.i0=self.i
self.j0=self.j
end
else
self.i=self.i0
self.j=self.j0
end
end
-- limite du damier
if self.i<0 then
self.i=0
end
if self.j<0 then
self.j=0
end
if self.i>nb_l then
self.i=nb_l
end
if self.j>nb_h then
self.j=nb_h
end
self.x=cotedam*self.i
self.y=cotedam*self.j
-- Codea does not automatically call this method
end
end
Guerrier = class()
function Guerrier:init(i,j,id)
-- you can accept and set parameters here
self.i = i
self.j=j
self.x=cotedam*self.i
self.y=cotedam*self.j
self.id=id
self.i0=self.i
self.j0=self.j
end
function Guerrier:draw()
spriteMode(CORNER)
sprite("Planet Cute:Character Horn Girl",self.x+3,self.y+3,100,100)
end
function Guerrier:touched(touch)
if touch.state==BEGAN or touch.state==MOVING then
if (touch.x>cotedam*self.i and touch.x<cotedam*(self.i+1)) and (touch.y>cotedam*self.j and touch.y<cotedam*(self.j+1)) then
self.x=touch.x-cote/2
self.y=touch.y-cote/2
end
elseif touch.state==ENDED then
if (touch.x>cotedam*(self.i-1) and touch.x<cotedam*(self.i+2)) and (touch.y>cotedam*(self.j-1) and touch.y<cotedam*(self.j+2)) then
self.i=math.ceil(touch.x/cotedam)-1
self.j=math.ceil(touch.y/cotedam)-1
if cases[6*(self.i)+(self.j+1)].etat==1 then
cases[6*(self.i)+(self.j+1)].etat=2
if self.i0~=self.i or self.j0~=self.j then
cases[6*(self.i0) + (self.j0+1)].etat=1
self.i0=self.i
self.j0=self.j
end
else
self.i=self.i0
self.j=self.j0
end
end
-- limite du damier
if self.i<0 then
self.i=0
end
if self.j<0 then
self.j=0
end
if self.i>nb_l then
self.i=nb_l
end
if self.j>nb_h then
self.j=nb_h
end
self.x=cotedam*self.i
self.y=cotedam*self.j
-- Codea does not automatically call this method
end
end
-- Civ Isn
-- Use this function to perform your initial setup
function setup()
llc=100
cote=50
cotedam=110
nb_l=math.ceil(WIDTH/cotedam)-2
nb_h=math.ceil(HEIGHT/cotedam)-2
exploreurs={}
table.insert(exploreurs, Exploreur(1,1))
guerriers={}
table.insert(guerriers, Guerrier(5,5))
colons={}
table.insert(colons, Colon(1,5))
cases={}
for i=0,nb_l do
for j=0,nb_h do
table.insert(cases,Case(i,j,1))
end
end
end
-- This function gets called once every frame
function draw()
background(1, 1, 1, 255)
for i,objet in ipairs(cases) do
objet:draw()
end
-- Do your drawing here
for i,objet in ipairs(guerriers) do
objet:touched(CurrentTouch)
objet:draw()
end
for i,objet in ipairs(exploreurs) do
objet:touched(CurrentTouch)
objet:draw()
end
for i,objet in ipairs(colons) do
objet:touched(CurrentTouch)
-- print(CurrentTouch)
--if CurrentTouch.state==ENDED then
-- objet.x=objet.dpx
--objet.y=objet.dpy
-- end
objet:draw()
end
end