coins and tilemap stuff for coins

This commit is contained in:
Tyrel Souza 2018-01-18 00:19:17 -05:00
parent 8a2b76567c
commit 5332d5b1db
4 changed files with 726 additions and 10 deletions

View File

@ -4,9 +4,26 @@ function spawnCoin(x, y)
local coin = {} local coin = {}
coin.x = x coin.x = x
coin.y = y coin.y = y
coin.collected = false
coin.grid = anim8.newGrid(41, 42, 123, 126) coin.grid = anim8.newGrid(41, 42, 123, 126)
coin.animation = anim8.newAnimation(coin.grid('1-3', 1, '1-3', 2, '1-2', 3), 0.1) coin.animation = anim8.newAnimation(coin.grid('1-3', 1, '1-3', 2, '1-2', 3), 0.1)
table.insert(coins, coin) table.insert(coins, coin)
end end
function coinUpdate(dt)
for i, c in ipairs(coins) do
if distanceBetween(c.x, c.y, player.body:getX(), player.body:getY()) <= 50 then
c.collected = true
end
end
for i=#coins, 1, -1 do
local c = coins[i]
if c.collected == true then
table.remove(coins, i)
end
end
end

View File

@ -16,8 +16,15 @@ function love.load()
platforms = {} platforms = {}
spawnPlatform(50, 500, 300, 30) gameMap = sti("maps/map.lua", { "box2d" })
spawnCoin(200, 100) for i, obj in pairs(gameMap.layers['platforms'].objects) do
spawnPlatform(obj.x, obj.y, obj.width, obj.height)
end
for i, obj in pairs(gameMap.layers['coins'].objects) do
spawnCoin(obj.x, obj.y)
end
DIRECTION_LEFT = -1 DIRECTION_LEFT = -1
DIRECTION_RIGHT = 1 DIRECTION_RIGHT = 1
end end
@ -25,6 +32,8 @@ end
function love.update(dt) function love.update(dt)
myWorld:update(dt) myWorld:update(dt)
playerUpdate(dt) playerUpdate(dt)
gameMap:update(dt)
coinUpdate(dt)
for i,c in ipairs(coins) do for i,c in ipairs(coins) do
c.animation:update(dt) c.animation:update(dt)
@ -32,6 +41,8 @@ function love.update(dt)
end end
function love.draw() function love.draw()
gameMap:drawLayer(gameMap.layers['Tile Layer 1'])
love.graphics.draw( love.graphics.draw(
player.sprite, player.sprite,
player.body:getX(), player.body:getX(),
@ -39,17 +50,14 @@ function love.draw()
nil, player.direction, 1, nil, player.direction, 1,
sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2) sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2)
for i, p in ipairs(platforms) do
love.graphics.rectangle("fill", p.body:getX(), p.body:getY(), p.width, p.height)
end
for i, c in ipairs(coins) do for i, c in ipairs(coins) do
c.animation:draw(sprites.coin_sheet, c.x, c.y) c.animation:draw(sprites.coin_sheet, c.x, c.y, nil, nil, nil, 20.5, 21)
end end
end end
function love.keypressed(key, scancode, isrepeat) function love.keypressed(key, scancode, isrepeat)
if key == "space" and player.grounded then if key == "space" and player.grounded then
player.body:applyLinearImpulse(0, -2500) player.body:applyLinearImpulse(0, -2800)
end end
end end
@ -71,3 +79,13 @@ end
function endContact(a, b, coll) function endContact(a, b, coll)
player.grounded = false player.grounded = false
end end
function distanceBetweenObjects(obj1, obj2)
return distanceBetween(obj1.x, obj1.y, obj2.x, obj2.y)
end
function distanceBetween(x1, y1, x2, y2)
return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
end

631
maps/map.lua Normal file
View File

