-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobile.lua
62 lines (55 loc) · 1.13 KB
/
mobile.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Mobile = class('Mobile')
function Mobile:initialize(grid,speed,x,y)
self.grid = grid
self.speed = speed
self.sx = 0
self.sy = 0
self:setGridCoord(x,y)
end
function Mobile:setGridCoord(x,y)
self.x = x
self.y = y
local scrX,scrY = self.grid:getScreenCoord(x,y)
self.act_x = scrX
self.act_y = scrY
self.grid_x = scrX
self.grid_y = scrY
end
function Mobile:update(dt)
local deltaX = self.grid_x - self.act_x
local deltaY = self.grid_y - self.act_y
if math.abs(deltaX) < 2 then
self.sx=0
end
if math.abs(deltaY) < 2 then
self.sy=0
end
if self.sx == 0 then
self.act_x = self.grid_x
deltaX=0
end
if self.sy == 0 then
self.act_y = self.grid_y
deltaY=0
end
if self.sx ~=0 or self.sy ~= 0 then
local u = self.speed*dt
local dx = self.sx*u
local dy = self.sy*u
if math.abs(dx)>math.abs(deltaX) then
dx = deltaX
end
if math.abs(dy)>math.abs(deltaY) then
dy = deltaY
end
self.act_y = self.act_y + dy
self.act_x = self.act_x + dx
end
end
function Mobile:atdest()
if math.abs(self.act_x - self.grid_x) < 3 and math.abs(self.act_y - self.grid_y) < 3 then
return true
else
return false
end
end