2019-12-28 12:15:43 +01:00
|
|
|
local cwd = (...):gsub('%.player$', '') .. "."
|
|
|
|
local Parent = require(cwd .. "parent")
|
|
|
|
local Player = Parent:extend()
|
|
|
|
|
2019-12-28 13:54:57 +01:00
|
|
|
local Frame = require("game.modules.gui.frame")
|
2019-12-30 22:55:01 +01:00
|
|
|
local Statusbar = require("game.modules.gui.status")
|
2019-12-28 13:54:57 +01:00
|
|
|
|
2020-04-25 12:29:07 +02:00
|
|
|
local Weapons = require(cwd .. "player.weapons")
|
2020-04-25 12:15:12 +02:00
|
|
|
|
2019-12-28 12:15:43 +01:00
|
|
|
function Player:new(world, x, y, z, id)
|
2020-04-25 12:15:12 +02:00
|
|
|
Player.super.new(self, world, "player", x, y, 0, 16, 16, 24, false)
|
2019-12-28 12:15:43 +01:00
|
|
|
self:setGravity(480*2)
|
|
|
|
|
2019-12-29 10:04:16 +01:00
|
|
|
self:init(id)
|
2019-12-28 12:15:43 +01:00
|
|
|
|
2019-12-28 13:54:57 +01:00
|
|
|
self.frame = Frame()
|
2019-12-28 12:15:43 +01:00
|
|
|
|
2020-04-25 12:29:07 +02:00
|
|
|
self.weapons = Weapons(self)
|
|
|
|
|
2019-12-28 12:15:43 +01:00
|
|
|
self.action = "normal"
|
2020-04-25 12:15:12 +02:00
|
|
|
|
|
|
|
self.rings = 0
|
|
|
|
self.score = 0
|
2019-12-28 12:15:43 +01:00
|
|
|
end
|
|
|
|
|
2019-12-29 10:04:16 +01:00
|
|
|
function Player:init(id)
|
|
|
|
self.playerID = id
|
|
|
|
self.charName = self.scene:getCharacterName(self.playerID)
|
|
|
|
self:setSprite(self.charName, 8, 10)
|
|
|
|
self:cloneSprite()
|
2019-12-30 22:55:01 +01:00
|
|
|
self.statusbar = Statusbar(self, "speed", 50)
|
2019-12-29 10:04:16 +01:00
|
|
|
end
|
|
|
|
|
2019-12-28 12:15:43 +01:00
|
|
|
function Player:updateStart(dt)
|
|
|
|
self.xfrc, self.yfrc = 480*3, 480*3
|
|
|
|
|
2020-02-02 08:27:33 +01:00
|
|
|
self:basicMovements()
|
2019-12-28 12:15:43 +01:00
|
|
|
|
2019-12-28 21:06:41 +01:00
|
|
|
if self.keys["A"].isPressed and (self.onGround) then
|
2019-12-28 12:15:43 +01:00
|
|
|
self.zsp = 280*1.33
|
|
|
|
end
|
|
|
|
|
2020-04-25 12:15:12 +02:00
|
|
|
if self.keys["B"].isPressed then
|
2020-04-25 12:29:07 +02:00
|
|
|
self.weapons:shoot(self.x, self.y+1, self.z+8, 1)
|
2019-12-28 12:15:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-02 08:27:33 +01:00
|
|
|
function Player:basicMovements()
|
|
|
|
|
|
|
|
if self.keys["up"].isDown then
|
|
|
|
self.ysp = -160
|
|
|
|
end
|
|
|
|
if self.keys["down"].isDown then
|
|
|
|
self.ysp = 160
|
|
|
|
end
|
|
|
|
if (self.world.autorun == true) then
|
|
|
|
self.xsp = 160
|
|
|
|
else
|
|
|
|
if self.keys["left"].isDown then
|
|
|
|
self.xsp = -160
|
|
|
|
end
|
|
|
|
if self.keys["right"].isDown then
|
|
|
|
self.xsp = 160
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2020-01-26 12:28:53 +01:00
|
|
|
function Player:collisionResponse(collision)
|
|
|
|
if collision.other.type == "collectible" then
|
|
|
|
collision.other.owner:getPicked(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:15:43 +01:00
|
|
|
function Player:animationEnded(name)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:updateEnd(dt)
|
|
|
|
self:setAnimation()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:setAnimation()
|
|
|
|
local gsp = utils.math.pointDistance(0, 0, self.xsp, self.ysp)
|
|
|
|
self:setCustomSpeed(math.abs(gsp) / 12)
|
|
|
|
self:setDirection(self.xsp)
|
|
|
|
if (self.action == "punching") then
|
|
|
|
--the animation system is already active
|
|
|
|
else
|
|
|
|
if (self.onGround) then
|
|
|
|
if (math.abs(self.xsp) > 0) or (math.abs(self.ysp) > 0) then
|
|
|
|
self:changeAnimation("walk", false)
|
|
|
|
else
|
|
|
|
self:changeAnimation("idle", false)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if (self.zsp) > 0 then
|
|
|
|
self:changeAnimation("jump", false)
|
|
|
|
else
|
|
|
|
self:changeAnimation("fall", false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:setDirection(direction)
|
|
|
|
direction = direction or 0
|
|
|
|
if direction ~= 0 then
|
|
|
|
direction = utils.math.sign(direction)
|
|
|
|
self.direction = direction
|
|
|
|
self:setSpriteScallingX(direction)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:getViewCenter()
|
|
|
|
local x, y = Player.super.getViewCenter(self)
|
2019-12-28 14:17:58 +01:00
|
|
|
return self.world:getViewCenter(x, y)
|
2019-12-28 12:15:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:draw()
|
|
|
|
Player.super.draw(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:drawHUD(id)
|
2019-12-28 13:54:57 +01:00
|
|
|
self.frame:draw()
|
2019-12-30 22:55:01 +01:00
|
|
|
self.statusbar:draw(8, 12)
|
2019-12-28 12:15:43 +01:00
|
|
|
end
|
|
|
|
|
2020-04-25 12:15:12 +02:00
|
|
|
function Player:setRing(value, isRelative)
|
|
|
|
if (isRelative == false) then
|
|
|
|
self.rings = 0
|
|
|
|
end
|
|
|
|
self.rings = self.rings + value
|
|
|
|
self.statusbar.rings = self.rings
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:setScore(value, isRelative)
|
|
|
|
if (isRelative == false) then
|
|
|
|
self.score = 0
|
|
|
|
end
|
|
|
|
self.score = self.score + value
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:15:43 +01:00
|
|
|
return Player
|