Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add a renderer and get as far as drawing the beach

  • Loading branch information...
commit efb72c02616849bbfa77ccd80cb18f4ec504e0dd 1 parent 94ef323
Thomas R. Koll TomK32 authored
11 entities/beach.lua
@@ -6,12 +6,17 @@ function Beach:initialize(position, tiles)
6 6 end
7 7
8 8 function Beach:draw()
  9 + love.graphics.push()
9 10 for x = 1, #self.tiles do
10 11 for y = 1, #self.tiles[x] do
11 12 c = self.tiles[x][y]
12   - print(c)
13   - self.map.view.rectangle('fill', {255, 255, c, 255}, x, y)
14   - self.map.view.print('.', {255, 255, 200, 240}, x, y)
  13 + game.renderer:rectangle('fill', {255, 255, c, 255}, x-1, y-1)
  14 + game.renderer:print('.', {55, 55, 0, 240}, x-1, y-1)
15 15 end
16 16 end
  17 + love.graphics.setCanvas(last_canvas)
  18 + love.graphics.pop()
  19 +end
  20 +
  21 +function Beach:update()
17 22 end
1  game.lua
@@ -5,6 +5,7 @@ game = {
5 5 mode = { height = love.graphics.getHeight(), width = love.graphics.getWidth() }
6 6 },
7 7 fonts = {},
  8 + renderer = require('renderers/ascii')
8 9 }
9 10
10 11 function game:createFonts(offset)
4 game_states/map_state.lua
@@ -10,7 +10,9 @@ require 'entities/beach'
10 10 MapState = class("MapState", GameState)
11 11 function MapState:initialize()
12 12 self.level = Level(1, 1)
13   - self.view = MapView(self, self.level.map)
  13 + self.view = MapView(self.level.map)
  14 + game.renderer.map_view = self.view
  15 + self.view:update()
14 16 self.view.map = self.level.map
15 17 end
16 18
7 generators/map_generator.lua
@@ -3,9 +3,8 @@ LuaBit = require("lib/LuaBit")
3 3
4 4 MapGenerator = class("MapGenerator")
5 5
6   -function MapGenerator:initialize(seed, map)
  6 +function MapGenerator:initialize(seed)
7 7 self.seed = seed
8   - self.map = map
9 8 SimplexNoise.seedP(self.seed)
10 9 end
11 10
@@ -34,9 +33,7 @@ function MapGenerator:newBeach()
34 33 tiles[x][y] = math.floor((SimplexNoise.Noise2D(x*0.1, y*0.1) + 1) * 120) % 255
35 34 end
36 35 end
37   - local beach = Beach({x = 1, y = 1, z = 1}, tiles)
38   - beach.map = self.map
39   - return map:addEntity(beach)
  36 + return self.map:addEntity(Beach({x = 1, y = 1, z = 1}, tiles))
40 37 end
41 38
42 39 function MapGenerator:randomizeBeach()
3  level.lua
@@ -8,9 +8,8 @@ function Level:initialize(level, seed)
8 8 self.seed = self.seed + self.level
9 9
10 10
11   - self.map = Map(200,200)
  11 + self.map = Map(40,30, MapGenerator(self.seed))
12 12
13   - self.generator = MapGenerator(self.seed, self.map)
14 13 end
15 14
16 15 function Level:update(dt)
7 map.lua
@@ -9,14 +9,17 @@
9 9 -- and update and draw functions
10 10
11 11 Map = class("Map")
12   -function Map:initialize(width, height)
  12 +function Map:initialize(width, height, generator)
13 13 self.width = width
14 14 self.height = height
15 15 self.layers = {} -- here the entities are stuffed into
16 16 self.map = {}
  17 + self.generator = generator
  18 + self.generator.map = self
  19 + self.generator:randomize()
17 20 end
18 21
19   -function Map:place(entity)
  22 +function Map:addEntity(entity)
