From 235d470c4b7c9c506e2c2a50afba64d7eb5d6597 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Thu, 7 Jan 2021 15:50:35 -0600 Subject: [PATCH] Add doors and buttons --- main.lua | 93 ++++++++++++++-- maps/tutorial/2.lua | 262 ++++++++++++++++++++++++++++++++++++++++++++ maps/tutorial/2.tmx | 42 +++++++ 3 files changed, 385 insertions(+), 12 deletions(-) create mode 100644 maps/tutorial/2.lua create mode 100644 maps/tutorial/2.tmx diff --git a/main.lua b/main.lua index d971d4e..0783f24 100644 --- a/main.lua +++ b/main.lua @@ -6,7 +6,7 @@ stalker = require "lib/STALKER-X" downwall = 1; rightwall = 2; leftwall = 3 mainmenu = 0; game = 1; gameover = 2; pause = 3 -world = wind.newWorld(0, 40, true) +world = wind.newWorld(0, 50, true) -- GAME STATES -------------------------------------------------------------------------------- @@ -208,7 +208,7 @@ end function game_load() mode = game camera:fade(.2, {0,0,0,0}) - map = Map:new("maps/tutorial/1.lua") + map = Map:new("maps/tutorial/2.lua") camera:setFollowLerp(0.1) camera:setFollowStyle('PLATFORMER') @@ -311,7 +311,7 @@ function Monk:initialize(x, y, count) for i=0,(count-1) do self.monks[i] = world:newRectangleCollider(x - (i * 20), y, 16, 16); - self.monks[i]:setCollisionClass('Monk') + self.monks[i]:setCollisionClass('monk') self.monks[i]:setObject(self) self.monkSprites[i] = 'default' self.frozen[i] = false @@ -341,6 +341,7 @@ function Monk:update(dt) local allFrozen = true for k,frozen in pairs(self.frozen) do + if (frozen == true) then self.monks[k]:setType('static'); end if (frozen == false) then allFrozen = false; end end if (allFrozen == true) then gameover_load(); end @@ -440,8 +441,6 @@ function Monk:freeze (indice) indice = indice or self.current local monk = self.monks[indice] self.frozen[indice] = true - monk:setType('static') - monk:setCollisionClass('Platform') if (indice == self.current) then for i=0,self.last do @@ -465,8 +464,8 @@ function Monk:makeCollisionCallback (i) local function collision(collision1, collision2, contact) local nx, ny = contact:getNormal( ) - if collision1.collision_class == "Monk" - and collision2.collision_class == "Platform" + if (collision1.collision_class == "monk" + and collision2.collision_class == "platform") then if (math.abs(ny) == 1) then self.onGround[i] = downwall @@ -477,6 +476,17 @@ function Monk:makeCollisionCallback (i) if not (self.onGround[i] == 0) then return; end self.onGround[i] = rightwall end + + elseif (collision1.collision_class == "monk" + and collision2.collision_class == "button") + then + map.buttonHit = true + + elseif (collision1.collision_class == "monk" + and collision2.collision_class == "frozenButton" + and self.frozen[i] == true) + then + map.frozenHit = true end end return collision @@ -514,6 +524,9 @@ function Map:initialize(filepath) self.outOfBounds = "die" self.spawn = {['x'] = 100, ['y'] = 100} self.respawn = {['x'] = 100, ['y'] = 100} + self.buttonHit = false + self.frozenHit = false + local maptable = dofile(filepath) self.width,self.height = maptable.width,maptable.height @@ -524,9 +537,16 @@ function Map:initialize(filepath) local monkX,monkY = 100,100 world:destroy() world = wind.newWorld(0, 400, true) - world:addCollisionClass('Monk') - world:addCollisionClass('Banana') - world:addCollisionClass('Platform') + world:addCollisionClass('monk') + world:addCollisionClass('banana') + world:addCollisionClass('platform') + world:addCollisionClass('button') + world:addCollisionClass('frozenButton') + world:addCollisionClass('toyButton') + world:addCollisionClass('door') + world:addCollisionClass('frozenDoor') + world:addCollisionClass('toyDoor') + world:addCollisionClass('toy') for n,layer in pairs(maptable.layers) do if not (layer.type == "objectgroup") then break; end @@ -537,7 +557,7 @@ function Map:initialize(filepath) world:newRectangleCollider(object.x, object.y, object.width, object.height) self.tables[object.id]:setType('static') - self.tables[object.id]:setCollisionClass('Platform') + self.tables[object.id]:setCollisionClass('platform') elseif (object.shape == "polygon") then local ox,oy = object.x,object.y @@ -551,7 +571,7 @@ function Map:initialize(filepath) self.tables[object.id] = world:newPolygonCollider(vertices) self.tables[object.id]:setType('static') - self.tables[object.id]:setCollisionClass('Platform') + self.tables[object.id]:setCollisionClass('platform') elseif (object.shape == "text") then self.tables[object.id] = object @@ -570,6 +590,29 @@ function Map:initialize(filepath) elseif (object.shape == "point" and object.type == "banana") then self.objects[object.id] = Banana:new(object.x, object.y) end + + local thisTable = self.tables[object.id] + if (object.type == "door" or object.type == "frozenDoor" + or object.type == "toyDoor") + then + thisTable.normPos = {} + thisTable.normPos['x'],thisTable.normPos['y'] = + thisTable.body:getPosition() + end + + if (object.type == "door") then + thisTable:setCollisionClass('door') + elseif (object.type == "frozenDoor") then + thisTable:setCollisionClass('frozenDoor') + elseif (object.type == "toyDoor") then + thisTable:setCollisionClass('toyDoor') + elseif (object.type == "button") then + thisTable:setCollisionClass('button') + elseif (object.type == "frozenButton") then + thisTable:setCollisionClass('frozenButton') + elseif (object.type == "toyButton") then + thisTable:setCollisionClass('toyButton') + end end end player = Monk:new(self.spawn['x'], self.spawn['y'], monkCount) @@ -580,6 +623,7 @@ function Map:update(dt) local heightMax = self.height * self.tileHeight + 100 local widthMax = self.width * self.tileWidth + 200 + -- enforce borders for i=0,player.last do local x,y = player.monks[i].body:getPosition() @@ -592,6 +636,31 @@ function Map:update(dt) self.respawn['x'], self.respawn['y']) end end + + -- unlock or reset doors + for k,table in pairs(self.tables) do + if (table.collision_class == "door" and self.buttonHit == true) then + table.body:setPosition(-1000, -1000) + elseif (table.collision_class == "door") then + table.body:setPosition(table.normPos['x'], table.normPos['y']) + + elseif (table.collision_class == "frozenDoor" + and self.frozenHit == true) + then + table.body:setPosition(-1000, -1000) + elseif (table.collision_clas == "frozenDoor") then + table.body:setPosition(table.normPos['x'], table.normPos['y']) + + elseif (table.collision_class == "toyDoor" + and self.toyHit == true) + then + table.body:setPosition(-1000, -1000) + elseif (table.collision_clas == "toyDoor") then + table.body:setPosition(table.normPos['x'], table.normPos['y']) + end + end + + self.toyHit = false end diff --git a/maps/tutorial/2.lua b/maps/tutorial/2.lua new file mode 100644 index 0000000..edab4a4 --- /dev/null +++ b/maps/tutorial/2.lua @@ -0,0 +1,262 @@ +return { + version = "1.4", + luaversion = "5.1", + tiledversion = "1.4.3", + orientation = "orthogonal", + renderorder = "right-down", + width = 50, + height = 20, + tilewidth = 16, + tileheight = 16, + nextlayerid = 7, + nextobjectid = 18, + properties = {}, + tilesets = {}, + layers = { + { + type = "objectgroup", + draworder = "topdown", + id = 2, + name = "platforms", + visible = true, + opacity = 1, + offsetx = 0, + offsety = 0, + properties = {}, + objects = { + { + id = 1, + name = "", + type = "", + shape = "polygon", + x = 752, + y = 128, + width = 0, + height = 0, + rotation = 0, + visible = true, + polygon = { + { x = 0, y = 0 }, + { x = -224, y = 48 }, + { x = -1.33333, y = 48 } + }, + properties = {} + }, + { + id = 2, + name = "", + type = "", + shape = "rectangle", + x = 748, + y = 14.6667, + width = 52, + height = 161.333, + rotation = 0, + visible = true, + properties = {} + }, + { + id = 3, + name = "", + type = "", + shape = "rectangle", + x = 1.33333, + y = 177.333, + width = 797.333, + height = 16, + rotation = 0, + visible = true, + properties = {} + }, + { + id = 4, + name = "", + type = "", + shape = "rectangle", + x = -2.66667, + y = 30.6667, + width = 510.667, + height = 16, + rotation = 0, + visible = true, + properties = {} + }, + { + id = 5, + name = "", + type = "", + shape = "rectangle", + x = -2.66667, + y = 46.6667, + width = 20, + height = 145.333, + rotation = 0, + visible = true, + properties = {} + } + } + }, + { + type = "objectgroup", + draworder = "topdown", + id = 4, + name = "special", + visible = true, + opacity = 1, + offsetx = 0, + offsety = 0, + properties = {}, + objects = { + { + id = 9, + name = "", + type = "spawn", + shape = "point", + x = 697.333, + y = 134.667, + width = 0, + height = 0, + rotation = 0, + visible = true, + properties = { + ["count"] = 2 + } + }, + { + id = 12, + name = "", + type = "banana", + shape = "point", + x = 54.6667, + y = 174.667, + width = 0, + height = 0, + rotation = 0, + visible = true, + properties = {} + } + } + }, + { + type = "objectgroup", + draworder = "topdown", + id = 6, + name = "signs", + visible = true, + opacity = 1, + offsetx = 0, + offsety = 0, + properties = {}, + objects = { + { + id = 16, + name = "", + type = "", + shape = "text", + x = 515.628, + y = 63.5, + width = 82.0781, + height = 17, + rotation = 0, + visible = true, + text = "touch it! go on!", + wrap = true, + properties = {} + }, + { + id = 17, + name = "", + type = "", + shape = "text", + x = 151.628, + y = 63.5, + width = 82.0781, + height = 17, + rotation = 0, + visible = true, + text = "frozen monkeys + frozen buttons", + wrap = true, + properties = {} + } + } + }, + { + type = "objectgroup", + draworder = "topdown", + id = 3, + name = "doors", + visible = true, + opacity = 1, + offsetx = 0, + offsety = 0, + properties = {}, + objects = { + { + id = 7, + name = "", + type = "door", + shape = "rectangle", + x = 444, + y = 49.3333, + width = 20, + height = 128, + rotation = 0, + visible = true, + properties = {} + }, + { + id = 8, + name = "", + type = "frozenDoor", + shape = "rectangle", + x = 110.667, + y = 48, + width = 17.3333, + height = 129.333, + rotation = 0, + visible = true, + properties = {} + } + } + }, + { + type = "objectgroup", + draworder = "topdown", + id = 5, + name = "buttons", + visible = true, + opacity = 1, + offsetx = 0, + offsety = 0, + properties = {}, + objects = { + { + id = 10, + name = "", + type = "button", + shape = "rectangle", + x = 480, + y = 166.667, + width = 42.6667, + height = 9.33333, + rotation = 0, + visible = true, + properties = {} + }, + { + id = 11, + name = "", + type = "frozenButton", + shape = "rectangle", + x = 176, + y = 166.667, + width = 58.6667, + height = 9.33333, + rotation = 0, + visible = true, + properties = {} + } + } + } + } +} diff --git a/maps/tutorial/2.tmx b/maps/tutorial/2.tmx new file mode 100644 index 0000000..ee6502b --- /dev/null +++ b/maps/tutorial/2.tmx @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + touch it! go on! + + + frozen monkeys + frozen buttons + + + + + + + + + + +