Permalink
Browse files

Add randomly spawning pickups

  • Loading branch information...
1 parent b16da21 commit 0a4d50ceeadadfb4e65f28afa74f5f3a83b00136 @GloryFish committed Jan 9, 2011
View
@@ -31,6 +31,8 @@ Level = class(function(level, name)
end
local x, y = 1, 1
+
+ level.pickupSpawns = {}
for row in level.tileString:gmatch("[^\n]+") do
assert(#row == width, 'Map is not aligned: width of row ' .. tostring(y) .. ' should be ' .. tostring(width) .. ', but it is ' .. tostring(#row))
@@ -44,6 +46,9 @@ Level = class(function(level, name)
elseif character == 'E' then
level:addEnemyStart(x, y)
level.tiles[x][y] = ' '
+ elseif character == '*' then
+ level:addPickupSpawn(x, y)
+ level.tiles[x][y] = ' '
else
level.tiles[x][y] = character
end
@@ -64,6 +69,11 @@ function Level:addEnemyStart(x, y)
table.insert(self.enemyStarts, coords + vector(math.floor(self.tileSize * self.scale / 2), math.floor(self.tileSize * self.scale / 2)))
end
+function Level:addPickupSpawn(x, y)
+ local coords = self:toWorldCoords(vector(x, y))
+ table.insert(self.pickupSpawns, coords + vector(math.floor(self.tileSize * self.scale / 2), math.floor(self.tileSize * self.scale / 2)))
+end
+
function Level:draw()
love.graphics.setColor(255, 255, 255, 255)
for x, column in ipairs(self.tiles) do
View
@@ -0,0 +1,74 @@
+--
+-- pickup.lua
+-- RedditGameJam-05
+--
+-- Created by Jay Roberts on 2011-01-09.
+-- Copyright 2011 GloryFish.org. All rights reserved.
+--
+
+require 'class'
+require 'vector'
+
+Pickup = class(function(pickup, pos)
+
+ -- Tileset
+ pickup.tileset = love.graphics.newImage('resources/images/spritesheet.png')
+ pickup.tileset:setFilter('nearest', 'nearest')
+
+ pickup.tileSize = 16
+ pickup.scale = 2
+ pickup.offset = vector(pickup.tileSize / 2, pickup.tileSize / 2)
+
+ -- Quads, animation frames
+ pickup.animations = {}
+
+ pickup.animations['idle'] = {}
+ pickup.animations['idle'].frameInterval = 0.1
+ pickup.animations['idle'].quads = {
+ love.graphics.newQuad(0 * pickup.tileSize, 6 * pickup.tileSize, pickup.tileSize, pickup.tileSize, pickup.tileset:getWidth(), pickup.tileset:getHeight()),
+ love.graphics.newQuad(1 * pickup.tileSize, 6 * pickup.tileSize, pickup.tileSize, pickup.tileSize, pickup.tileset:getWidth(), pickup.tileset:getHeight()),
+ love.graphics.newQuad(2 * pickup.tileSize, 6 * pickup.tileSize, pickup.tileSize, pickup.tileSize, pickup.tileset:getWidth(), pickup.tileset:getHeight()),
+ love.graphics.newQuad(3 * pickup.tileSize, 6 * pickup.tileSize, pickup.tileSize, pickup.tileSize, pickup.tileset:getWidth(), pickup.tileset:getHeight())
+ }
+
+ pickup.animation = {}
+ pickup.animation.current = 'idle'
+ pickup.animation.frame = 1
+ pickup.animation.elapsed = 0
+
+ pickup.flip = 1
+ pickup.position = pos
+end)
+
+function Pickup:get()
+ -- Play sound
+end
+
+function Pickup:update(dt)
+ self.animation.elapsed = self.animation.elapsed + dt
+
+ -- Handle animation
+ if #self.animations[self.animation.current].quads > 1 then -- More than one frame
+ local interval = self.animations[self.animation.current].frameInterval
+
+ if self.animation.elapsed > interval then -- Switch to next frame
+ self.animation.frame = self.animation.frame + 1
+ if self.animation.frame > #self.animations[self.animation.current].quads then -- Aaaand back around
+ self.animation.frame = 1
+ end
+ self.animation.elapsed = 0
+ end
+ end
+end
+
+function Pickup:draw()
+ love.graphics.drawq(self.tileset,
+ self.animations[self.animation.current].quads[self.animation.frame],
+ self.position.x,
+ self.position.y,
+ 0,
+ self.scale * self.flip,
+ self.scale,
+ self.offset.x,
+ self.offset.y)
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
@@ -51,7 +51,7 @@ ______________________________________________
] [
] [
] [
-] P [
+] *** P [
] h################h## ###h###############h [
] H H H H [
] H H H H [
@@ -63,7 +63,7 @@ ______________________________________________
] H H H H H H [
] H ###h########### ####h####### H [
] H H H H [
-] H E H H H [
+] H H H H [
##############################################
]]
View
@@ -14,6 +14,7 @@ require 'player'
require 'enemy'
require 'heartburst'
require 'resolve'
+require 'pickup'
require 'astar'
require 'camera'
@@ -31,13 +32,13 @@ function game.enter(self, pre)
end
- lvl = Level(game.level)
+ level = Level(game.level)
- player = Player(lvl.playerStart)
+ player = Player(level.playerStart)
game.enemies = {}
- for i,enemyStart in ipairs(lvl.enemyStarts) do
+ for i,enemyStart in ipairs(level.enemyStarts) do
local enemy = Enemy(enemyStart, player)
table.insert(game.enemies, enemy)
end
@@ -47,14 +48,16 @@ function game.enter(self, pre)
camera = Camera()
camera.bounds = {
top = 0,
- right = math.max(lvl:getWidth(), love.graphics.getWidth()),
- bottom = math.max(lvl:getHeight(), love.graphics.getHeight()),
+ right = math.max(level:getWidth(), love.graphics.getWidth()),
+ bottom = math.max(level:getHeight(), love.graphics.getHeight()),
left = 0
}
camera.position = player.position
camera:update(0)
- astar = AStar(lvl)
+ astar = AStar(level)
+
+ game.pickups = {}
game.resolve = Resolve(vector(10, 580))
@@ -70,9 +73,9 @@ end
function game.mousereleased(self, x, y, button)
local mouseWorldPoint = vector(x, y) + camera.offset
- local mouseTilePoint = lvl:toTileCoords(mouseWorldPoint)
+ local mouseTilePoint = level:toTileCoords(mouseWorldPoint)
- local playerTilePoint = lvl:toTileCoords(player.position)
+ local playerTilePoint = level:toTileCoords(player.position)
path = astar:findPath(mouseTilePoint, playerTilePoint)
@@ -84,17 +87,23 @@ function game.mousereleased(self, x, y, button)
end
+function game.spawnRandomPickup(self)
+ local pos = level.pickupSpawns[math.random(#level.pickupSpawns)]
+ local pickup = Pickup(pos)
+ table.insert(self.pickups, pickup)
+end
+
function game.update(self, dt)
game.logger:update(dt)
local mouse = vector(love.mouse.getX(), love.mouse.getY()) + camera.offset
- local tile = lvl:toTileCoords(mouse)
+ local tile = level:toTileCoords(mouse)
local tileString = 'air'
tile = tile + vector(1, 1)
- if lvl.tiles[tile.x] then
- tileString = lvl.tiles[tile.x][tile.y]
+ if level.tiles[tile.x] then
+ tileString = level.tiles[tile.x][tile.y]
if tileString == nil or tileString == ' ' then
tileString = 'air'
@@ -109,9 +118,9 @@ function game.update(self, dt)
else
game.logger:addLine(string.format('State: %s', 'Jumping'))
end
- game.logger:addLine(string.format('Width: %i Height: %i', lvl:getWidth(), lvl:getHeight()))
+ game.logger:addLine(string.format('Width: %i Height: %i', level:getWidth(), level:getHeight()))
- if (lvl:pointIsWalkable(mouse)) then
+ if (level:pointIsWalkable(mouse)) then
game.logger:addLine(string.format('Walkable'))
else
game.logger:addLine(string.format('Wall'))
@@ -127,7 +136,7 @@ function game.update(self, dt)
-- Update enemies
for i, enemy in ipairs(game.enemies) do
- enemy:update(dt, lvl, player)
+ enemy:update(dt, level, player)
if player.position:dist(enemy.position) < 32 then
game.heartburst:burst(player.position, math.random(3, 5))
@@ -136,6 +145,24 @@ function game.update(self, dt)
end
end
+ -- Update pickups
+ local removePickups = {}
+ for i, pickup in ipairs(game.pickups) do
+ pickup:update(dt)
+
+ if player.position:dist(pickup.position) < 16 then
+ pickup:get()
+ table.insert(removePickups, i)
+ end
+ end
+ for i, v in ipairs(removePickups) do
+ table.remove(self.pickups, v-i+1)
+ end
+
+ if #self.pickups == 0 then
+ self:spawnRandomPickup()
+ end
+
game.heartburst:update(dt)
-- Apply any controller movement to the player
@@ -154,7 +181,7 @@ function game.update(self, dt)
gravityAmount = 0.5
end
- player.velocity = player.velocity + lvl.gravity * dt * gravityAmount -- Gravity
+ player.velocity = player.velocity + level.gravity * dt * gravityAmount -- Gravity
if dt > 0.5 then
player.velocity.y = 0
@@ -168,8 +195,8 @@ function game.update(self, dt)
local testBL = vector(curBL.x, newBL.y)
local testBR = vector(curBR.x, newBR.y)
- if lvl:pointIsWalkable(testBL) == false or lvl:pointIsWalkable(testBR) == false then -- Collide with bottom
- player:setFloorPosition(lvl:floorPosition(testBL))
+ if level:pointIsWalkable(testBL) == false or level:pointIsWalkable(testBR) == false then -- Collide with bottom
+ player:setFloorPosition(level:floorPosition(testBL))
player.velocity.y = 0
player.onground = true
end
@@ -179,7 +206,7 @@ function game.update(self, dt)
local testUL = vector(curUL.x, newUL.y)
local testUR = vector(curUR.x, newUR.y)
- if lvl:pointIsWalkable(testUL) == false or lvl:pointIsWalkable(testUR) == false then -- Collide with top
+ if level:pointIsWalkable(testUL) == false or level:pointIsWalkable(testUR) == false then -- Collide with top
player.velocity.y = 0
end
end
@@ -192,7 +219,7 @@ function game.update(self, dt)
local testUR = vector(newUR.x, curUR.y)
local testBR = vector(newBR.x, curBR.y - 1)
- if lvl:pointIsWalkable(testUR) == false or lvl:pointIsWalkable(testBR) == false then
+ if level:pointIsWalkable(testUR) == false or level:pointIsWalkable(testBR) == false then
player.velocity.x = 0
end
end
@@ -201,7 +228,7 @@ function game.update(self, dt)
local testUL = vector(newUL.x, curUL.y)
local testBL = vector(newBL.x, curBL.y - 1)
- if lvl:pointIsWalkable(testUL) == false or lvl:pointIsWalkable(testBL) == false then
+ if level:pointIsWalkable(testUL) == false or level:pointIsWalkable(testBL) == false then
player.velocity.x = 0
end
end
@@ -215,18 +242,21 @@ function game.update(self, dt)
camera.focus = player.position
camera:update(dt)
-
-
end
function game.draw(self)
love.graphics.push()
-- Game
love.graphics.translate(-camera.offset.x, -camera.offset.y)
- lvl:draw()
+ level:draw()
- -- Update enemies
+ -- Draw pickups
+ for i, pickup in ipairs(game.pickups) do
+ pickup:draw()
+ end
+
+ -- Draw enemies
for i, enemy in ipairs(game.enemies) do
enemy:draw()
end

0 comments on commit 0a4d50c

Please sign in to comment.