improvement(levels): use Actor2D integrated sprite system

This commit is contained in:
Kazhnuz 2019-06-16 20:16:48 +02:00
parent cd7e381672
commit 5241cb8d6e
5 changed files with 21 additions and 34 deletions

View File

@ -2,6 +2,8 @@ return {
metadata = {
width = 32,
height = 48,
ox = 16,
oy = 12,
defaultAnim = "idle"
},
animations = {

View File

@ -2,7 +2,9 @@ return {
metadata = {
width = 32,
height = 48,
defaultAnim = "walk"
ox = 16,
oy = 12,
defaultAnim = "idle"
},
animations = {
["idle"] = {

View File

@ -5,7 +5,7 @@ local Loot = Entity:extend()
function Loot:new(world, x, y, name)
Loot.super.new(self, world, "loot", x, y, 16, 16)
self.type = "loot"
self.sprite = name
self:setSprite(name)
end
function Loot:takeLoot()
@ -20,8 +20,4 @@ function Loot:update(dt)
Loot.super.update(self, dt)
end
function Loot:draw()
self.scene.assets.sprites[self.sprite]:drawAnimation(self.x, self.y)
end
return Loot

View File

@ -88,8 +88,4 @@ function Entity:isInsideView()
return self.camera:isInsideView(self.x, self.y, self.w, self.h)
end
function Entity:draw()
-- Cette fonction en contient rien par défaut
end
return Entity

View File

@ -1,18 +1,18 @@
local Entity = require "scenes.levels.entities.parent"
local Player = Entity:extend()
local PLAYER_W, PLAYER_H = 32, 24
-- INIT functions
-- Initialize the player
function Player:new(world, x, y, playerID)
local w, h = 32, 24
self.direction = 1
self.startx, self.starty = x, y
Player.super.new(self, world, "player", x - (w / 2), y - (h / 2), w, h)
Player.super.new(self, world, "player", x, y, PLAYER_W, PLAYER_H)
self.manager = self.scene.playermanager
self.center = {
@ -29,6 +29,7 @@ function Player:new(world, x, y, playerID)
self.currentWeapon = 1
self:setDebugColor(0,255,0)
self:setSprite(self.stats.race, PLAYER_W/2, -PLAYER_H/2)
end
function Player:getStats(playerID)
@ -65,7 +66,7 @@ end
function Player:update(dt)
self.keys = self.scene.sources[1].keys
self.scene.assets.sprites[self.stats.race]:setCustomSpeed(math.abs(self.xsp / 60))
self:setCustomSpeed(math.abs(self.xsp / 60))
self:actionMove(dt)
self:shoot(dt)
@ -86,6 +87,8 @@ function Player:update(dt)
if self.y >= (height + 64) then
self:die()
end
self:setAnimation()
end
function Player:setFilter()
@ -199,41 +202,29 @@ end
-- DRAW functions
function Player:getAnimation()
function Player:setAnimation()
local animation = "idle"
self:setSpriteScallingX(self.direction)
if (self.onGround) then
if (self.xsp == 0) then
animation = "idle"
self:changeAnimation("idle", false)
else
if (self.keys["left"].isDown and (self.xsp > 0)) or
(self.keys["right"].isDown and (self.xsp < 0)) then
animation = "brake"
self:changeAnimation("brake", true)
else
animation = "walk"
self:changeAnimation("walk", false)
end
end
else
if (self.ysp < 0) and (self.isJumping) then
animation = "jump"
self:changeAnimation("jump", true)
else
animation = "fall"
self:changeAnimation("fall", true)
end
end
if not (self.scene.assets.sprites[self.stats.race]:animationExist(animation)) then
animation = "idle"
end
self.scene.assets.sprites[self.stats.race]:changeAnimation(animation, false)
return animation
end
function Player:draw(dt)
local drawx, drawy = utils.math.floorCoord(self.center.x, self.center.y)
local animation = self:getAnimation()
self.scene.assets.sprites[self.stats.race]:drawAnimation(drawx, drawy, 0, self.direction, 1, 16, 36)
end
-- DEATH and HIT functions