1
0
Fork 0

Draw vehicle sprites and set up colliders in Tiled.

This commit is contained in:
Gonzalo Delgado 2023-08-30 08:29:09 -03:00
parent 9840219e98
commit 8299a7c300
3 changed files with 234 additions and 51 deletions

View File

@ -63,6 +63,7 @@ function love.load()
game.scaleH = desktop_height / game.SCREEN_HEIGHT
for i, layer in ipairs(game.map.layers) do
if layer.objects then
layer.visible = false
for j, mapObj in ipairs(layer.objects) do
mapObj.visible = false
local typekey = mapObj.type.."s"
@ -75,7 +76,12 @@ function love.load()
w=mapObj.width,
h=mapObj.height,
type=mapObj.type,
gid=mapObj.gid,
}
if mapObj.type == "vehicle" then
local tile = game.map.tiles[mapObj.gid] or game.map:setFlippedGID(mapObj.gid)
entity.collider = tile.objectGroup.objects[1]
end
if entity.type == "floatdev" then
entity.speed = mapObj.properties.speed or game.FLOATDEV_SPEED
end
@ -153,11 +159,12 @@ function love.update(dt)
player.turtlePos.y = player.turtlePos.y + player.float_speed*dt
end
for i, vehicle in ipairs(game.entities.vehicles) do
if rectanglesIntersect(playerRect, vehicle) then
local colliderRect = {x=vehicle.x + vehicle.collider.x, y=vehicle.y + vehicle.collider.y, w=vehicle.collider.width, h=vehicle.collider.height}
if rectanglesIntersect(playerRect, colliderRect) then
player.dead = true
player.lives = player.lives - 1
end
if rectanglesIntersect(turtleRect, vehicle) then
if rectanglesIntersect(turtleRect, colliderRect) then
player.turtleHit = true
end
@ -224,9 +231,14 @@ function love.draw()
love.graphics.line(0, t2p(i), t2p(game.PLAYFIELD_WIDTH), t2p(i))
end
end
love.graphics.setColor(0.9, 0.2, 0)
for i, vehicle in ipairs(game.entities.vehicles) do
love.graphics.rectangle("fill", vehicle.x, vehicle.y, vehicle.w, vehicle.h)
local tile = game.map.tiles[vehicle.gid] or game.map:setFlippedGID(vehicle.gid)
local tileset = game.map.tilesets[tile.tileset]
local image = tileset.image
love.graphics.setColor(1, 1, 1)
love.graphics.draw(image, tile.quad, vehicle.x, vehicle.y)
love.graphics.setColor(0.9, 0.1, 0)
-- love.graphics.rectangle("line", vehicle.x + vehicle.collider.x, vehicle.y + vehicle.collider.y, vehicle.collider.width, vehicle.collider.height)
end
love.graphics.setColor(0.8, 0.7, 0)
for i, floatdev in ipairs(game.entities.floatdevs) do

235
map.lua
View File

