-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmob.lua
More file actions
242 lines (207 loc) · 6.57 KB
/
mob.lua
File metadata and controls
242 lines (207 loc) · 6.57 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
local Entity = require("entity")
local Mob = Entity:extend()
--- Instance a new Mob
-- @param sp SpriteSheet object
-- @param map_object Object from STI
function Mob:new(sp, map_object)
Mob.super.new(self, sp, map_object)
self.name = "mob"
self.speed = 1
self.move = { left = 0, right = 0, up = 0, down = 0 }
self.moving = "standing" -- standing, left, right, up, down
self.moving_px = 0
self.move_wait = 0
self.move_wait_total = 0.083 -- secs
self.moved_event_queue = false -- hacky thing to help detecting Mob:moved() event
self.facing = "south"
self.stand_on = nil -- object
self.an:set_states({
default = { { 2, 1 } },
walk_left = {
{ 1, 2 },
{ 3, 2 },
},
walk_right = {
{ 1, 3 },
{ 3, 3 },
},
walk_up = {
{ 1, 4 },
{ 3, 4 },
},
walk_down = {
{ 1, 1 },
{ 3, 1 },
},
stand_left = {
{ 2, 2 },
},
stand_right = {
{ 2, 3 },
},
stand_up = {
{ 2, 4 },
},
stand_down = {
{ 2, 1 },
},
rotating = {
{ 2, 1 },
{ 2, 3 },
{ 2, 4 },
{ 2, 2 },
},
})
end
--- Update
-- @param dt DeltaTime
-- @param world World object
function Mob:update(dt, world)
Mob.super.update(self, dt)
if self.moving == "standing" then
self.stand_on = nil
local items, len = world:queryPoint(self.bb.x + 1, self.bb.y + 1)
for _, value in pairs(items) do
if value ~= self.map_object then
if self.stand_on == nil then
self.stand_on = {}
end
table.insert(self.stand_on, value)
end
end
if self.moved_event_queue then
self.moved_event_queue = false
self:moved()
end
else
self.moved_event_queue = true
end
local move_key, _ = self:_get_move_max()
if self.moving == "standing" and move_key ~= "nothing" then
if self.move_wait < self.move_wait_total then
if self.move_wait == 0 then
if move_key == "left" and self.facing == "west" then
self.move_wait = self.move_wait_total
elseif move_key == "right" and self.facing == "east"then
self.move_wait = self.move_wait_total
elseif move_key == "up" and self.facing == "north" then
self.move_wait = self.move_wait_total
elseif move_key == "down" and self.facing == "south" then
self.move_wait = self.move_wait_total
end
end
if move_key == "left" then
self.facing = "west"
elseif move_key == "right" then
self.facing = "east"
elseif move_key == "up" then
self.facing = "north"
elseif move_key == "down" then
self.facing = "south"
end
self.move_wait = self.move_wait + dt
else
self.move_wait = 0
if move_key == "left" then
self.an:set_state_cur("walk_left")
self.moving = "left"
elseif move_key == "right" then
self.an:set_state_cur("walk_right")
self.moving = "right"
elseif move_key == "up" then
self.an:set_state_cur("walk_up")
self.moving = "up"
elseif move_key == "down" then
self.an:set_state_cur("walk_down")
self.moving = "down"
end
end
elseif move_key == "nothing" then
self.move_wait = 0
end
if moving ~= "standing" then
move_px = (TILE_SIZE_O / self.speed) * dt
if self.moving_px + move_px > TILE_SIZE_O then
move_px = TILE_SIZE_O - self.moving_px
end
end
local cols, len
if self.moving == "left" then
self.bb.x, self.bb.y, cols, len = world:move(self.map_object,
self.bb.x - move_px, self.bb.y, self.col_filter)
elseif self.moving == "right" then
self.bb.x, self.bb.y, cols, len = world:move(self.map_object,
self.bb.x + move_px, self.bb.y, self.col_filter)
elseif self.moving == "up" then
self.bb.x, self.bb.y, cols, len = world:move(self.map_object, self.bb.x,
self.bb.y - move_px, self.col_filter)
elseif self.moving == "down" then
self.bb.x, self.bb.y, cols, len = world:move(self.map_object, self.bb.x,
self.bb.y + move_px, self.col_filter)
end
if len then
if len > 0 then
self:collide(cols)
end
end
if self.moving ~= "standing" then
self.moving_px = self.moving_px + move_px
if self.moving_px >= TILE_SIZE_O then
self.moving_px = 0
if move_key ~= self.moving then
self.moving = "standing"
end
end
world:update(self.map_object, self.bb.x, self.bb.y)
end
if self.moving == "standing" then
if self.facing == "west" then
self.an:set_state_cur("stand_left")
elseif self.facing == "east" then
self.an:set_state_cur("stand_right")
elseif self.facing == "north" then
self.an:set_state_cur("stand_up")
elseif self.facing == "south" then
self.an:set_state_cur("stand_down")
end
end
end
--- Collision filter
-- @param item
-- @param other Things that got collided with self
-- @return "slide" or nil Collide or not
function Mob.col_filter(item, other)
if other.type == "teleporter" then
return nil
elseif other.type == "door" and other.properties.open then
return nil
else
return "slide"
end
end
--- Collide event
-- @param cols Things involved in the event
function Mob:collide(cols)
-- Doing nothing, extend me in subclasses
end
--- Moved event
function Mob:moved()
end
--- Get the most relevant movement corresponding to pressed the key(s) pressed
--- this is to keep some history on the order the keys have been pressed
-- @return move_key, move_value Movement
function Mob:_get_move_max()
local move_key = 0
local move_value = -math.huge
for key, value in pairs(self.move) do
if move_value < value then
move_key = key
move_value = value
end
end
if move_value == 0 then
move_key = "nothing"
end
return move_key, move_value
end
return Mob