Weeeeee/modules/player.lua
2022-03-02 18:42:51 +01:00

80 lines
2.3 KiB
Lua

local player = {}
player.x = 0
player.y = 500
player.width = 160
player.height = 160
player.speed = 200
player.jump_height = 500
player.state = "idle"
player.direction = "right"
player.alive = true
player.init = function(self, sound_effects, spritesheets, game)
self.sound_effects = sound_effects
self.spritesheets = spritesheets
self.game = game
player.animation = spritesheets["player_idle"]
end
player.walk_right = function(self, dt)
if self.x < self.game.width - self.width and self.alive then
self.x = self.x + self.speed * dt
self.state = "walk_right"
self.direction = "right"
self.animation = self.spritesheets["player_walk_right"]
end
end
player.walk_left = function(self, dt)
if self.x > 0 and self.alive then
self.x = self.x - self.speed * dt
self.state = "walk_left"
self.direction = "left"
self.animation = self.spritesheets["player_walk_left"]
end
end
player.jump = function(self, dt)
if self.y > 0 and self.alive then
self.y = self.y - self.jump_height * dt
self.state = "jump"
self.animation = self.spritesheets["player_jump"]
self.sound_effects["jump"]:play()
end
end
player.down = function(self, dt)
if self.y < self.game.height - self.height and self.alive then
self.y = self.y + self.jump_height * dt
self.state = "jump"
self.animation = self.spritesheets["player_jump"]
self.sound_effects["jump"]:play()
end
end
player.die = function(self, dt)
if self.alive then
self.alive = false
self.state = "die"
self.animation = self.spritesheets["player_die"]
self.sound_effects["die"]:play()
die_animation_elapsed = false
end
end
player.revive = function(self, dt)
if not self.alive then
self.alive = true
self.state = "idle"
self.animation = self.spritesheets["player_default"]
self.sound_effects["revive"]:play()
die_animation_elapsed = false
end
end
player.idle = function(self, dt)
if self.alive then
self.state = "idle"
self.animation = self.spritesheets["player_idle"]
end
end
player.default = function(self, dt)
if self.alive then
self.state = "default"
self.animation = self.spritesheets["player_default"]
end
end
return player