Map bounds; mainmenu and gameover menu

This commit is contained in:
Jaidyn Ann 2021-01-07 13:41:04 -06:00
parent 1ddd35f28c
commit 2054e27282
3 changed files with 354 additions and 93 deletions

258
main.lua
View File

@ -6,7 +6,7 @@ stalker = require "lib/STALKER-X"
downwall = 1; rightwall = 2; leftwall = 3 downwall = 1; rightwall = 2; leftwall = 3
mainmenu = 0; game = 1; gameover = 2; pause = 3 mainmenu = 0; game = 1; gameover = 2; pause = 3
world = wind.newWorld(0, 400, true) world = wind.newWorld(0, 40, true)
-- GAME STATES -- GAME STATES
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -20,8 +20,11 @@ function love.load ()
a_ttf = love.graphics.newFont("art/font/alagard.ttf", nil, "none") a_ttf = love.graphics.newFont("art/font/alagard.ttf", nil, "none")
r_ttf = love.graphics.newFont("art/font/romulus.ttf", nil, "none") r_ttf = love.graphics.newFont("art/font/romulus.ttf", nil, "none")
-- mainmenu_load() camera = stalker()
game_load() camera:setFollowStyle('PLATFORMER')
camera:setFollowLerp(0.1)
mainmenu_load()
end end
@ -31,15 +34,24 @@ function love.update(dt)
elseif(mode == gameover) then gameover_update(dt) elseif(mode == gameover) then gameover_update(dt)
elseif(mode == pause) then pause_update(dt) elseif(mode == pause) then pause_update(dt)
end end
camera:update(dt)
end end
function love.draw() function love.draw()
camera:attach()
if(mode == mainmenu) then mainmenu_draw() if(mode == mainmenu) then mainmenu_draw()
elseif(mode == game) then game_draw() elseif(mode == game) then game_draw()
elseif(mode == gameover) then gameover_draw() elseif(mode == gameover) then gameover_draw()
elseif(mode == pause) then pause_draw() elseif(mode == pause) then pause_draw()
end end
camera:detach()
camera:draw()
end
function love.resize()
camera = stalker()
end end
@ -74,29 +86,64 @@ function mainmenu_load ()
-- bgm:setLooping(true) -- bgm:setLooping(true)
-- bgm:setVolume(1.5) -- bgm:setVolume(1.5)
-- --
-- frontMenu = Menu:new(100, 100, 30, 50, 2, camera:setBounds(0,0, 10, 10)
-- { { love.graphics.newText(a_ttf, "get banana!"), camera:fade(.2, {0,0,0,0}, function() camera = stalker() end)
-- function () game_load() end }, map = Map:new("maps/menu.lua")
-- { love.graphics.newText(a_ttf, "get help!"), player.following = true
-- function () helpScreen = true end },
-- { love.graphics.newText(a_ttf, "get outta dodge!"), frontMenu = Menu:new(100, 100, 30, 50, 3, {
-- function () love.event.quit(0) end } }) {love.graphics.newText(a_ttf, "get banana"),
function ()
camera:fade(.2, {0,0,0,1}, function() game_load() end)
end},
{love.graphics.newText(a_ttf, "get outta dodge"),
function () love.event.quit(0) end }})
end end
function mainmenu_update(dt) function mainmenu_update(dt)
world:update(dt)
player:update(dt)
map:update(dt)
-- make player monkey move randomly
local num = math.random(100)
local dirs = player.directionals
if (num == 1) then
dirs['left'] = 1
dirs['right'] = 0
elseif (num == 2) then
dirs['right'] = 1
dirs['left'] = 0
elseif (num == 3) then
dirs['up'] = 1
end
-- teleport off-screen monkeys
for i=0,player.last do
local x,y = player.monks[i].body:getPosition()
if (y > 2000) then
player.monks[i].body:setPosition(200,0)
end
end
print(camera.x, ",", camera.y)
end end
function mainmenu_draw () function mainmenu_draw ()
map:draw()
player:draw()
frontMenu:draw()
end end
function mainmenu_keypressed(key) function mainmenu_keypressed(key)
frontMenu:keypressed(key)
end end
function mainmenu_keyreleased(key) function mainmenu_keyreleased(key)
frontMenu:keyreleased(key)
end end
@ -126,6 +173,7 @@ end
-- GAMEOVER STATE -- GAMEOVER STATE
---------------------------------------- ----------------------------------------
function gameover_load () function gameover_load ()
mode = gameover
end end
@ -135,12 +183,18 @@ end
function gameover_draw () function gameover_draw ()
game_draw() game_draw()
camera:detach()
love.graphics.draw(love.graphics.newText(r_ttf,
"nice try!\n[enter to restart]\n[escape to exit]"), 200, 200, 0, 3, 3)
camera:attach()
end end
function gameover_keypressed(key) function gameover_keypressed(key)
if(key == "return" or key == "escape") then if (key == "return") then
mainmenu_load() camera:fade(.2, {0,0,0,1}, function() game_load() end)
elseif (key == "escape") then
camera:fade(.2, {0,0,0,1}, function() mainmenu_load() end)
end end
end end
@ -154,10 +208,10 @@ end
---------------------------------------- ----------------------------------------
function game_load() function game_load()
mode = game mode = game
camera:fade(.2, {0,0,0,0})
map = Map:new("maps/tutorial/1.lua") map = Map:new("maps/tutorial/1.lua")
camera = stalker() camera:setBounds(-200, -1000, map.width * map.tileWidth + 200,
camera:setFollowStyle('PLATFORMER') map.height * map.tileHeight + 2000)
camera:setFollowLerp(0.1)
-- bgm:stop() -- bgm:stop()
-- bgm = love.audio.newSource("art/music/game.ogg", "static") -- bgm = love.audio.newSource("art/music/game.ogg", "static")
@ -171,20 +225,13 @@ function game_update(dt)
map:update(dt) map:update(dt)
local x, y = player.monks[player.current].body:getPosition() local x, y = player.monks[player.current].body:getPosition()
camera:update(dt)
camera:follow(x, y) camera:follow(x, y)
end end
function game_draw () function game_draw ()
camera:attach()
map:draw() map:draw()
player:draw() player:draw()
banana:draw()
camera:detach()
camera:draw()
end end
@ -244,7 +291,6 @@ end
-- MONK player class -- MONK player class
---------------------------------------- ----------------------------------------
Monk = class('Monk') Monk = class('Monk')
world:addCollisionClass('Monk')
function Monk:initialize(x, y, count) function Monk:initialize(x, y, count)
self.monks = {} self.monks = {}
@ -288,6 +334,7 @@ function Monk:update(dt)
end end
end end
-- cleanup -- cleanup
for i=0,(self.last) do for i=0,(self.last) do
self.onGround[i] = 0 self.onGround[i] = 0
@ -360,9 +407,9 @@ function Monk:follow ()
local mx, my = thisMonk.body:getPosition() local mx, my = thisMonk.body:getPosition()
local dx, dy = thisMonk:getLinearVelocity() local dx, dy = thisMonk:getLinearVelocity()
if (mx < (x + 30)) then if (mx < (x + 30) and math.random(20) == 1) then
thisMonk:setLinearVelocity(newVel, dy) thisMonk:setLinearVelocity(newVel, dy)
elseif ((x - 30) < mx) then elseif ((x - 30) < mx and math.random(10) == 1) then
thisMonk:setLinearVelocity(-newVel, dy) thisMonk:setLinearVelocity(-newVel, dy)
end end
@ -394,6 +441,8 @@ function Monk:freeze ()
monk:setType('static') monk:setType('static')
monk:setCollisionClass('Platform') monk:setCollisionClass('Platform')
self:switch(self.current + 1) self:switch(self.current + 1)
else
gameover_load()
end end
end end
@ -433,16 +482,15 @@ end
-- BANNANA owo (win condition obj) -- BANNANA owo (win condition obj)
---------------------------------------- ----------------------------------------
Bananna = class('Bananna') Banana = class('Banana')
world:addCollisionClass('Bananna')
function Bananna:initialize(x, y) function Banana:initialize(x, y)
self.sprite = love.graphics.newImage("art/sprites/banana.png") self.sprite = love.graphics.newImage("art/sprites/banana.png")
self.collider = world:newRectangleCollider(x, y, 16, 16); self.collider = world:newRectangleCollider(x, y, 16, 16);
end end
function Bananna:draw() function Banana:draw()
local col = self.collider local col = self.collider
local x,y = col.body:getWorldPoints(col.shape:getPoints()) local x,y = col.body:getWorldPoints(col.shape:getPoints())
love.graphics.draw(self.sprite, x, y, col.body:getAngle(), 1, 1) love.graphics.draw(self.sprite, x, y, col.body:getAngle(), 1, 1)
@ -453,83 +501,103 @@ end
-- MAP used to store map data (ofc) -- MAP used to store map data (ofc)
---------------------------------------- ----------------------------------------
Map = class('Map') Map = class('Map')
world:addCollisionClass('Platform')
function Map:initialize(filepath) function Map:initialize(filepath)
self.ground = {} self.ground = {}
self.platforms = {} self.platforms = {}
self.tables = {}
self.objects = {} self.objects = {}
self.object_c = 0 self.outOfBounds = "teleport"
local maptable = dofile(filepath) local maptable = dofile(filepath)
self.width,self.height = maptable.width,maptable.height
self.tileWidth,self.tileHeight = maptable.tilewidth,maptable.tileheight
love.graphics.setBackgroundColor(146/255, 187/255, 203/255) love.graphics.setBackgroundColor(146/255, 187/255, 203/255)
local monkCount = 3 local monkCount = 3
local monkX,monkY = 100,100 local monkX,monkY = 100,100
local bananaX,bananaY = 200,200 world:destroy()
world = wind.newWorld(0, 400, true)
world:addCollisionClass('Monk')
world:addCollisionClass('Banana')
world:addCollisionClass('Platform')
for n,layer in pairs(maptable.layers) do for n,layer in pairs(maptable.layers) do
if not (layer.type == "objectgroup") then break; end if not (layer.type == "objectgroup") then break; end
for nn,object in pairs(layer.objects) do for nn,object in pairs(layer.objects) do
if (object.shape == "rectangle") then if (object.shape == "rectangle") then
self.object_c = self.object_c + 1 self.tables[object.id] =
local o_c = self.object_c
self.objects[o_c] =
world:newRectangleCollider(object.x, object.y, world:newRectangleCollider(object.x, object.y,
object.width, object.height) object.width, object.height)
self.objects[o_c]:setType('static') self.tables[object.id]:setType('static')
self.objects[o_c]:setCollisionClass('Platform') self.tables[object.id]:setCollisionClass('Platform')
elseif (object.shape == "polygon") then
local ox,oy = object.x,object.y
local vertices = {}
local vi = 1
for kk,verticePair in pairs(object.polygon) do
vertices[vi] = verticePair['x'] + ox
vertices[vi+1] = verticePair['y'] + oy
vi = vi + 2
end
self.tables[object.id] =
world:newPolygonCollider(vertices)
self.tables[object.id]:setType('static')
self.tables[object.id]:setCollisionClass('Platform')
elseif (object.shape == "text") then elseif (object.shape == "text") then
self.object_c = self.object_c + 1 self.tables[object.id] = object
self.objects[self.object_c] = object
elseif (object.shape == "point" and object.type == "spawn") then elseif (object.shape == "point" and object.type == "spawn") then
if not (object.properties == nil if not (object.properties == nil
and object.properties["count"] == nil) then or object.properties["count"] == nil) then
monkCount = object.properties["count"] monkCount = object.properties["count"]
end end
monkX,monkY = object.x,object.y monkX,monkY = object.x,object.y
elseif (object.shape == "point" and object.type == "banana") then elseif (object.shape == "point" and object.type == "banana") then
bananaX,bananaY = object.x,object.y self.objects[object.id] = Banana:new(object.x, object.y)
end end
end end
end end
player = Monk:new(monkX, monkY, monkCount) player = Monk:new(monkX, monkY, monkCount)
banana = Bananna:new(bananaX, bananaY)
end end
function Map:update(dt) function Map:update(dt)
local heightMax = self.height * self.tileHeight + 100
local widthMax = self.width * self.tileWidth + 200
for i=player.current,player.last do
local x,y = player.monks[i].body:getPosition()
if (math.abs(x) > widthMax or y > heightMax
and self.outOfBounds == "die") then
player:freeze()
elseif (math.abs(x) > widthMax or y > heightMax
and self.outOfBounds == "teleport") then
player.monks[i].body:setPosition(200,0)
end
end
end end
function Map:draw() function Map:draw()
for k,object in pairs(self.objects) do for k,table in pairs(self.tables) do
if (object.type == "Rectangle") then if (table.type == "Rectangle" or table.type == "Polygon") then
love.graphics.polygon('fill', love.graphics.polygon('fill',
object.body:getWorldPoints(object.shape:getPoints())) table.body:getWorldPoints(table.shape:getPoints()))
elseif (object.shape == "text") then
love.graphics.draw(love.graphics.newText(a_ttf, object.text), elseif (table.shape == "text") then
object.x, object.y) love.graphics.draw(love.graphics.newText(a_ttf, table.text),
table.x, table.y, 0, 2, 2)
end end
end end
for k,object in pairs(self.objects) do
object:draw()
end end
-- TEXTBOX gods forgive me
----------------------------------------
Textbox = class("Textbox")
function Textbox:initialize(x, y, string)
self.x,self.y = x,y
self.text = love.graphics.newText(a_ttf, string)
end
function Textbox:draw()
love.graphics.draw(self.text, self.x, self.y)
end end
@ -539,56 +607,70 @@ end
Menu = class("Menu") Menu = class("Menu")
function Menu:initialize(x, y, offset_x, offset_y, scale, menuItems) function Menu:initialize(x, y, offset_x, offset_y, scale, menuItems)
self.x = x; self.y = y self.x,self.y = x,y
self.offset_x = offset_x; self.offset_y = offset_y self.offset_x,self.offset_y = offset_x,offset_y
self.scale = scale
self.options = menuItems self.options = menuItems
self.selected = 1 self.selected = 1
self.enter = false self.scale = scale
self.up = false
self.down = false self.keys = {}
self.keys['up'] = false
self.keys['down'] = false
self.keys['enter'] = false
self.ttf = r_ttf
end end
function Menu:draw () function Menu:draw ()
-- love.graphics.draw(love.graphics.newText(a_ttf, ">>"), for i=1,table.maxn(self.options) do
-- self.x - self.offset_x, this_y, 0, local this_y = self.y + (self.offset_y * i)
-- self.scale, self.scale)
love.graphics.draw(love.graphics.newText(a_ttf, ">>"), love.graphics.draw(self.options[i][1],
self.x - self.offset_x, this_y, 0, self.x, this_y, 0, self.scale, self.scale)
self.scale, self.scale) if (i == self.selected) then
love.graphics.draw(love.graphics.newText(self.ttf, ">>"),
self.x - self.offset_x, this_y, 0, self.scale, self.scale)
end end
end
end
function Menu:keypressed(key) function Menu:keypressed(key)
maxn = table.maxn(self.options) maxn = table.maxn(self.options)
if(key == "return" and self.enter == false) then if (key == "return" or key == "space") then
self.enter = true self.keys['enter'] = true
if(self.options[self.selected][2]) then if(self.options[self.selected][2]) then
self.options[self.selected][2]() self.options[self.selected][2]()
end end
elseif(key == "up" and self.selected > 1 and self.up == false) then
self.up = true elseif (key == "up" and self.selected > 1
and self.keys['up'] == false) then
self.keys['up'] = true
self.selected = self.selected - 1 self.selected = self.selected - 1
elseif(key == "up" and self.up == false) then elseif (key == "up" and self.keys['up'] == false) then
self.up = true self.keys['up'] = true
self.selected = maxn self.selected = maxn
elseif(key == "down" and self.selected < maxn and self.down == false) then
self.down = true elseif (key == "down" and self.selected < maxn
and self.keys['down'] == false) then
self.keys['down'] = true
self.selected = self.selected + 1 self.selected = self.selected + 1
elseif(key == "down" and self.down == false) then elseif (key == "down" and self.keys['down'] == false) then
self.down = true self.keys['down'] = true
self.selected = 1 self.selected = 1
end end
end end
function Menu:keyreleased(key) function Menu:keyreleased(key)
if(key == "return") then if (key == "return" or key == "space") then
self.enter = false self.keys['enter'] = false
elseif (key == "up") then elseif (key == "up") then
self.up = false self.keys['up'] = false
elseif (key == "down") then elseif (key == "down") then
self.down = false self.keys['down'] = false
end end
end end

