Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

WIP: MapState with Level, MapView and a first entitiy

  • Loading branch information...
commit 94ef3236ae6dd74cfedbba01372136a0040bdf9e 1 parent 5ebf0b0
@TomK32 authored
View
17 entities/beach.lua
@@ -0,0 +1,17 @@
+
+Beach = class("Beach", Entity)
+function Beach:initialize(position, tiles)
+ self.position = position
+ self.tiles = tiles
+end
+
+function Beach:draw()
+ for x = 1, #self.tiles do
+ for y = 1, #self.tiles[x] do
+ c = self.tiles[x][y]
+ print(c)
+ self.map.view.rectangle('fill', {255, 255, c, 255}, x, y)
+ self.map.view.print('.', {255, 255, 200, 240}, x, y)
+ end
+ end
+end
View
19 entities/entity.lua
@@ -0,0 +1,19 @@
+
+Entity = class("Entity")
+Entity.map = nil
+
+function Entity:initialize(options)
+ for k, v in pairs(options) do
+ self[k] = v
+ end
+ if not self.position then
+ self.position = {x = 1, y = 1, z = 1}
+ end
+end
+
+function Entity:draw()
+end
+
+function Entity:update(dt)
+end
+
View
10 game_states/map_state.lua
@@ -1,11 +1,17 @@
+require 'level'
require 'map'
+require 'generators/map_generator'
require 'views/map_view'
+require 'entities/entity'
+require 'entities/beach'
+
MapState = class("MapState", GameState)
function MapState:initialize()
- self.map = Map(200, 200)
- self.view = MapView(self, map)
+ self.level = Level(1, 1)
+ self.view = MapView(self, self.level.map)
+ self.view.map = self.level.map
end
function MapState:draw()
View
44 generators/map_generator.lua
@@ -0,0 +1,44 @@
+SimplexNoise = require("lib/SimplexNoise")
+LuaBit = require("lib/LuaBit")
+
+MapGenerator = class("MapGenerator")
+
+function MapGenerator:initialize(seed, map)
+ self.seed = seed
+ self.map = map
+ SimplexNoise.seedP(self.seed)
+end
+
+-- fill a whole map
+function MapGenerator:randomize()
+ self:newBeach()
+end
+
+-- int, int, 0..1, 0..1, int, int
+function MapGenerator:seedPosition(seed_x,seed_y, scale_x, scale_y, offset_x, offset_y)
+ scale_x = scale_x or 1
+ scale_y = scale_y or 1
+ offset_x = offset_x or 0
+ offset_y = offset_y or 0
+ return {
+ x = math.floor(((SimplexNoise.Noise2D(seed_x*0.1, seed_x*0.1)) * 120) % math.floor(scale_x * self.map.width-1) + offset_x) + 1,
+ y = math.floor(((SimplexNoise.Noise2D(seed_y*0.1, seed_y*0.1)) * 120) % math.floor(scale_x * self.map.height-1) + offset_y) + 1
+ }
+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
+ end
+ end
+ local beach = Beach({x = 1, y = 1, z = 1}, tiles)
+ beach.map = self.map
+ return map:addEntity(beach)
+end
+
+function MapGenerator:randomizeBeach()
+end
+
View
20 level.lua
@@ -0,0 +1,20 @@
+Level = class("Level")
+
+function Level:initialize(level, seed)
+ self.level = level
+ self.seed = seed
+ self.dt = 0
+
+ self.seed = self.seed + self.level
+
+
+ self.map = Map(200,200)
+
+ self.generator = MapGenerator(self.seed, self.map)
+end
+
+function Level:update(dt)
+ self.dt = self.dt + dt
+end
+
+
View
1  map.lua
@@ -17,6 +17,7 @@ function Map:initialize(width, height)
end
function Map:place(entity)
+ entity.map = self
if not self.layers[entity.position.z] then
self.layers[entity.position.z] = {}
end
View
84 views/map_view.lua
@@ -0,0 +1,84 @@
+
+MapView = class("MapView", View)
+MapView:include({
+ map = nil,
+ top_left = { x = 0, y = 0 }, -- offset
+ scale = { x = 10, y = 10 },
+ canvas = nil,
+})
+
+function MapView:initialize(map)
+ self.map = map
+ self:updateDisplay()
+ self.draw_cursor = false
+ self.canvas = love.graphics.newCanvas(self.display.width, self.display.height)
+ self:update()
+end
+
+function MapView:updateDisplay()
+ self.display = {
+ x = 10,
+ y = 10,
+ width = game.graphics.mode.width - 20,
+ height = game.graphics.mode.height - 20
+ }
+end
+
+function MapView:drawContent()
+ if self.canvas then
+ love.graphics.setColor(255,255,255,255)
+ love.graphics.draw(self.canvas, 0, 0)
+ end
+end
+
+function MapView:rectangle(style, colour, x, y)
+ love.graphics.setColor(unpack(colour))
+ love.graphics.rectangle(style, x * self.scale.x, y * self.scale.y, self.scale.x, self.scale.y)
+end
+
+function MapView:print(text, colour, x, y)
+ love.graphics.setColor(unpack(colour))
+ love.graphics.print(text, x * self.scale.x, y * self.scale.y)
+end
+
+function MapView:update()
+ love.graphics.setCanvas(self.canvas)
+ love.graphics.setColor(255,255,255,255)
+ love.graphics.rectangle('fill', 0,0,game.graphics.mode.width, game.graphics.mode.height)
+ love.graphics.setCanvas()
+ return self.canvas
+end
+
+function MapView:centerAt(position)
+ x = position.x - math.floor(self:tiles_x() / 2)
+ y = position.y - math.floor(self:tiles_y() / 2)
+ if math.abs(self.top_left.x - x) >= 1 or math.abs(self.top_left.y - y) >= 1 then
+ self.top_left = {x = x, y = y}
+ self:fixTopLeft()
+ end
+ self:update()
+end
+
+function MapView:moveTopLeft(offset, dontMoveCursor)
+ self.top_left.x = self.top_left.x + 3 * offset.x
+ self.top_left.y = self.top_left.y + 3 * offset.y
+ fixTopLeft()
+ if not dontMoveCursor then
+ self:moveCursor({x = offset.x * 3, y = offset.y * 3}, true)
+ end
+end
+
+function MapView:fixTopLeft()
+ max_x = math.floor(self.map.height - self:tiles_x())
+ if self.top_left.x < 0 then
+ self.top_left.x = 0
+ elseif self.top_left.x > max_x then
+ self.top_left.x = max_x + 1
+ end
+ max_y = math.floor(self.map.height - self:tiles_y())
+ if self.top_left.y < 0 then
+ self.top_left.y = 0
+ elseif self.top_left.y > max_y then
+ self.top_left.y = max_y + 1
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.