Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add waves and make them move towards the short

  • Loading branch information...
commit 3409fdfa73230589fd94c8a45b43d891ae327ba9 1 parent efb72c0
@TomK32 TomK32 authored
View
6 entities/beach.lua
@@ -7,14 +7,12 @@ end
function Beach:draw()
love.graphics.push()
- for x = 1, #self.tiles do
- for y = 1, #self.tiles[x] do
- c = self.tiles[x][y]
+ for x, row in pairs(self.tiles) do
+ for y, c in pairs(row) do
game.renderer:rectangle('fill', {255, 255, c, 255}, x-1, y-1)
game.renderer:print('.', {55, 55, 0, 240}, x-1, y-1)
end
end
- love.graphics.setCanvas(last_canvas)
love.graphics.pop()
end
View
44 entities/wave.lua
@@ -0,0 +1,44 @@
+
+Wave = class("Wave", Entity)
+function Wave:initialize(position, tiles)
+ self.position = position
+ self.tiles = tiles
+ self.dt = 0
+ self.dead = false
+end
+
+function Wave:draw()
+ love.graphics.push()
+ game.renderer:translate(self.position.x, self.position.y)
+ for x, row in pairs(self.tiles) do
+ for y, c in pairs(row) do
+ game.renderer:rectangle('fill', {0, 0, 255-c, 155}, x-1, y-1)
+ game.renderer:print('~', {2200, 200, 255-c, 255}, x-1, y-1)
+ end
+ end
+ love.graphics.pop()
+end
+
+
+function Wave:update(dt)
+ self.dead = false
+ self.dt = self.dt + dt * 3
+ if self.dt > 1 then
+ local new_tiles = {}
+ self.dt = 0
+ self.position.y = self.position.y - 1
+ if self.position.y < 1 then
+ self.position.y = 1
+ table.remove(self.tiles, 0)
+ end
+ for x, row in pairs(self.tiles) do
+ new_tiles[x] = {}
+ -- remove wave if it is very small
+ for y, c in pairs(row) do
+ new_tiles[x][y-1] = c
+ end
+ end
+ if self.position.y == 2 then self.dead = true end
+ self.tiles = new_tiles
+ end
+end
View
7 game_states/map_state.lua
@@ -6,6 +6,7 @@ require 'views/map_view'
require 'entities/entity'
require 'entities/beach'
+require 'entities/wave'
MapState = class("MapState", GameState)
function MapState:initialize()
@@ -18,5 +19,11 @@ end
function MapState:draw()
self.view:draw()
+
+ love.graphics.print("FPS: "..love.timer.getFPS(), 10, 20)
end
+function MapState:update(dt)
+ self.level:update(dt)
+ self.view:update()
+end
View
59 generators/map_generator.lua
@@ -5,12 +5,31 @@ MapGenerator = class("MapGenerator")
function MapGenerator:initialize(seed)
self.seed = seed
+ self:incrementSeed(0)
+ self.dt = { wave = 0 }
+end
+
+function MapGenerator:incrementSeed(dt)
+ self.seed = self.seed + dt
SimplexNoise.seedP(self.seed)
end
-- fill a whole map
function MapGenerator:randomize()
- self:newBeach()
+ self:newBeach(1, 1/4)
+ self:newWave(1)
+ self:newWave(2)
+ self:newWave(3)
+ self:newBeach(3, 1/7)
+end
+
+function MapGenerator:update(dt)
+ self.dt.wave = self.dt.wave + dt
+ if self.dt.wave > 0.7 then
+ self:incrementSeed(1)
+ self.dt.wave = 0
+ self:newWave(1)
+ end
end
-- int, int, 0..1, 0..1, int, int
@@ -25,17 +44,37 @@ function MapGenerator:seedPosition(seed_x,seed_y, scale_x, scale_y, offset_x, of
}
end
-function MapGenerator:newBeach()
- local tiles = {}
- for x=1, self.map.width do
- tiles[x] = {}
- for y=1, self.map.height/4 do
- tiles[x][y] = math.floor((SimplexNoise.Noise2D(x*0.1, y*0.1) + 1) * 120) % 255
+function MapGenerator:newWave(offset_y)
+ print('Add wave')
+ wave_factor = 6
+
+ local tiles = self:fillTiles(1, 1, self.map.width, wave_factor,
+ function(x,y)
+ local w = math.floor(SimplexNoise.Noise2D(x*0.002, y*0.03)*200) % (60+offset_y)
+ if y == wave_factor / 2 then
+ return w * 3
+ else
+ return w
+ end
end
- end
- return self.map:addEntity(Beach({x = 1, y = 1, z = 1}, tiles))
+ )
+ return self.map:addEntity(Wave({x = 0, y = self.map.height - offset_y + wave_factor, z = 2, speed = 1 }, tiles))
end
-function MapGenerator:randomizeBeach()
+function MapGenerator:newBeach(z, depth)
+ local tiles = self:fillTiles(1, 1, self.map.width, self.map.height * depth,
+ function(x,y) return math.floor((SimplexNoise.Noise2D(x*0.1, y*0.1) + 1) * 120) % 255 end
+ )
+ return self.map:addEntity(Beach({x = 0, y = 0, z = z}, tiles))
end
+function MapGenerator:fillTiles(x1, y1, x2, y2, callback)
+ local tiles = {}
+ for x=math.floor(x1), math.floor(x2-x1+1) do
+ tiles[x] = {}
+ for y=math.floor(y1), math.abs(math.floor(y2-y1+1)) do
+ tiles[x][y] = callback(x,y)
+ end
+ end
+ return tiles
+end
View
14 level.lua
@@ -8,12 +8,24 @@ function Level:initialize(level, seed)
self.seed = self.seed + self.level
- self.map = Map(40,30, MapGenerator(self.seed))
+ self.generator = MapGenerator(self.seed)
+ self.map = Map(40,30, self.generator)
end
function Level:update(dt)
self.dt = self.dt + dt
+ for layer, entities in pairs(self.map.layers) do
+ for i, entity in pairs(entities) do
+ entity:update(dt)
+ if entity.dead == true then
+ print("Removing", entity.position.y)
+ table.remove(self.map.layers[layer], i)
+ end
+ end
+ --print(layer, #self.map.layers[layer])
+ end
+ self.generator:update(dt)
end
View
3  renderers/ascii.lua
@@ -5,6 +5,9 @@
local AsciiRenderer = class("AsciiRenderer")
AsciiRenderer.map_view = nil
+function AsciiRenderer:translate(x, y)
+ love.graphics.translate(self:scaledXY(x,y))
+end
function AsciiRenderer:rectangle(style, color, x, y, tiles_x, tiles_y)
if not self.map_view then return end
love.graphics.setColor(unpack(color))
Please sign in to comment.
Something went wrong with that request. Please try again.