-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
169 lines (141 loc) · 5.12 KB
/
player.lua
File metadata and controls
169 lines (141 loc) · 5.12 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
local s = require('setup')
local Player = {}
function Player.new(map)
local self = {}
-- Variables -----------------------------------------------------
local map = map
local pos = {}
pos.lx = s.player.x
pos.ty = s.player.y
local w = s.player.w
local h = s.player.h
local speed = s.player.speed
local gravity = s.gravity
local color = s.colors.player
local yVelocity = 0
local jumpHeight = s.player.jumpHeight
local score = 0
-- WARNING! Hard coded.
local targets = {
[1] = false,
[2] = false,
}
-- Utilities -----------------------------------------------------
-- Block tile = Cannot walk through.
function self:isBlockTile(row, col)
if row < 1 or row > s.tiles.numRows then return false end
if col < 1 or col > s.tiles.numCols then return false end
return map[row][col] == '@' or false
end
-- Directions: top, bottom, left, right
function self:isBlocked(direction)
if direction == 'top' or direction == 'bottom' then
for col = pos.lCol, pos.rCol, 1 do
local row
if direction == 'top' then row = pos.tRow else row = pos.bRow end
if self:isBlockTile(row, col) then
return true
end
end
elseif direction == 'left' or direction == 'right' then
for row = pos.tRow, pos.bRow, 1 do
local col
if direction == 'left' then col = pos.lCol else col = pos.rCol end
if self:isBlockTile(row, col) then
return true
end
end
end
return false
end
-- Update --------------------------------------------------------
function self:update(dt)
score = score + 1
if love.keyboard.isDown('w') and yVelocity == 0 then
yVelocity = jumpHeight
jumpSound:play()
end
if yVelocity ~= 0 then
pos.ty = pos.ty - (yVelocity * dt)
end
yVelocity = yVelocity - (gravity * dt)
local distance = speed * dt
self:updatePosVariables()
-- Pull back if blocked.
if self:isBlocked('top') then
pos.ty = pos.tRow * s.tiles.size
yVelocity = 0
elseif self:isBlocked('bottom') then
pos.ty = ((pos.bRow - 1) * s.tiles.size) - 1 - h
yVelocity = 0
end
if love.keyboard.isDown('a') then pos.lx = pos.lx - distance end
if love.keyboard.isDown('d') then pos.lx = pos.lx + distance end
self:updatePosVariables()
-- Pull back if blocked.
if self:isBlocked('left') then
pos.lx = pos.lCol * s.tiles.size
elseif self:isBlocked('right') then
pos.lx = ((pos.rCol - 1) * s.tiles.size) - 1 - w
end
-- Wrap player from bottom to top.
if pos.ty > s.window.h then
pos.ty = -h
end
-- Not strictly needed, for drawing infos.
self:updatePosVariables()
-- WARNING! Hard coded.
if pos.bRow == 10 and pos.rCol == 3 then targets[1] = true end
if pos.bRow == 20 and pos.rCol == 22 then targets[2] = true end
if love.keyboard.isDown('r') then
targets[1] = false
targets[2] = false
end
end
function self:updatePosVariables()
pos.rx = pos.lx + w
pos.by = pos.ty + h
pos.tRow = math.floor(pos.ty / s.tiles.size) + 1
pos.bRow = math.floor(pos.by / s.tiles.size) + 1
pos.lCol = math.floor(pos.lx / s.tiles.size) + 1
pos.rCol = math.floor(pos.rx / s.tiles.size) + 1
end
-- Draw ----------------------------------------------------------
function self:draw()
if s.debug then self:drawInfos() end
self:drawPlayer()
self:drawDotGotIndicator()
love.graphics.print(score, 10, 10)
end
function self:drawInfos()
love.graphics.setColor(s.colors.debug)
love.graphics.print('DEBUG', 20, 46)
love.graphics.print('x: ' .. math.floor(pos.lx), 20, 100)
love.graphics.print('y: ' .. math.floor(pos.ty), 20, 125)
love.graphics.print('top row: ' .. pos.tRow, 20, 150)
love.graphics.print('bot row: ' .. pos.bRow, 20, 175)
love.graphics.print('lft col: ' .. pos.lCol, 20, 200)
love.graphics.print('rgt col: ' .. pos.rCol, 20, 225)
local isJumping
if yVelocity > 0 then isJumping = 'ya' else isJumping = 'no' end
love.graphics.print('Jumping?: ' .. isJumping, 20, 250)
end
function self:drawPlayer()
love.graphics.setColor(color)
local x = math.floor(pos.lx)
local y = math.floor(pos.ty)
love.graphics.rectangle('fill', x, y, w, h)
end
-- WARNING! Hard coded.
function self:drawDotGotIndicator()
love.graphics.setColor(1, 1, 1)
if targets[1] then
love.graphics.circle('fill', 280, 354, 8)
end
if targets[2] then
love.graphics.circle('fill', 888, 674, 8)
end
end
return self
end
return Player