-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
119 lines (99 loc) · 2.92 KB
/
player.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
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
local Mob = require("mob")
local Player = Mob:extend()
--- Instance a new Player
-- @param sp SpriteSheet object
-- @param map_object Object from STI
function Player:new(sp, map_object)
Player.super.new(self, sp, map_object)
self.name = "player"
self.speed = 0.2
self._teleporting = nil
self.interact = false
end
--- Update
-- @param dt DeltaTime
-- @param world World object
function Player:update(dt, world)
Player.super.update(self, dt, world)
self:_manage_input()
if self.interact then
self.interact = false
local items, len
if self.facing == "west" then
items, len = world:queryPoint(self.bb.x - 1, self.bb.y + 1)
elseif self.facing == "east" then
items, len = world:queryPoint(self.bb.x + 17, self.bb.y + 1)
elseif self.facing == "north" then
items, len = world:queryPoint(self.bb.x + 1, self.bb.y - 1)
elseif self.facing == "south" then
items, len = world:queryPoint(self.bb.x + 1, self.bb.y + 17)
end
if len > 0 then
for _, object in pairs(items) do
if object.entity ~= nil then
-- print(object.type, len)
if object.type == "door" then
object.entity:toggle()
elseif object.type == "chest" then
object.entity:toggle()
print("openning chest")
end
end
end
end
end
end
--- Manage input
function Player:_manage_input()
local _, current_move_value = self:_get_move_max()
if input:pressed("left") then
self.move.left = current_move_value + 1
end
if input:pressed("right") then
self.move.right = current_move_value + 1
end
if input:pressed("up") then
self.move.up = current_move_value + 1
end
if input:pressed("down") then
self.move.down = current_move_value + 1
end
if input:released("left") then
self.move.left = 0
end
if input:released("right") then
self.move.right = 0
end
if input:released("up") then
self.move.up = 0
end
if input:released("down") then
self.move.down = 0
end
if input:released("interact") then
self.interact = true
end
end
--- Collide event
-- @param cols Things involved in the event
function Player:collide(cols)
if cols[1].other.type == "enemy" then
print("Ouch! It hurts!")
elseif cols[1].other.type == "trap" then
cols[1].other.entity:trigger()
end
end
--- Moved event
function Player:moved()
if self.stand_on and self.stand_on[1].type == "teleporter" then
self._teleporting = self.stand_on[1].properties.destination
end
end
function Player:get_teleporting()
local tel = self._teleporting
if tel then
self._teleporting = nil
end
return tel
end
return Player