Weeeeee/modules/player.lua

83 lines
2.4 KiB
Lua
Raw Normal View History

2022-03-02 18:36:52 +01:00
local player = {}
player.x = 0
player.y = 500
2022-03-02 19:15:45 +01:00
player.width = 140
2022-03-02 18:36:52 +01:00
player.height = 160
player.speed = 200
2022-03-02 19:15:45 +01:00
player.speed_jumping = 400
player.speed_walking = 200
2022-03-02 18:36:52 +01:00
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()
2022-03-02 19:15:45 +01:00
self.spritesheets["player_die"]:setFrame(1)
2022-03-02 18:36:52 +01:00
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()
2022-03-02 19:15:45 +01:00
self.spritesheets["player_die"]:setFrame(1)
2022-03-02 18:36:52 +01:00
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