@ -0,0 +1,631 @@
return {
version = "1.1",
luaversion = "5.1",
tiledversion = "1.1.1",
orientation = "orthogonal",
renderorder = "right-down",
width = 100,
height = 10,
tilewidth = 70,
tileheight = 70,
nextobjectid = 64,
properties = {},
tilesets = {
{
name = "map",
firstgid = 1,
tilewidth = 70,
tileheight = 70,
spacing = 2,
margin = 0,
image = "tiles_spritesheet.png",
imagewidth = 914,
imageheight = 936,
transparentcolor = "#ff05fa",
tileoffset = {
x = 0,
y = 0
},
grid = {
orientation = "orthogonal",
width = 70,
height = 70
},
properties = {},
terrains = {},
tilecount = 156,
tiles = {}
}
},
layers = {
{
type = "tilelayer",
name = "Tile Layer 1",
x = 0,
y = 0,
width = 100,
height = 10,
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
properties = {},
encoding = "base64",
compression = "zlib",
data = "eJztkzsOACAIQ525/4GdHSQa+dSmLyGMbWgZQ3Rj3QbEgvLAQnlgEZWHOROJp5OhVw2qf9tsJl77lN3H3/t98rssf9xF930jtG57UtkVVF/MsNx3Ahk9A98="
},
{
type = "tilelayer",
name = "hidden",
x = 0,
y = 0,
width = 100,
height = 10,
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
properties = {},
encoding = "base64",
compression = "zlib",
data = "eJxjYBgFo4A2QGqgHTAIgBQONq3sIZYeBZQDaoXtcI8TWueBoWLmKCAfDIf4oFYZjC0/0aueoacdo4B4MBofo2AUDD8AACv4AqU="
},
{
type = "objectgroup",
name = "platforms",
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
draworder = "topdown",
properties = {},
objects = {
{
id = 4,
name = "",
type = "",
shape = "rectangle",
x = 71.3333,
y = 560.667,
width = 419.333,
height = 68.6667,
rotation = 0,
visible = true,
properties = {}
},
{
id = 5,
name = "",
type = "",
shape = "rectangle",
x = 561.333,
y = 421.333,
width = 420,
height = 67.3333,
rotation = 0,
visible = true,
properties = {}
},
{
id = 6,
name = "",
type = "",
shape = "rectangle",
x = 1051.33,
y = 280.667,
width = 488,
height = 69.3333,
rotation = 0,
visible = true,
properties = {}
},
{
id = 8,
name = "",
type = "",
shape = "rectangle",
x = 1259.67,
y = 560,
width = 910.33,
height = 69.3333,
rotation = 0,
visible = true,
properties = {}
},
{
id = 10,
name = "",
type = "",
shape = "rectangle",
x = 2310,
y = 420,
width = 70,
height = 69.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 11,
name = "",
type = "",
shape = "rectangle",
x = 2450,
y = 350.5,
width = 70.5,
height = 69.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 12,
name = "",
type = "",
shape = "rectangle",
x = 2591,
y = 350.5,
width = 69,
height = 68.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 13,
name = "",
type = "",
shape = "rectangle",
x = 2730,
y = 350,
width = 69.5,
height = 70,
rotation = 0,
visible = true,
properties = {}
},
{
id = 15,
name = "",
type = "",
shape = "rectangle",
x = 2870,
y = 281,
width = 631,
height = 68,
rotation = 0,
visible = true,
properties = {}
},
{
id = 16,
name = "",
type = "",
shape = "rectangle",
x = 3500,
y = 628,
width = 491,
height = 71,
rotation = 0,
visible = true,
properties = {}
},
{
id = 17,
name = "",
type = "",
shape = "rectangle",
x = 3990,
y = 560,
width = 841,
height = 71,
rotation = 0,
visible = true,
properties = {}
},
{
id = 19,
name = "",
type = "",
shape = "rectangle",
x = 4830,
y = 491,
width = 772,
height = 69,
rotation = 0,
visible = true,
properties = {}
},
{
id = 20,
name = "",
type = "",
shape = "rectangle",
x = 5603,
y = 420,
width = 836,
height = 70,
rotation = 0,
visible = true,
properties = {}
},
{
id = 24,
name = "",
type = "",
shape = "rectangle",
x = 6440.5,
y = 350.5,
width = 420.5,
height = 69.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 25,
name = "",
type = "",
shape = "rectangle",
x = 6789.5,
y = 0,
width = 71,
height = 350.5,
rotation = 0,
visible = true,
properties = {}
}
}
},
{
type = "objectgroup",
name = "coins",
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
draworder = "topdown",
properties = {},
objects = {
{
id = 33,
name = "",
type = "",
shape = "rectangle",
x = 211,
y = 421,
width = 69,
height = 68.6667,
rotation = 0,
visible = true,
properties = {}
},
{
id = 36,
name = "",
type = "",
shape = "rectangle",
x = 350.667,
y = 420.667,
width = 70,
height = 69.3333,
rotation = 0,
visible = true,
properties = {}
},
{
id = 37,
name = "",
type = "",
shape = "rectangle",
x = 630.667,
y = 280.667,
width = 69,
height = 69,
rotation = 0,
visible = true,
properties = {}
},
{
id = 38,
name = "",
type = "",
shape = "rectangle",
x = 840,
y = 281,
width = 69.3333,
height = 69,
rotation = 0,
visible = true,
properties = {}
},
{
id = 39,
name = "",
type = "",
shape = "rectangle",
x = 1121.33,
y = 141.333,
width = 69,
height = 68.3333,
rotation = 0,
visible = true,
properties = {}
},
{
id = 40,
name = "",
type = "",
shape = "rectangle",
x = 1331,
y = 141,
width = 70.3333,
height = 68.6667,
rotation = 0,
visible = true,
properties = {}
},
{
id = 42,
name = "",
type = "",
shape = "rectangle",
x = 1470.5,
y = 421.5,
width = 70,
height = 68.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 43,
name = "",
type = "",
shape = "rectangle",
x = 1681,
y = 420.5,
width = 69,
height = 68.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 44,
name = "",
type = "",
shape = "rectangle",
x = 1821,
y = 422,
width = 70.5,
height = 68.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 45,
name = "",
type = "",
shape = "rectangle",
x = 2031,
y = 420.5,
width = 70,
height = 71,
rotation = 0,
visible = true,
properties = {}
},
{
id = 46,
name = "",
type = "",
shape = "rectangle",
x = 2311,
y = 280.5,
width = 70,
height = 70,
rotation = 0,
visible = true,
properties = {}
},
{
id = 47,
name = "",
type = "",
shape = "rectangle",
x = 2450.5,
y = 210.5,
width = 70,
height = 70.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 48,
name = "",
type = "",
shape = "rectangle",
x = 2591.5,
y = 209.5,
width = 70,
height = 71,
rotation = 0,
visible = true,
properties = {}
},
{
id = 49,
name = "",
type = "",
shape = "rectangle",
x = 2730.5,
y = 211,
width = 69.5,
height = 70,
rotation = 0,
visible = true,
properties = {}
},
{
id = 50,
name = "",
type = "",
shape = "rectangle",
x = 2940.5,
y = 140.5,
width = 68.5,
height = 69.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 51,
name = "",
type = "",
shape = "rectangle",
x = 3080,
y = 140.5,
width = 70,
height = 69,
rotation = 0,
visible = true,
properties = {}
},
{
id = 52,
name = "",
type = "",
shape = "rectangle",
x = 3220.5,
y = 140,
width = 70.5,
height = 70.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 53,
name = "",
type = "",
shape = "rectangle",
x = 3361.5,
y = 140,
width = 68,
height = 70.5,
rotation = 0,
visible = true,
properties = {}
},
{
id = 55,
name = "",
type = "",
shape = "rectangle",
x = 3502,
y = 142,
width = 64,
height = 68,
rotation = 0,
visible = true,
properties = {}
},
{
id = 56,
name = "",
type = "",
shape = "rectangle",
x = 3571,
y = 72,
width = 67,
height = 68,
rotation = 0,
visible = true,
properties = {}
},
{
id = 57,
name = "",
type = "",
shape = "rectangle",
x = 3640,
y = 144,
width = 70,
height = 63,
rotation = 0,
visible = true,
properties = {}
},
{
id = 58,
name = "",
type = "",
shape = "rectangle",
x = 3709,
y = 210,
width = 74,
height = 73,
rotation = 0,
visible = true,
properties = {}
},
{
id = 60,
name = "",
type = "",
shape = "rectangle",
x = 3781,
y = 281,
width = 67,
height = 69,
rotation = 0,
visible = true,
properties = {}
},
{
id = 61,
name = "",
type = "",
shape = "rectangle",
x = 3782,
y = 353,
width = 67,
height = 64,
rotation = 0,
visible = true,
properties = {}
},
{
id = 62,
name = "",
type = "",
shape = "rectangle",
x = 3783,
y = 419,
width = 67,
height = 71,
rotation = 0,
visible = true,
properties = {}
},
{
id = 63,
name = "",
type = "",
shape = "rectangle",
x = 3783,
y = 493,
width = 66,
height = 65,
rotation = 0,
visible = true,
properties = {}
}
}
}
}
}

