-
Notifications
You must be signed in to change notification settings - Fork 2
/
tile.lua
64 lines (56 loc) · 1.38 KB
/
tile.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
SimplexNoise = require("lib/SimplexNoise")
LuaBit = require("lib/LuaBit")
Tile = class("Tile")
Tile:include({
entities = {},
color = nil,
seedVal = 0,
passable = true
})
function Tile:initialize(x, y)
self.entities = {}
self:seed(x,y)
end
function Tile:seed(x, y)
self.seedVal = math.floor((SimplexNoise.Noise2D(x*0.1, y*0.1) + 1) * 120) % 255
local s = self.seedVal
if s < 80 then
self.color = {0,0,0}
self.passable = false
elseif s >= 80 and s < 120 then
self.color = {s, s-20, math.floor(s / 2)}
elseif s >= 120 and s < 200 then
self.color = {s, s, s}
else
self.color = {s-40, s, s-40}
end
end
function Tile:addEntity(entity)
local class_name = entity.entity_type or entity.class.name
if not self.entities[class_name] then
self.entities[class_name] = {}
end
table.insert(self.entities[class_name], entity)
end
function Tile:removeEntity(entity)
local class_name = entity.entity_type or entity.class.name
if not self.entities[class_name] then return false end
for i, e in pairs(self.entities[class_name]) do
if e == entity then
table.remove(self.entities[class_name], i)
return true
end
end
return false
end
function Tile:get(klass)
return self.entities[klass]
end
function Tile:actors()
return self:get('Actor')
end
function Tile:draw()
for i, entity in ipairs(self.entities) do
entity:draw()
end
end