-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
231 lines (184 loc) · 8.02 KB
/
Copy pathplayer.lua
File metadata and controls
231 lines (184 loc) · 8.02 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
_G.playerID = 0
function ReturnPlayer(items, position)
local player = {}
player.id = playerID
playerID = playerID + 1
local state = {}
local stats = {}
local controls = {}
local textures = {}
controls.movement = {}
controls.attack = {}
controls.movement.up = function () return love.keyboard.isDown("w") end
controls.movement.down = function () return love.keyboard.isDown("s") end
controls.movement.left = function () return love.keyboard.isDown("a") end
controls.movement.right = function () return love.keyboard.isDown("d") end
controls.movement.sprint = function () return love.keyboard.isDown("lshift") end
controls.attack.fire = function () return love.mouse.isDown(1) end
controls.attack.secondary = function () return love.mouse.isDown(2) end
textures.body = love.graphics.newImage("Assets/Sprites/Player/Player.png")
stats.acceleration = 800
stats.max_speed = 200
stats.sprint_boost = 100
stats.sprinttime = 15
stats.zoommult = 3
state.position = vector.new(0,0)
state.speed = vector.new(0,0)
state.scoped_in = false
state.health = 1
state.size = 20
state.dead = function () return player.state.health <= 0 end
state.isStill = function () return (player.state.speed == {0,0}) end
state.fireConditionsMet = function () return (controls.attack.fire() or love.keyboard.isDown("space")) end
state.secondaryConditonsMet = function () return (controls.attack.secondary() or love.keyboard.isDown("r")) end
state.movementConditionsMet = function() return true end
player.equips = {}
local gun_file = require("Weapons.Guns.G19")
player.equips.gun = ReturnGun()
player.textures = textures
player.controls = controls
player.stats = stats
player.state = state
player.Render = function ()
local x,y = PLAYER_SCREEN_POSITION:unpack()
love.graphics.setColor(0.1,0.8,0.1)
love.graphics.circle("fill", x,y, player.state.size*ZOOM_MULT)
love.graphics.setColor(1, 1, 1)
end
-- Not using rn but just in case i end up needing it for whatever reason
player.DesiredMovementVector = function()
local desired_movement = vector.new(0,0)
if player.controls.up then
if player.controls.left or player.controls.right then
desired_movement[1] = desired_movement[1] - SIN45
else
desired_movement[1] = desired_movement[1] - 1
end
end
if player.controls.down then
if player.controls.left or player.controls.right then
desired_movement[1] = desired_movement[1] + SIN45
else
desired_movement[1] = desired_movement[1] + 1
end
end
if player.controls.left then
if player.controls.up or player.controls.down then
desired_movement[2] = desired_movement[2] - SIN45
else
desired_movement[2] = desired_movement[2] - 1
end
end
if player.controls.right then
if player.controls.up or player.controls.down then
desired_movement[2] = desired_movement[2] + SIN45
else
desired_movement[2] = desired_movement[2] + 1
end
end
return desired_movement
end
player.Acceleration = function (dt)
local up,down,left,right,sprint = player.controls.movement.up(), player.controls.movement.down(), player.controls.movement.left(), player.controls.movement.right(), player.controls.movement.sprint()
local max_speed, acceleration, sprint_boost = player.stats.max_speed, player.stats.acceleration*dt, player.stats.sprint_boost
if sprint then
max_speed = max_speed + sprint_boost
end
if player.equips and player.equips.gun and player.equips.gun ~= nil then
if player.state.scoped_in then
max_speed = max_speed - player.equips.gun.aiming_walk_speed_penalty
end
end
if xor(up,down) and xor(left,right) then -- if (up xor down) and (left xor right) then cap the speed at the root of the original max speed, to prevent diagonal movement being faster than movement along one of the cardinals
max_speed = max_speed / ROOT2
elseif not xor(left,right) then
max_speed = 0
end
local new_speed_x, new_speed_y = player.state.speed:unpack()
-- Determine movement direction
if left and not right then
if new_speed_x > -max_speed then
new_speed_x = new_speed_x - acceleration
if new_speed_x < -max_speed then
new_speed_x = -max_speed
end
end
end
if right and not left then
if new_speed_x < max_speed then
new_speed_x = new_speed_x + acceleration
if new_speed_x > max_speed then
new_speed_x = max_speed
end
end
end
-- Limit left speed
if new_speed_x < -max_speed then
new_speed_x = new_speed_x + acceleration
if new_speed_x > -max_speed then -- ease the speed to the intended range if not in the intended range
new_speed_x = -max_speed
end
end
-- Limit right speed
if new_speed_x > max_speed then
new_speed_x = new_speed_x - acceleration
if new_speed_x < max_speed then -- ease the speed to the intended range if not in the intended range
new_speed_x = max_speed
end
end
max_speed, acceleration, sprint_boost = player.stats.max_speed, player.stats.acceleration*dt, player.stats.sprint_boost
if sprint then
max_speed = max_speed + sprint_boost
end
if player.equips and player.equips.gun and player.equips.gun ~= nil then
if player.state.scoped_in then
max_speed = max_speed - player.equips.gun.aiming_walk_speed_penalty
end
end
if xor(up,down) and xor(left,right) then -- if (up xor down) and (left xor right) then cap the speed at the root of the original max speed, to prevent diagonal movement being faster than movement along one of the cardinals
max_speed = max_speed / ROOT2
elseif not xor(up,down) then
max_speed = 0
end
-- Determine movement direction
if up and not down then
if new_speed_y > -max_speed then
new_speed_y = new_speed_y - acceleration
if new_speed_y < -max_speed then
new_speed_y = -max_speed
end
end
end
if down and not up then
if new_speed_y < max_speed then
new_speed_y = new_speed_y + acceleration
if new_speed_y > max_speed then
new_speed_y = max_speed
end
end
end
-- Limit up speed
if new_speed_y < -max_speed then
new_speed_y = new_speed_y + acceleration
if new_speed_y > -max_speed then -- ease the speed to the intended range if not in the intended range
new_speed_y = -max_speed
end
end
-- Limit down speed
if new_speed_y > max_speed then
new_speed_y = new_speed_y - acceleration
if new_speed_y < max_speed then -- ease the speed to the intended range if not in the intended range
new_speed_y = max_speed
end
end
player.state.speed = vector.new(new_speed_x, new_speed_y)
end
player.UsePrimary = function ()
local mouse_pos = vector.new(love.mouse.getPosition())
MOUSE_GAME_POS = ((mouse_pos-PLAYER_SCREEN_POSITION) / ZOOM_MULT) + GAME.player.state.position
GAME.functions.Fire(player.state.position,MOUSE_GAME_POS, player.equips.gun, player)
end
player.UseSecondary = function ()
end
return player
end