20 23 entity.map = self
21 24 if not self.layers[entity.position.z] then
22 25 self.layers[entity.position.z] = {}
31 renderers/ascii.lua
... ... @@ -0,0 +1,31 @@
  1 +
  2 +-- Handles how things are being draw. tiles, colours, text
  3 +--
  4 +
  5 +local AsciiRenderer = class("AsciiRenderer")
  6 +AsciiRenderer.map_view = nil
  7 +
  8 +function AsciiRenderer:rectangle(style, color, x, y, tiles_x, tiles_y)
  9 + if not self.map_view then return end
  10 + love.graphics.setColor(unpack(color))
  11 + love.graphics.rectangle(style,
  12 + self:scaledX(x), self:scaledY(y),
  13 + (tiles_x or 1) * self.map_view.scale.x, (tiles_y or 1) * self.map_view.scale.y
  14 + )
  15 +end
  16 +
  17 +function AsciiRenderer:print(text, color, x, y)
  18 + love.graphics.setColor(unpack(color))
  19 + love.graphics.print(text, self:scaledXY(x, y))
  20 +end
  21 +function AsciiRenderer:scaledXY(x, y)
  22 + return self:scaledX(x), self:scaledY(y)
  23 +end
  24 +function AsciiRenderer:scaledX(x)
  25 + return x * self.map_view.scale.x
  26 +end
  27 +function AsciiRenderer:scaledY(y)
  28 + return y * self.map_view.scale.y
  29 +end
  30 +return AsciiRenderer
  31 +
28 views/map_view.lua
@@ -3,7 +3,7 @@ MapView = class("MapView", View)
3 3 MapView:include({
4 4 map = nil,
5 5 top_left = { x = 0, y = 0 }, -- offset
6   - scale = { x = 10, y = 10 },
  6 + scale = { x = 16, y = 16 },
7 7 canvas = nil,
8 8 })
9 9
@@ -12,39 +12,35 @@ function MapView:initialize(map)
12 12 self:updateDisplay()
13 13 self.draw_cursor = false
14 14 self.canvas = love.graphics.newCanvas(self.display.width, self.display.height)
15   - self:update()
16 15 end
17 16
18 17 function MapView:updateDisplay()
19 18 self.display = {
20 19 x = 10,
21 20 y = 10,
22   - width = game.graphics.mode.width - 20,
23   - height = game.graphics.mode.height - 20
  21 + width = math.min(self.map.width * self.scale.x, game.graphics.mode.width) - 20,
  22 + height = math.min(self.map.height * self.scale.y, game.graphics.mode.height) - 20
24 23 }
25 24 end
26 25
27 26 function MapView:drawContent()
28 27 if self.canvas then
29   - love.graphics.setColor(255,255,255,255)
  28 + love.graphics.setColor(255,255,255,155)
30 29 love.graphics.draw(self.canvas, 0, 0)
31 30 end
32 31 end
33 32
34   -function MapView:rectangle(style, colour, x, y)
35   - love.graphics.setColor(unpack(colour))
36   - love.graphics.rectangle(style, x * self.scale.x, y * self.scale.y, self.scale.x, self.scale.y)
37   -end
38   -
39   -function MapView:print(text, colour, x, y)
40   - love.graphics.setColor(unpack(colour))
41   - love.graphics.print(text, x * self.scale.x, y * self.scale.y)
42   -end
43   -
44 33 function MapView:update()
45 34 love.graphics.setCanvas(self.canvas)
46   - love.graphics.setColor(255,255,255,255)
  35 + love.graphics.setColor(55,55,55,255)
47 36 love.graphics.rectangle('fill', 0,0,game.graphics.mode.width, game.graphics.mode.height)
  37 + for layer, entities in pairs(self.map.layers) do
  38 + for i,entity in ipairs(entities) do
  39 + love.graphics.push()
  40 + entity:draw()
  41 + love.graphics.pop()
  42 + end
  43 + end
48 44 love.graphics.setCanvas()
49 45 return self.canvas
50 46 end

0 comments on commit efb72c0

Please sign in to comment.
Something went wrong with that request. Please try again.