Weeeeee/modules/player.lua

117 lines
3.8 KiB
Lua
Raw Normal View History

2022-03-02 18:36:52 +01:00
local player = {}
2022-03-02 19:15:45 +01:00
player.width = 140
2022-03-02 18:36:52 +01:00
player.height = 160
2022-03-03 13:13:17 +01:00
player.speed = 320000
player.speed_jumping = 640000
player.speed_walking = 320000
player.jump_height = 400
2022-03-02 18:36:52 +01:00
player.state = "idle"
player.direction = "right"
player.alive = true
2022-03-03 13:13:17 +01:00
player.object = {}
2022-03-02 18:36:52 +01:00
player.init = function(self, sound_effects, spritesheets, game)
self.sound_effects = sound_effects
self.spritesheets = spritesheets
self.game = game
2022-03-03 13:13:17 +01:00
self.animation = spritesheets["player_idle"]
self.object.body = love.physics.newBody(game.world, player.width, player.height, "dynamic")
self.object.shape = love.physics.newRectangleShape(player.width, player.height)
self.object.fixture = love.physics.newFixture(self.object.body, self.object.shape)
self.object.fixture:setUserData("player")
self.spawnx, self.spawny = game:getcurlevel().width / 2, game:getcurlevel().height / 4 - self.height * 2
self.object.body:setPosition(self.spawnx, self.spawny)
self.game.player = self
2022-03-02 18:36:52 +01:00
end
player.walk_right = function(self, dt)
2022-03-03 13:13:17 +01:00
if self.alive then
self.object.body:applyForce(self.speed * dt, 0)
2022-03-02 18:36:52 +01:00
self.state = "walk_right"
self.direction = "right"
self.animation = self.spritesheets["player_walk_right"]
end
end
player.walk_left = function(self, dt)
2022-03-03 13:13:17 +01:00
if self.alive then
self.object.body:applyForce(-(self.speed * dt), 0)
2022-03-02 18:36:52 +01:00
self.state = "walk_left"
self.direction = "left"
self.animation = self.spritesheets["player_walk_left"]
end
end
player.jump = function(self, dt)
2022-03-03 13:13:17 +01:00
--check if alive and if on floor
if self.alive then
--check all contacts
for i,v in pairs(self.object.body:getContacts()) do
--get fixtures
local fixtureA, fixtureB = v:getFixtures( )
--get userdata
local userdataA = fixtureA:getUserData()
local userdataB = fixtureB:getUserData()
--check if on floor
if userdataA == "floor" or userdataB == "floor" then
--jump
self.object.body:applyLinearImpulse(0, -self.jump_height)
self.state = "jump"
self.animation = self.spritesheets["player_jump"]
self.sound_effects["jump"]:play()
break
end
end
2022-03-02 18:36:52 +01:00
end
end
player.down = function(self, dt)
2022-03-03 13:13:17 +01:00
if self.alive then
self.object.body:applyForce(0, self.jump_height * dt)
2022-03-02 18:36:52 +01:00
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
2022-03-03 13:13:17 +01:00
player.update = function(self, dt)
if self.alive then
--check if below map
if self.object.body:getY() > self.game:getcurlevel().height then
self:die()
--reset position, rotation and velocity
self.object.body:setPosition(self.spawnx, self.spawny)
self.object.body:setAngle(0)
self.object.body:setLinearVelocity(0, 0)
self:revive()
end
end
end
2022-03-02 18:36:52 +01:00
return player