Skip to content

Commit

Permalink
Add spikes.
Browse files Browse the repository at this point in the history
  • Loading branch information
slemonide committed Dec 3, 2017
1 parent b688a59 commit c33e747
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 20 deletions.
42 changes: 30 additions & 12 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ end
-- Draw
------------------------

draw = {}
draw.deathMessage = {}
draw.deathMessage.x = 0.5 + math.random()
draw.deathMessage.y = 0.5 + math.random()

-- Put origin to the center of the window
local function updateOrigin()
love.graphics.origin()
Expand All @@ -40,21 +45,29 @@ local function updateOrigin()
end

function love.draw()
updateOrigin();

-- need this to render textures properly
love.graphics.setColor(255, 255, 255)
if (not player.dead) then
updateOrigin();
-- need this to render textures properly
love.graphics.setColor(255, 255, 255)

-- Draw nodes
for _, node in ipairs(nodes) do
if (math.abs(node.x - player.x) * SIZE < love.graphics.getWidth()
and math.abs(node.y - player.y) * SIZE < love.graphics.getHeight()) then
love.graphics.draw(node.texture, node.x * SIZE, node.y * SIZE)
-- Draw nodes
for _, node in ipairs(nodes) do
if (math.abs(node.x - player.x) * SIZE < love.graphics.getWidth()
and math.abs(node.y - player.y) * SIZE < love.graphics.getHeight()) then
love.graphics.draw(node.texture, node.x * SIZE, node.y * SIZE)
end
end
end

-- Draw player
love.graphics.draw(textures.player, player.x * SIZE, player.y * SIZE)
-- Draw player
love.graphics.draw(textures.player, player.x * SIZE, player.y * SIZE)
else
love.graphics.setColor(255, 0, 0, 255)
love.graphics.print(
"You died.",
love.graphics.getWidth() / 2 * draw.deathMessage.x,
love.graphics.getHeight() / 2 * draw.deathMessage.y,
0, 2, 2)
end
end

------------------------
Expand All @@ -69,4 +82,9 @@ function love.keypressed(key)
end

player.handleKey(key)

if (player.dead) then
draw.deathMessage.x = 0.5 + math.random()
draw.deathMessage.y = 0.5 + math.random()
end
end
23 changes: 21 additions & 2 deletions maze.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ maze.addConnection = function(x0, y0, x1, y1, open)
table.insert(maze.connections, {
x = x0 + x1,
y = y0 + y1,
open = open
open = open,
from = {
x = x0,
y = y0
},
to = {
x = x1,
y = y1
}
})

if (open) then
Expand Down Expand Up @@ -106,7 +114,18 @@ maze.writeToMap = function()
if (connection.open) then
nodes.addFloor(connection.x, connection.y)
else
nodes.addWall(connection.x, connection.y)
if (math.random() > 0.7) then
nodes.addSpikes(connection.x, connection.y)
if not (nodes.nodeMap[connection.from.x * 2] or {})[connection.from.y * 2] then
nodes.addWall(connection.from.x * 2, connection.from.y * 2)
end

if not (nodes.nodeMap[connection.to.x * 2] or {})[connection.to.y * 2] then
nodes.addWall(connection.to.x * 2, connection.to.y * 2)
end
else
nodes.addWall(connection.x, connection.y)
end
end
end
end
30 changes: 25 additions & 5 deletions nodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ SIZE = 100;

-- all nodes in the world
nodes = {}
nodes.collisionMap = {}
nodes.nodeMap = {} -- contains all generated nodes
nodes.collisionMap = {} -- contains solid nodes
nodes.deathMap = {} -- contatins deadly nodes

nodes.addNode = function (x, y, texture, walkable, deadly)
if (not nodes.nodeMap[x]) then
nodes.nodeMap[x] = {}
end
nodes.nodeMap[x][y] = true

nodes.addNode = function (x, y, texture, walkable)
if (not nodes.collisionMap[x]) then
nodes.collisionMap[x] = {}
end
nodes.collisionMap[x][y] = walkable

if (not nodes.deathMap[x]) then
nodes.deathMap[x] = {}
end
nodes.deathMap[x][y] = deadly

table.insert(nodes, {
x = x,
y = y,
Expand All @@ -26,13 +38,21 @@ nodes.addNode = function (x, y, texture, walkable)
end

nodes.addWall = function (x, y)
nodes.addNode(x, y, textures.wall, false)
nodes.addNode(x, y, textures.wall, false, true)
end

nodes.addFloor = function (x, y)
nodes.addNode(x, y, textures.floor, true)
nodes.addNode(x, y, textures.floor, true, false)
end

nodes.isWalkable = function(x, y)
nodes.addSpikes = function (x, y)
nodes.addNode(x, y, textures.spikes, true, true)
end

nodes.isWalkable = function (x, y)
return (nodes.collisionMap[x] or {})[y]
end

nodes.isDeadly = function (x, y)
return (nodes.deathMap[x] or {})[y]
end
7 changes: 6 additions & 1 deletion player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ require('nodes')

player = {
x = 0,
y = 0
y = 0,
dead = false
}

local function getMovement(key)
Expand Down Expand Up @@ -37,5 +38,9 @@ player.handleKey = function(key)
if movement and nodes.isWalkable(movement.x, movement.y) then
player.x = movement.x
player.y = movement.y

if (nodes.isDeadly(movement.x, movement.y)) then
player.dead = true
end
end
end
1 change: 1 addition & 0 deletions textures.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ textures.load = function()
textures.floor = love.graphics.newImage("assets/block-unit.png")
textures.wall = love.graphics.newImage("assets/wall-unit.png")
textures.player = love.graphics.newImage("assets/blindguyv2.png")
textures.spikes = love.graphics.newImage("assets/spike-unit.png")
end

0 comments on commit c33e747

Please sign in to comment.