local player = {} player.width = 140 player.height = 160 player.speed = 320000 player.speed_jumping = 640000 player.speed_walking = 320000 player.jump_height = 400 player.state = "idle" player.direction = "right" player.alive = true player.object = {} player.init = function(self, sound_effects, spritesheets, game) self.sound_effects = sound_effects self.spritesheets = spritesheets self.game = game 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 end player.walk_right = function(self, dt) if self.alive then self.object.body:applyForce(self.speed * dt, 0) self.state = "walk_right" self.direction = "right" self.animation = self.spritesheets["player_walk_right"] end end player.walk_left = function(self, dt) if self.alive then self.object.body:applyForce(-(self.speed * dt), 0) self.state = "walk_left" self.direction = "left" self.animation = self.spritesheets["player_walk_left"] end end player.jump = function(self, dt) --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 end end player.down = function(self, dt) if self.alive then self.object.body:applyForce(0, 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() self.spritesheets["player_die"]:setFrame(1) 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() self.spritesheets["player_die"]:setFrame(1) 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 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 return player