refactor(levels): use the player/camera system of the base world.

This commit is contained in:
Kazhnuz 2019-06-17 16:17:21 +02:00
parent 201cc80d0a
commit d7210db5e5
9 changed files with 40 additions and 86 deletions

View File

@ -1,7 +1,7 @@
return { return {
version = "1.2", version = "1.2",
luaversion = "5.1", luaversion = "5.1",
tiledversion = "1.2.2", tiledversion = "1.2.4",
orientation = "orthogonal", orientation = "orthogonal",
renderorder = "right-down", renderorder = "right-down",
width = 210, width = 210,
@ -3126,7 +3126,7 @@ return {
{ {
type = "objectgroup", type = "objectgroup",
id = 5, id = 5,
name = "playerstart", name = "player",
visible = true, visible = true,
opacity = 1, opacity = 1,
offsetx = 0, offsetx = 0,

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.2" orientation="orthogonal" renderorder="right-down" width="210" height="17" tilewidth="16" tileheight="16" infinite="0" nextlayerid="6" nextobjectid="37"> <map version="1.2" tiledversion="1.2.4" orientation="orthogonal" renderorder="right-down" width="210" height="17" tilewidth="16" tileheight="16" infinite="0" nextlayerid="6" nextobjectid="37">
<tileset firstgid="1" name="base_tiles" tilewidth="16" tileheight="16" tilecount="16384" columns="128"> <tileset firstgid="1" name="base_tiles" tilewidth="16" tileheight="16" tilecount="16384" columns="128">
<image source="tilesets/base_tiles.png" width="2048" height="2048"/> <image source="tilesets/base_tiles.png" width="2048" height="2048"/>
<terraintypes> <terraintypes>
@ -779,7 +779,7 @@
<object id="34" x="192" y="112" width="16" height="16"/> <object id="34" x="192" y="112" width="16" height="16"/>
<object id="35" x="224" y="112" width="16" height="16"/> <object id="35" x="224" y="112" width="16" height="16"/>
</objectgroup> </objectgroup>
<objectgroup id="5" name="playerstart"> <objectgroup id="5" name="player">
<object id="36" x="32" y="192" width="32" height="32"/> <object id="36" x="32" y="192" width="32" height="32"/>
</objectgroup> </objectgroup>
</map> </map>

View File

@ -1,7 +1,7 @@
return { return {
version = "1.2", version = "1.2",
luaversion = "5.1", luaversion = "5.1",
tiledversion = "1.2.2", tiledversion = "1.2.4",
orientation = "orthogonal", orientation = "orthogonal",
renderorder = "right-down", renderorder = "right-down",
width = 100, width = 100,
@ -182,7 +182,9 @@ return {
height = 48, height = 48,
rotation = 0, rotation = 0,
visible = true, visible = true,
properties = {} properties = {
["batchActors"] = true
}
} }
} }
}, },
@ -345,7 +347,7 @@ return {
{ {
type = "objectgroup", type = "objectgroup",
id = 4, id = 4,
name = "playerstart", name = "player",
visible = true, visible = true,
opacity = 1, opacity = 1,
offsetx = 0, offsetx = 0,
@ -546,7 +548,9 @@ return {
height = 32, height = 32,
rotation = 0, rotation = 0,
visible = true, visible = true,
properties = {} properties = {
["batchActors"] = true
}
} }
} }
} }

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.2" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#55aaff" nextlayerid="8" nextobjectid="31"> <map version="1.2" tiledversion="1.2.4" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#55aaff" nextlayerid="8" nextobjectid="31">
<tileset firstgid="1" name="beastlands" tilewidth="16" tileheight="16" tilecount="2006" columns="59"> <tileset firstgid="1" name="beastlands" tilewidth="16" tileheight="16" tilecount="2006" columns="59">
<image source="tilesets/beastlands.png" trans="55aaff" width="944" height="544"/> <image source="tilesets/beastlands.png" trans="55aaff" width="944" height="544"/>
</tileset> </tileset>
@ -111,7 +111,11 @@
<properties> <properties>
<property name="item" value="coin"/> <property name="item" value="coin"/>
</properties> </properties>
<object id="26" x="736" y="1392" width="144" height="48"/> <object id="26" x="736" y="1392" width="144" height="48">
<properties>
<property name="batchActors" type="bool" value="true"/>
</properties>
</object>
</objectgroup> </objectgroup>
<objectgroup id="3" name="coin"> <objectgroup id="3" name="coin">
<object id="12" x="240" y="1472" width="16" height="16"/> <object id="12" x="240" y="1472" width="16" height="16"/>
@ -126,7 +130,7 @@
<object id="21" x="144" y="1392" width="16" height="16"/> <object id="21" x="144" y="1392" width="16" height="16"/>
<object id="22" x="144" y="1360" width="16" height="16"/> <object id="22" x="144" y="1360" width="16" height="16"/>
</objectgroup> </objectgroup>
<objectgroup id="4" name="playerstart"> <objectgroup id="4" name="player">
<object id="23" x="64" y="1472" width="32" height="32"/> <object id="23" x="64" y="1472" width="32" height="32"/>
</objectgroup> </objectgroup>
<objectgroup id="5" name="wall"> <objectgroup id="5" name="wall">
@ -144,6 +148,10 @@
<object id="27" x="528" y="1424" width="16" height="16"/> <object id="27" x="528" y="1424" width="16" height="16"/>
<object id="28" x="576" y="1424" width="16" height="16"/> <object id="28" x="576" y="1424" width="16" height="16"/>
<object id="29" x="624" y="1424" width="16" height="16"/> <object id="29" x="624" y="1424" width="16" height="16"/>
<object id="30" x="672" y="1408" width="32" height="32"/> <object id="30" x="672" y="1408" width="32" height="32">
<properties>
<property name="batchActors" type="bool" value="true"/>
</properties>
</object>
</objectgroup> </objectgroup>
</map> </map>

