-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
116 lines (102 loc) · 2.9 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
require('nodes')
require('tts')
BLINDNESS_DELAY = 3 -- in seconds
player = {
x = 0,
y = 0,
dead = false,
blind = false,
checkpoint = {
x = 0,
y = 0
},
coinsCollected = 0
}
local function getMovement(key)
if key == "up" or key == "w" then
return {
x = player.x,
y = player.y - 1
}
elseif key == "down" or key == "s" then
return {
x = player.x,
y = player.y + 1
}
elseif key == "left" or key == "a" then
return {
x = player.x - 1,
y = player.y
}
elseif key == "right" or key == "d" then
return {
x = player.x + 1,
y = player.y
}
else
return false
end
end
function player:update(dt)
if (player.blind) then
if (player.blind < 0) then
player.blind = false
draw:fadeEnd(1/BLINDNESS_DELAY)
else
player.blind = player.blind - dt
end
end
end
-- The proper way to kill a player
function player:kill()
if (not player.dead) then
player.dead = true
tts:say("You are dead. Press C to restart at a checkpoint. Press R to restart at a random location.")
end
end
function player:ressurect()
if (player.dead) then
player.dead = false
tts:stop_saying("You are dead. Press C to restart at a checkpoint. Press R to restart at a random location.")
tts:say("you are alive")
end
end
player.handleKey = function(key)
local movement = getMovement(key)
if (movement and not player.blind) then
player.blind = BLINDNESS_DELAY
draw:fadeBegin(1/BLINDNESS_DELAY)
end
if movement and player.blind then
local dx = player.x
local dy = player.y
if (nodes:isWalkable(movement.x, movement.y)) then
player.x = movement.x
player.y = movement.y
if (nodes:isDeadly(player.x, player.y)) then
player:kill()
end
if (candles:contains(movement.x, movement.y)) then
player.checkpoint.x = movement.x
player.checkpoint.y = movement.y
tts:say("new checkpoint")
draw:fadeEnd(1/BLINDNESS_DELAY)
end
if (coins:contains(movement.x, movement.y)) then
player.coinsCollected = player.coinsCollected + 1
coins:remove(movement.x, movement.y)
tts:say("coin collected")
draw:fadeEnd(1/BLINDNESS_DELAY)
end
dx = player.x - dx
dy = player.y - dy
if (not nodes:isWalkable(player.x + dx, player.y + dy)) then
tts:say("wall")
elseif (nodes:isDeadly(player.x + dx, player.y + dy)) then
tts:say("Be careful!")
end
else
tts:say("wall")
end
end
end