-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
231 lines (185 loc) · 6.91 KB
/
Copy pathplayer.lua
File metadata and controls
231 lines (185 loc) · 6.91 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
local Player = {}
function Player.new(x, y)
local self = {
x = x or 0,
y = y or 0,
speed = 200,
scale = .2,
health = 100,
maxHealth = 100,
currentState = "idle",
animations = {},
currentAnimation = nil,
facingDirection = 1, -- 1 for right, -1 for left
_debugTimer = 0 -- Timer for throttling debug updates
}
-- Load all animation types
self.animations = {
idle = Player.loadAnimation("idle", 10), -- Adjust frame count
walk = Player.loadAnimation("walk", 10),
shoot = Player.loadAnimation("shoot",10),
walk_shoot = Player.loadAnimation("walk_shoot", 10),
melee = Player.loadAnimation("melee", 10)
}
-- Set initial animation
self.currentAnimation = self.animations[self.currentState]
setmetatable(self, {__index = Player})
return self
end
function Player.loadAnimation(animName, frameCount)
local animation = {
frames = {},
currentFrame = 1,
timer = 0,
frameTime = 0.1,
loop = true
}
-- Load frames for this animation
for i = 1, frameCount do
local imagePath = "images/trooper/" .. animName .. "__" .. i .. ".png"
local frame = love.graphics.newImage(imagePath)
table.insert(animation.frames, frame)
end
-- Special cases for non-looping animations
if animName == "melee" or animName == "shoot" then
animation.loop = false
animation.frameTime = 0.08 -- Faster for action animations
end
return animation
end
function Player:setState(newState)
if self.currentState ~= newState and self.animations[newState] then
self.currentState = newState
self.currentAnimation = self.animations[newState]
self.currentAnimation.currentFrame = 1
self.currentAnimation.timer = 0
end
end
function Player:update(dt, level, input)
local moving = false
local shooting = false
local melee = false
-- Try to get our input module
local Input = input or package.loaded["input"]
-- Input handling
if Input then
-- Get movement vector from input module
local dx, dy = Input.getMovementVector()
if dx ~= 0 or dy ~= 0 then
self.x = self.x + dx * self.speed * dt
self.y = self.y + dy * self.speed * dt
moving = true
-- Update facing direction based on horizontal movement
if dx ~= 0 then
self.facingDirection = (dx > 0) and 1 or -1 -- 1 for right, -1 for left
end
end
-- Combat inputs using Input
if Input.isDown("jump") then
shooting = true
end
if Input.isDown("attack") then
melee = true
end
else
-- Legacy input handling if Input not provided
if love.keyboard.isDown("w", "up") then
self.y = self.y - self.speed * dt
moving = true
end
if love.keyboard.isDown("s", "down") then
self.y = self.y + self.speed * dt
moving = true
end
if love.keyboard.isDown("a", "left") then
self.x = self.x - self.speed * dt
moving = true
self.facingDirection = -1 -- Facing left
end
if love.keyboard.isDown("d", "right") then
self.x = self.x + self.speed * dt
moving = true
self.facingDirection = 1 -- Facing right
end
-- Combat inputs (you can change these keys)
if love.keyboard.isDown("space") then
shooting = true
end
if love.keyboard.isDown("f") then
melee = true
end
end
-- Determine animation state based on actions
local newState = "idle"
if melee then
newState = "melee"
elseif moving and shooting then
newState = "walk_shoot"
elseif shooting then
newState = "shoot"
elseif moving then
newState = "walk"
end
self:setState(newState)
-- Update current animation
self:updateAnimation(dt)
if level then
-- Keep player within level boundaries
self.x = math.max(0, math.min(level.width, self.x))
self.y = math.max(0, math.min(level.height, self.y))
else
-- Existing screen boundary code
self.x = math.max(0, math.min(love.graphics.getWidth(), self.x))
self.y = math.max(0, math.min(love.graphics.getHeight(), self.y))
end
-- Update debug info in Console
-- We only do this occasionally to prevent console spam
if self._debugTimer and self._debugTimer > 0 then
self._debugTimer = self._debugTimer - dt
else self._debugTimer = 0.5 -- Update debug info every half second
-- Get the debug console module
local DebugConsole = require("lib.debug_console")
-- Update debug values using the variable registration system
DebugConsole.updateVariable("playerState", self.currentState)
DebugConsole.updateVariable("playerFacing", (self.facingDirection == 1 and "Right" or "Left"))
DebugConsole.updateVariable("playerPosition", {x = math.floor(self.x), y = math.floor(self.y)})
end
end
function Player:updateAnimation(dt)
if not self.currentAnimation then
return
end
self.currentAnimation.timer = self.currentAnimation.timer + dt
if self.currentAnimation.timer >= self.currentAnimation.frameTime then
self.currentAnimation.timer = 0
self.currentAnimation.currentFrame = self.currentAnimation.currentFrame + 1
-- Handle animation end
if self.currentAnimation.currentFrame > #self.currentAnimation.frames then
if self.currentAnimation.loop then
self.currentAnimation.currentFrame = 1
else
-- Non-looping animation finished, return to appropriate state
self.currentAnimation.currentFrame = #self.currentAnimation.frames
if self.currentState == "melee" or self.currentState == "shoot" then
self:setState("idle") -- Return to idle after action
end
end
end
end
end
function Player:draw(offsetX, offsetY)
offsetX = offsetX or 0
offsetY = offsetY or 0
if not self.currentAnimation then
return
end
local currentImage = self.currentAnimation.frames[self.currentAnimation.currentFrame]
if currentImage then
-- Calculate scale with direction (negative scale flips the image)
local scaleX = self.scale * self.facingDirection
local scaleY = self.scale
love.graphics.draw(currentImage, self.x + offsetX, self.y + offsetY, 0, scaleX, scaleY, currentImage:getWidth()/2, currentImage:getHeight()/2)
end
-- Debug info is now handled by the Console module
end
return Player