Weeeeee/modules/player.lua

85 lines
2.6 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
2022-03-02 20:31:33 +01:00
self.x = self.game.width / 2 + 8
self.y = self.game.height + 8
2022-03-02 18:36:52 +01:00
player.animation = spritesheets["player_idle"]
end
player.walk_right = function(self, dt)
2022-03-02 20:31:33 +01:00
if self.x < self.game:getcurlevel().width - self.width - self.game.width - 8 and self.alive then
2022-03-02 18:36:52 +01:00
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)
2022-03-02 20:31:33 +01:00
if self.x > self.game.width / 2 + 8 and self.alive then
2022-03-02 18:36:52 +01:00
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)
2022-03-02 20:31:33 +01:00
if self.y > self.game.height / 2 + 8 and self.alive then
2022-03-02 18:36:52 +01:00
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)
2022-03-02 20:31:33 +01:00
if self.y < self.game:getcurlevel().height - self.height - self.game.height / 2 - 8 and self.alive then
2022-03-02 18:36:52 +01:00
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