View File

@ -63,7 +63,15 @@ function Entity:getDirection()
end end
function Entity:isInsideView() function Entity:isInsideView()
return self.camera:isInsideView(self.x, self.y, self.w, self.h) local camx, camy, camw, camh = self.world.cameras:getViewCoordinate(1)
local x, y, w, h = self.x, self.y, self.w, self.h
local border = border or 0
if (x + w + border >= camx) and (x < camx + camw + border)
and (y + h + border >= camy) and (y < camy + camh + border) then
return true
else
return false
end
end end
return Entity return Entity

View File

@ -34,7 +34,7 @@ function Player:new(world, x, y)
end end
function Player:getStats(playerID) function Player:getStats(playerID)
self.pigID = self.manager.players[playerID].pigID self.pigID = playerID
self.stats = game.pigmanager:getPig(1) self.stats = game.pigmanager:getPig(1)
self.playerID = playerID self.playerID = playerID
end end
@ -131,11 +131,11 @@ end
function Player:limitMovement() function Player:limitMovement()
if (math.abs(self.xsp) >= self.maxxsp) then if (math.abs(self.xsp) >= self.maxxsp) then
self.xsp = self.maxxsp * math.sign(self.xsp) self.xsp = self.maxxsp * utils.math.sign(self.xsp)
end end
if (math.abs(self.ysp) >= self.maxysp) then if (math.abs(self.ysp) >= self.maxysp) then
self.ysp = self.maxysp * math.sign(self.ysp) self.ysp = self.maxysp * utils.math.sign(self.ysp)
end end
end end

View File

