noble engine migration
This commit is contained in:
72
source/scripts/balebaSprite.lua
Normal file
72
source/scripts/balebaSprite.lua
Normal file
@@ -0,0 +1,72 @@
|
||||
Baleba = {}
|
||||
class('Baleba').extends(AnimatedSprite)
|
||||
|
||||
function randomFloat(lower, greater)
|
||||
return lower + math.random() * (greater - lower);
|
||||
end
|
||||
|
||||
local balebaImageTable = Graphics.imagetable.new("assets/sprites/baleba")
|
||||
|
||||
function Baleba:init(x, y, player, loop)
|
||||
Baleba.super.init(self, balebaImageTable)
|
||||
|
||||
-- Animation properties
|
||||
self:addState("run", 1,4, {tickStep = 2})
|
||||
self:setDefaultState("run")
|
||||
self:playAnimation()
|
||||
|
||||
-- Collision properties
|
||||
self:setZIndex(11)
|
||||
self:setCollideRect(3, 25, 40, 15)
|
||||
self:setTag(154)
|
||||
|
||||
-- Sprite properties
|
||||
self.standrate = randomFloat(0.3, 2)
|
||||
self.xVelocity = Baleba.standrate
|
||||
self.player = player
|
||||
self.loop = loop
|
||||
|
||||
-- Danger properties
|
||||
self.danger = Danger(366, y, self)
|
||||
self.danger:add()
|
||||
|
||||
-- Move to initial position
|
||||
self:moveTo(x, y)
|
||||
end
|
||||
|
||||
function Baleba:update()
|
||||
self:updateAnimation()
|
||||
|
||||
-- Speedup when player is moving right
|
||||
if self.player == nil or self.player.isMovingRight() == false then
|
||||
self.xVelocity = self.standrate
|
||||
else
|
||||
self.xVelocity = 2 + self.standrate
|
||||
end
|
||||
|
||||
self:moveTo(self.x-self.xVelocity, self.y)
|
||||
|
||||
-- Danger arrow management
|
||||
if self.x < 430 and self.danger ~= nil then
|
||||
self.danger:remove()
|
||||
self.danger = nil
|
||||
elseif self.danger == nil and self.x > 430 then
|
||||
self.danger = Danger(366, self.y, self)
|
||||
self.danger:add()
|
||||
end
|
||||
|
||||
-- Lifeciycle management
|
||||
if self.x < 0 and self.loop == true then
|
||||
self.standrate = randomFloat(0.5, 6)
|
||||
self:moveTo(math.random(450, 600), math.random(50, 200))
|
||||
elseif self.x < 0 then
|
||||
self:destroy()
|
||||
end
|
||||
end
|
||||
|
||||
function Baleba:destroy()
|
||||
if self.danger ~= nil then
|
||||
self.danger:remove()
|
||||
end
|
||||
self:remove()
|
||||
end
|
24
source/scripts/dangerSprite.lua
Normal file
24
source/scripts/dangerSprite.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
Danger = {}
|
||||
class("Danger").extends(AnimatedSprite)
|
||||
|
||||
local dangerImageTable = Graphics.imagetable.new("assets/sprites/danger")
|
||||
|
||||
function Danger:init(x, y, dangerousObject)
|
||||
Danger.super.init(self, dangerImageTable)
|
||||
self.dangerousObject = dangerousObject
|
||||
|
||||
-- Animation properties
|
||||
self:addState("run", 1,5, {tickStep = 2})
|
||||
self:setDefaultState("run")
|
||||
self:playAnimation()
|
||||
|
||||
-- Collision properties
|
||||
self:setZIndex(11)
|
||||
self:setTag(7)
|
||||
|
||||
self:moveTo(x, y)
|
||||
end
|
||||
|
||||
function Danger:update()
|
||||
self:updateAnimation()
|
||||
end
|
41
source/scripts/groundSprite.lua
Normal file
41
source/scripts/groundSprite.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
Ground = {}
|
||||
class("Ground").extends(Graphics.sprite)
|
||||
|
||||
local groundImage = playdate.graphics.image.new("assets/sprites/groundFin")
|
||||
|
||||
function Ground:init(x, y, player)
|
||||
Ground.super.init(self, groundImage)
|
||||
|
||||
-- Collision properties
|
||||
self:setZIndex(100)
|
||||
self:setTag(3)
|
||||
self:setCollideRect(0, 28, 800, 10)
|
||||
|
||||
-- Main properties
|
||||
Ground.moveSpeed = 2
|
||||
Ground.player = player
|
||||
|
||||
-- Move to initial position
|
||||
self:moveTo(x, y)
|
||||
end
|
||||
|
||||
function Ground:setMoveSpeed(speed)
|
||||
Ground.moveSpeed = speed
|
||||
end
|
||||
|
||||
function Ground:update()
|
||||
-- Speedup when player is moving right
|
||||
if Ground.player.isMovingRight() == false then
|
||||
Ground.moveSpeed = 0.2
|
||||
else
|
||||
Ground.moveSpeed = 1
|
||||
end
|
||||
|
||||
-- Reset position
|
||||
if self.x <= 0 then
|
||||
self:moveWithCollisions(400, self.y)
|
||||
end
|
||||
|
||||
-- Move ground
|
||||
self:moveWithCollisions(self.x-Ground.moveSpeed, self.y)
|
||||
end
|
214
source/scripts/player.lua
Normal file
214
source/scripts/player.lua
Normal file
@@ -0,0 +1,214 @@
|
||||
Player = {}
|
||||
class("Player").extends(AnimatedSprite)
|
||||
|
||||
local playerImageTable = Graphics.imagetable.new("assets/sprites/player")
|
||||
|
||||
function Player:init(x, y)
|
||||
Player.super.init(self, playerImageTable)
|
||||
|
||||
-- Animation properties
|
||||
self:addState("run", 1, 7, { tickStep = 2 })
|
||||
self:addState("up", 1, 7, { tickStep = 6 })
|
||||
self:addState("down", 1, 7, { tickStep = 6 })
|
||||
self:addState("boom", 15, 21, { tickStep = 10, loop = false })
|
||||
self:setDefaultState("down")
|
||||
self:playAnimation()
|
||||
|
||||
-- Collision properties
|
||||
self:moveTo(x, y)
|
||||
self:setZIndex(10)
|
||||
self:setCollideRect(3, 19, 63, 33)
|
||||
self:setTag(1)
|
||||
|
||||
-- Physics properties
|
||||
self.xVelocity = 0
|
||||
self.yVelocity = 0
|
||||
self.maxXSpeed = 2
|
||||
self.maxYSpeed = 5
|
||||
self.fallSpeed = 0.05
|
||||
self.maxFallSpeed = 0.4
|
||||
|
||||
-- Player State
|
||||
self.touchingGround = false
|
||||
self.touchingCeiling = false
|
||||
self.touchingWall = false
|
||||
Player.dead = false
|
||||
Player.bat = 10000
|
||||
Player.dischargeRate = 1
|
||||
self.cantDown = false
|
||||
Player.moveRight = false
|
||||
end
|
||||
|
||||
function Player:handleInput()
|
||||
if Player.bat <= 0 or Player.dead then
|
||||
return
|
||||
end
|
||||
|
||||
local crankChange, acceleratedChange = playdate.getCrankChange()
|
||||
|
||||
if playdate.buttonJustReleased(playdate.kButtonLeft) or playdate.buttonJustReleased(playdate.kButtonRight) then
|
||||
self.cantDown = false
|
||||
end
|
||||
|
||||
if playdate.buttonIsPressed(playdate.kButtonLeft) then
|
||||
self:changeToRunState("left")
|
||||
self:setRotation(10)
|
||||
self.cantDown = true
|
||||
Player.moveRight = false
|
||||
elseif playdate.buttonIsPressed(playdate.kButtonRight) then
|
||||
self:changeToRunState("right")
|
||||
self:setRotation(10)
|
||||
self.cantDown = true
|
||||
Player.moveRight = true
|
||||
end
|
||||
|
||||
if crankChange ~= 0 then
|
||||
local force = 0.01
|
||||
if crankChange > 0 then
|
||||
self:changeState("up")
|
||||
else
|
||||
self:changeState("down")
|
||||
force = 0.05
|
||||
end
|
||||
self.yVelocity = self.yVelocity - acceleratedChange * force
|
||||
elseif self.cantDown == false then
|
||||
self.cantDown = false
|
||||
self:changeToDownState()
|
||||
else
|
||||
self.yVelocity = self.fallSpeed
|
||||
end
|
||||
end
|
||||
|
||||
function Player:changeToDownState()
|
||||
Player.moveRight = false
|
||||
self.yVelocity = self.fallSpeed
|
||||
self.xVelocity = 0
|
||||
self:changeState("down")
|
||||
self:setRotation(0)
|
||||
end
|
||||
|
||||
function Player:changeToRunState(direction)
|
||||
if direction == "left" then
|
||||
self.xVelocity = -self.maxXSpeed
|
||||
self.globalFlip = 1
|
||||
self:changeState("run")
|
||||
elseif direction == "right" then
|
||||
self.xVelocity = self.maxXSpeed
|
||||
self.globalFlip = 0
|
||||
self:changeState("run")
|
||||
end
|
||||
end
|
||||
|
||||
function Player:handleMovementAndCollisions()
|
||||
if Player.dead then
|
||||
return
|
||||
end
|
||||
|
||||
local xVel = self.xVelocity
|
||||
|
||||
if (self.x < 20 and xVel < 0) or (self.x > 380 and xVel > 0) then -- Screen bounds
|
||||
xVel = 0
|
||||
end
|
||||
|
||||
local _, _, collisions, length = self:checkCollisions(self.x + xVel, self.y + self.yVelocity)
|
||||
|
||||
self.touchingGround = false
|
||||
self.touchingCeiling = false
|
||||
self.touchingWall = false
|
||||
|
||||
for i = 1, length do
|
||||
local collision = collisions[i]
|
||||
local collisionType = collision.type
|
||||
local collisionObject = collision.other
|
||||
local collisionTag = collisionObject:getTag()
|
||||
if collisionType == Graphics.sprite.kCollisionTypeSlide then
|
||||
if collision.normal.y == -1 then
|
||||
self.touchingGround = true
|
||||
elseif collision.normal.y == 1 then
|
||||
self.touchingCeiling = true
|
||||
end
|
||||
|
||||
if collision.normal.x ~= 0 then
|
||||
self.touchingWall = true
|
||||
end
|
||||
end
|
||||
|
||||
if collisionTag == 3 then -- Ground
|
||||
Player.dead = true
|
||||
self:changeState("boom")
|
||||
return
|
||||
elseif collisionTag == 154 then -- Baleba
|
||||
Player.dead = true
|
||||
self:changeState("boom")
|
||||
|
||||
local particleB = ParticlePoly(self.x, self.y)
|
||||
particleB:setThickness(2)
|
||||
particleB:setAngular(-15, 15)
|
||||
particleB:setSize(1, 2)
|
||||
particleB:setSpeed(1, 3)
|
||||
particleB:setMode(Particles.modes.STAY)
|
||||
particleB:setBounds(0, 0, 400, 240)
|
||||
particleB:setColour(Graphics.kColorXOR)
|
||||
particleB:add(20)
|
||||
collisionObject:remove()
|
||||
return
|
||||
end
|
||||
end
|
||||
self:moveTo(self.x + xVel, self.y + self.yVelocity)
|
||||
end
|
||||
|
||||
function Player:handleDischarge(state)
|
||||
if Player.dead then
|
||||
return
|
||||
end
|
||||
if Player.bat <= 0 then
|
||||
Player.bat = 0
|
||||
self.fallSpeed = 2
|
||||
self:changeToDownState()
|
||||
return
|
||||
end
|
||||
|
||||
if Player.bat < 5000 then
|
||||
self.fallSpeed = self.maxFallSpeed
|
||||
end
|
||||
|
||||
if state == "run" then
|
||||
Player.dischargeRate = 5
|
||||
elseif state == "up" then
|
||||
Player.dischargeRate = 10
|
||||
else
|
||||
Player.dischargeRate = 1
|
||||
end
|
||||
|
||||
if self.y < 5 then
|
||||
Player.dischargeRate = 20
|
||||
end
|
||||
|
||||
Player.bat = Player.bat - Player.dischargeRate
|
||||
end
|
||||
|
||||
function Player:update()
|
||||
self:updateAnimation()
|
||||
self:handleMovementAndCollisions()
|
||||
|
||||
if Player.dead then
|
||||
return
|
||||
end
|
||||
|
||||
local state = self:getCurrentState()["name"]
|
||||
|
||||
self:handleDischarge(state)
|
||||
self:handleInput()
|
||||
end
|
||||
|
||||
function Player:isMovingRight()
|
||||
return Player.moveRight
|
||||
end
|
||||
|
||||
function Player:isDead()
|
||||
return Player.dead
|
||||
end
|
||||
|
||||
function Player:getBat()
|
||||
return Player.bat
|
||||
end
|
Reference in New Issue
Block a user