@ -8,8 +8,8 @@ return {
height = 10,
tilewidth = 16,
tileheight = 16,
nextlayerid = 4,
nextobjectid = 26,
nextlayerid = 6,
nextobjectid = 35,
properties = {},
tilesets = {
{
@ -38,6 +38,127 @@ return {
wangsets = {},
tilecount = 10,
tiles = {}
},
{
name = "sprites",
firstgid = 11,
tilewidth = 16,
tileheight = 32,
spacing = 0,
margin = 0,
columns = 10,
image = "sprites.png",
imagewidth = 160,
imageheight = 32,
transparentcolor = "#ff00ff",
objectalignment = "unspecified",
tileoffset = {
x = 0,
y = 0
},
grid = {
orientation = "orthogonal",
width = 16,
height = 32
},
properties = {},
wangsets = {},
tilecount = 10,
tiles = {
{
id = 0,
objectGroup = {
type = "objectgroup",
draworder = "index",
id = 2,
name = "",
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
parallaxx = 1,
parallaxy = 1,
properties = {},
objects = {
{
id = 1,
name = "",
type = "",
shape = "rectangle",
x = 1,
y = 1,
width = 14,
height = 24,
rotation = 0,
visible = true,
properties = {}
}
}
}
},
{
id = 1,
objectGroup = {
type = "objectgroup",
draworder = "index",
id = 2,
name = "",
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
parallaxx = 1,
parallaxy = 1,
properties = {},
objects = {
{
id = 1,
name = "",
type = "",
shape = "rectangle",
x = 1,
y = 2,
width = 14,
height = 24,
rotation = 0,
visible = true,
properties = {}
}
}
}
},
{
id = 2,
objectGroup = {
type = "objectgroup",
draworder = "index",
id = 2,
name = "",
visible = true,
opacity = 1,
offsetx = 0,
offsety = 0,
parallaxx = 1,
parallaxy = 1,
properties = {},
objects = {
{
id = 1,
name = "",
type = "",
shape = "rectangle",
x = 1,
y = 1,
width = 14,
height = 24,
rotation = 0,
visible = true,
properties = {}
}
}
}
}
}
}
},
layers = {
@ -122,22 +243,8 @@ return {
offsety = 0,
parallaxx = 1,
parallaxy = 1,
tintcolor = { 170, 0, 0 },
properties = {},
objects = {
{
id = 1,
name = "",
type = "vehicle",
shape = "rectangle",
x = 18,
y = 0,
width = 12,
height = 16,
rotation = 0,
visible = true,
properties = {}
},
{
id = 8,
name = "",
@ -239,19 +346,6 @@ return {
["speed"] = -18
}
},
{
id = 2,
name = "",
type = "vehicle",
shape = "rectangle",
x = 18,
y = 64,
width = 12,
height = 16,
rotation = 0,
visible = true,
properties = {}
},
{
id = 7,
name = "",
@ -353,19 +447,6 @@ return {
["speed"] = -18
}
},
{
id = 3,
name = "",
type = "vehicle",
shape = "rectangle",
x = 18,
y = 128,
width = 12,
height = 16,
rotation = 0,
visible = true,
properties = {}
},
{
id = 6,
name = "",
@ -436,6 +517,76 @@ return {
properties = {
["speed"] = -10
}
},
{
id = 30,
name = "",
type = "vehicle",
shape = "rectangle",
x = 44,
y = 152,
width = 16,
height = 32,
rotation = 0,
gid = 11,
visible = true,
properties = {}
},
{
id = 31,
name = "",
type = "vehicle",
shape = "rectangle",
x = 44,
y = 64,
width = 16,
height = 32,
rotation = 0,
gid = 13,
visible = true,
properties = {}
},
{
id = 32,
name = "",
type = "vehicle",
shape = "rectangle",
x = 16,
y = 32,
width = 16,
height = 32,
rotation = 0,
gid = 13,
visible = true,
properties = {}
},
{
id = 33,
name = "",
type = "vehicle",
shape = "rectangle",
x = 16,
y = 88,
width = 16,
height = 32,
rotation = 0,
gid = 12,
visible = true,
properties = {}
},
{
id = 34,
name = "",
type = "vehicle",
shape = "rectangle",
x = 16,
y = 152,
width = 16,
height = 32,
rotation = 0,
gid = 11,
visible = true,
properties = {}
}
}
}

30
map.tmx
View File

@ -1,11 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.8" tiledversion="1.8.6" orientation="orthogonal" renderorder="right-down" width="20" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="26">
<map version="1.8" tiledversion="1.8.6" orientation="orthogonal" renderorder="right-down" width="20" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="6" nextobjectid="35">
<editorsettings>
<export target="map.lua" format="lua"/>
</editorsettings>
<tileset firstgid="1" name="tiles" tilewidth="16" tileheight="16" tilecount="10" columns="10">
<image source="tiles.png" trans="ff00ff" width="160" height="16"/>
</tileset>
<tileset firstgid="11" name="sprites" tilewidth="16" tileheight="32" tilecount="10" columns="10">
<image source="sprites.png" trans="ff00ff" width="160" height="32"/>
<tile id="0">
<objectgroup draworder="index" id="2">
<object id="1" x="1" y="1" width="14" height="24"/>
</objectgroup>
</tile>
<tile id="1">
<objectgroup draworder="index" id="2">
<object id="1" x="1" y="2" width="14" height="24"/>
</objectgroup>
</tile>
<tile id="2">
<objectgroup draworder="index" id="2">
<object id="1" x="1" y="1" width="14" height="24"/>
</objectgroup>
</tile>
</tileset>
<layer id="1" name="Capa de patrones 1" width="20" height="10">
<data encoding="csv">
1,2,3,4,1,5,5,5,1,5,5,5,5,1,2,3,7,3,4,6,
@ -24,8 +42,7 @@
<object id="4" type="water" x="80" y="0" width="48" height="160"/>
<object id="5" type="water" x="144" y="0" width="64" height="160"/>
</objectgroup>
<objectgroup id="2" name="entities" tintcolor="#aa0000">
<object id="1" type="vehicle" x="18" y="0" width="12" height="16"/>
<objectgroup id="2" name="entities">
<object id="8" type="floatdev" x="82" y="-8" width="12" height="32"/>
<object id="22" type="floatdev" x="146" y="-8" width="12" height="32"/>
<object id="25" type="floatdev" x="194" y="-8" width="12" height="32">
@ -53,7 +70,6 @@
<property name="speed" type="int" value="-18"/>
</properties>
</object>
<object id="2" type="vehicle" x="18" y="64" width="12" height="16"/>
<object id="7" type="floatdev" x="82" y="56" width="12" height="32"/>
<object id="19" type="floatdev" x="146" y="56" width="12" height="32"/>
<object id="24" type="floatdev" x="194" y="56" width="12" height="32">
@ -81,7 +97,6 @@
<property name="speed" type="int" value="-18"/>
</properties>
</object>
<object id="3" type="vehicle" x="18" y="128" width="12" height="16"/>
<object id="6" type="floatdev" x="82" y="120" width="12" height="32"/>
<object id="17" type="floatdev" x="146" y="120" width="12" height="32"/>
<object id="23" type="floatdev" x="194" y="120" width="12" height="32">
@ -99,5 +114,10 @@
<property name="speed" type="int" value="-10"/>
</properties>
</object>
<object id="30" type="vehicle" gid="11" x="44" y="152" width="16" height="32"/>
<object id="31" type="vehicle" gid="13" x="44" y="64" width="16" height="32"/>
<object id="32" type="vehicle" gid="13" x="16" y="32" width="16" height="32"/>
<object id="33" type="vehicle" gid="12" x="16" y="88" width="16" height="32"/>
<object id="34" type="vehicle" gid="11" x="16" y="152" width="16" height="32"/>
</objectgroup>
</map>