@ -4,8 +4,6 @@ local Level = Scene:extend()
-- Chargement des controllers -- Chargement des controllers
local World = require "scenes.levels.world" local World = require "scenes.levels.world"
local Camera = require "scenes.levels.camera"
local PlayerManager = require "scenes.levels.players"
local leveldatas = require "datas.levels" local leveldatas = require "datas.levels"
@ -44,15 +42,9 @@ function Level:loadMission(levelID, missionID)
end end
function Level:launchMission() function Level:launchMission()
self.playermanager = PlayerManager(self)
--self:resetSpawnAndEntities()
self.assets:silence() self.assets:silence()
self.assets:playMusic() self.assets:playMusic()
self.playermanager:addPlayer(1)
self.playermanager:spawnPlayer(1)
self.camera = Camera(self, self.playermanager.startx, self.playermanager.starty)
end end
@ -74,10 +66,6 @@ end
function Level:draw(dt) function Level:draw(dt)
utils.graphics.resetColor() utils.graphics.resetColor()
if (self.pause == false) then
self.playermanager:drawHUD()
end
end end
function Level:exit() function Level:exit()

View File

@ -25,8 +25,8 @@ end
function PlayerManager:spawnPlayer(playerID) function PlayerManager:spawnPlayer(playerID)
local play = self.players[playerID] local play = self.players[playerID]
Obj.Player(self.scene.world, self.startx, self.starty, playerID) --Obj.Player(self.scene.world, self.startx, self.starty, playerID)
self.activePlayer = playerID --self.activePlayer = playerID
end end
function PlayerManager:getPlayers() function PlayerManager:getPlayers()
@ -47,7 +47,8 @@ function PlayerManager:getPlayerByID(id)
local player local player
if (id == nil) then if (id == nil) then
error("You must have an ID to search") return nil
--error("You must have an ID to search")
end end
for i,v in ipairs(itemList) do for i,v in ipairs(itemList) do
@ -85,10 +86,6 @@ end
function PlayerManager:drawHUD(dt) function PlayerManager:drawHUD(dt)
if self:playerHaveObject(1) then
local player = self:getPlayerByID(1)
player:drawHUD()
end
end end

View File

@ -12,20 +12,6 @@ function World:new(scene, mapfile, actorlist)
self.super.new(self, scene, actorlist, mapfile) self.super.new(self, scene, actorlist, mapfile)
end end
function World:getStartPosition()
local startx, starty
for k, objectlayer in pairs(self.map.layers) do
if objectlayer.name == "playerstart" then
for k, object in pairs(objectlayer.objects) do
startx = object.x + (object.width/2)
starty = object.y + (object.height) - 12
end
self.map:removeLayer("playerstart")
end
end
return startx, starty
end
-- ACTORS FUNCTIONS -- ACTORS FUNCTIONS
-- Some Bump2D Wrapper not handled by gamecore -- Some Bump2D Wrapper not handled by gamecore
@ -33,41 +19,4 @@ function World:changeActorData(actor)
self.actors:update(actor, actor.x, actor.y, actor.w, actor.h) self.actors:update(actor, actor.x, actor.y, actor.w, actor.h)
end end
-- UPDATE FUNCTIONS
-- All update functions
function World:update(dt)
self.scene.playermanager:update(dt)
self.super.update(self, dt)
self.scene.camera:update(dt)
end
-- DRAW FUNCTIONS
-- All function to draw the map, world and entities
function World:draw()
-- Ona attache puis détache la caméra pour dessiner le monde, afin que celui
-- reste "fixe" tandis que le jouer bouge.
self.scene.camera:floorCoord()
self:drawBackgroundColor()
self.scene.camera:attach()
self:drawMap()
self:drawActors()
self.scene.camera:detach()
end
function World:drawMap()
-- Du à la manière dont fonctionne STI, on est obligé de récupérer les info
-- de position de camera pour afficher la carte par rapport à ces infos
local tx, ty = self.scene.camera:getCoord()
local scale = self.scene.camera:getScale()
local tx = tx
local ty = ty
tx = math.floor(tx)
ty = math.floor(ty)
self.map:draw(-tx, -ty, scale, scale)
end
return World return World