View File

@ -1,11 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="100" height="10" tilewidth="70" tileheight="70" nextobjectid="1"> <map version="1.0" tiledversion="1.1.1" orientation="orthogonal" renderorder="right-down" width="100" height="10" tilewidth="70" tileheight="70" infinite="0" nextobjectid="64">
<tileset firstgid="1" name="map" tilewidth="70" tileheight="70" spacing="2"> <tileset firstgid="1" name="map" tilewidth="70" tileheight="70" spacing="2" tilecount="156" columns="12">
<image source="tiles_spritesheet.png" trans="ff05fa" width="914" height="936"/> <image source="tiles_spritesheet.png" trans="ff05fa" width="914" height="936"/>
</tileset> </tileset>
<layer name="Tile Layer 1" width="100" height="10"> <layer name="Tile Layer 1" width="100" height="10">
<data encoding="base64" compression="zlib"> <data encoding="base64" compression="zlib">
eJzt1LENACAIBEDGZf8JLCysNBZKNLlLCOU38BEAUCsXU5VzI48uJ5uh6v7hVSd+YKfjdT6/aaPnEkk= eJztkzsOACAIQ525/4GdHSQa+dSmLyGMbWgZQ3Rj3QbEgvLAQnlgEZWHOROJp5OhVw2qf9tsJl77lN3H3/t98rssf9xF930jtG57UtkVVF/MsNx3Ahk9A98=
</data> </data>
</layer> </layer>
<layer name="hidden" width="100" height="10">
<data encoding="base64" compression="zlib">
eJxjYBgFo4A2QGqgHTAIgBQONq3sIZYeBZQDaoXtcI8TWueBoWLmKCAfDIf4oFYZjC0/0aueoacdo4B4MBofo2AUDD8AACv4AqU=
</data>
</layer>
<objectgroup name="platforms">
<object id="4" x="71.3333" y="560.667" width="419.333" height="68.6667"/>
<object id="5" x="561.333" y="421.333" width="420" height="67.3333"/>
<object id="6" x="1051.33" y="280.667" width="488" height="69.3333"/>
<object id="8" x="1259.67" y="560" width="910.33" height="69.3333"/>
<object id="10" x="2310" y="420" width="70" height="69.5"/>
<object id="11" x="2450" y="350.5" width="70.5" height="69.5"/>
<object id="12" x="2591" y="350.5" width="69" height="68.5"/>
<object id="13" x="2730" y="350" width="69.5" height="70"/>
<object id="15" x="2870" y="281" width="631" height="68"/>
<object id="16" x="3500" y="628" width="491" height="71"/>
<object id="17" x="3990" y="560" width="841" height="71"/>
<object id="19" x="4830" y="491" width="772" height="69"/>
<object id="20" x="5603" y="420" width="836" height="70"/>
<object id="24" x="6440.5" y="350.5" width="420.5" height="69.5"/>
<object id="25" x="6789.5" y="0" width="71" height="350.5"/>
</objectgroup>
<objectgroup name="coins">
<object id="33" x="211" y="421" width="69" height="68.6667"/>
<object id="36" x="350.667" y="420.667" width="70" height="69.3333"/>
<object id="37" x="630.667" y="280.667" width="69" height="69"/>
<object id="38" x="840" y="281" width="69.3333" height="69"/>
<object id="39" x="1121.33" y="141.333" width="69" height="68.3333"/>
<object id="40" x="1331" y="141" width="70.3333" height="68.6667"/>
<object id="42" x="1470.5" y="421.5" width="70" height="68.5"/>
<object id="43" x="1681" y="420.5" width="69" height="68.5"/>
<object id="44" x="1821" y="422" width="70.5" height="68.5"/>
<object id="45" x="2031" y="420.5" width="70" height="71"/>
<object id="46" x="2311" y="280.5" width="70" height="70"/>
<object id="47" x="2450.5" y="210.5" width="70" height="70.5"/>
<object id="48" x="2591.5" y="209.5" width="70" height="71"/>
<object id="49" x="2730.5" y="211" width="69.5" height="70"/>
<object id="50" x="2940.5" y="140.5" width="68.5" height="69.5"/>
<object id="51" x="3080" y="140.5" width="70" height="69"/>
<object id="52" x="3220.5" y="140" width="70.5" height="70.5"/>
<object id="53" x="3361.5" y="140" width="68" height="70.5"/>
<object id="55" x="3502" y="142" width="64" height="68"/>
<object id="56" x="3571" y="72" width="67" height="68"/>
<object id="57" x="3640" y="144" width="70" height="63"/>
<object id="58" x="3709" y="210" width="74" height="73"/>
<object id="60" x="3781" y="281" width="67" height="69"/>
<object id="61" x="3782" y="353" width="67" height="64"/>
<object id="62" x="3783" y="419" width="67" height="71"/>
<object id="63" x="3783" y="493" width="66" height="65"/>
</objectgroup>
</map> </map>