154
maps/menu.lua Normal file
View File

@ -0,0 +1,154 @@
return {
version = "1.4",
luaversion = "5.1",
tiledversion = "1.4.3",
orientation = "orthogonal",
renderorder = "right-down",
width = 50,
height = 50,
tilewidth = 16,
tileheight = 16,
nextlayerid = 4,
nextobjectid = 20,
properties = {},
tilesets = {},
layers = {
{
type = "objectgroup",
draworder = "topdown",
id = 2,
name = "Object Layer 1",
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
properties = {},
objects = {
{
id = 2,
name = "",
type = "",
shape = "rectangle",
x = 38.932,
y = 306.814,
width = 14.6488,
height = 242.519,
rotation = 0,
visible = true,
properties = {}
},
{
id = 4,
name = "",
type = "",
shape = "rectangle",
x = 55.2085,
y = 537.94,
width = 524.102,
height = 11.3935,
rotation = 0,
visible = true,
properties = {}
},
{
id = 6,
name = "",
type = "",
shape = "rectangle",
x = 581.752,
y = 306,
width = 14.6488,
height = 242.52,
rotation = 0,
visible = true,
properties = {}
}
}
},
{
type = "objectgroup",
draworder = "topdown",
id = 3,
name = "Object Layer 2",
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
properties = {},
objects = {
{
id = 9,
name = "",
type = "spawn",
shape = "point",
x = 378.031,
y = 420.905,
width = 0,
height = 0,
rotation = 0,
visible = true,
properties = {
["count"] = 10
}
},
{
id = 16,
name = "",
type = "",
shape = "polygon",
x = 800,
y = 524.242,
width = 0,
height = 0,
rotation = 0,
visible = true,
polygon = {
{ x = 0, y = 0 },
{ x = -360.606, y = 269.697 },
{ x = -6.06061, y = 266.667 }
},
properties = {}
},
{
id = 17,
name = "",
type = "",
shape = "rectangle",
x = 46.6667,
y = 785.333,
width = 66.6667,
height = 14.6667,
rotation = 0,
visible = true,
properties = {}
},
{
id = 18,
name = "",
type = "",
shape = "rectangle",
x = 177.333,
y = 784,
width = 105.333,
height = 16,
rotation = 0,
visible = true,
properties = {}
},
{
id = 19,
name = "",
type = "",
shape = "rectangle",
x = 346.667,
y = 782.667,
width = 54.6667,
height = 16,
rotation = 0,
visible = true,
properties = {}
}
}
}
}
}

25
maps/menu.tmx Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="50" height="50" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="20">
<editorsettings>
<export target="menu.lua" format="lua"/>
</editorsettings>
<objectgroup id="2" name="Object Layer 1">
<object id="2" x="38.932" y="306.814" width="14.6488" height="242.519"/>
<object id="4" x="55.2085" y="537.94" width="524.102" height="11.3935"/>
<object id="6" x="581.752" y="306" width="14.6488" height="242.52"/>
</objectgroup>
<objectgroup id="3" name="Object Layer 2">
<object id="9" type="spawn" x="378.031" y="420.905">
<properties>
<property name="count" type="int" value="10"/>
</properties>
<point/>
</object>
<object id="16" x="800" y="524.242">
<polygon points="0,0 -360.606,269.697 -6.06061,266.667"/>
</object>
<object id="17" x="46.6667" y="785.333" width="66.6667" height="14.6667"/>
<object id="18" x="177.333" y="784" width="105.333" height="16"/>
<object id="19" x="346.667" y="782.667" width="54.6667" height="16"/>
</objectgroup>
</map>