Created
January 8, 2018 00:42
-
-
Save Caaz/206c4bfa88a9aee1599381f5c08b6c67 to your computer and use it in GitHub Desktop.
Map class for pico-wars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- require partials/class.lua | |
-- require picowars/tile.lua | |
-- require partials/binary.lua | |
local maps = { | |
-- require logs/picowars_map.p8l | |
} | |
class.map = class{ | |
new = function(this) | |
merge(this, { | |
tick = 0, | |
width = 16, | |
height = 16, | |
tiles = {}, | |
}) | |
this:empty_map() | |
this:load(bits_from_ints(maps[1])) | |
end, | |
methods = { | |
dirty = function(this) | |
this:each_tile(function(x,y) | |
this.tiles[x][y]:dirty() | |
end) | |
end, | |
empty_map = function(this) | |
for x = 0, this.width+1 do | |
this.tiles[x] = {} | |
for y = 0, this.height+1 do | |
this.tiles[x][y] = class.tile({ | |
x = x, | |
y = y, | |
map = this | |
}) | |
end | |
end | |
this:dirty() | |
end, | |
to_bits = function(this) | |
local bits = {} | |
concat(bits,itb(this.width/4-1,4)) | |
concat(bits,itb(this.height/4-1,4)) | |
for y = 1, this.height do | |
for x = 1, this.width do | |
local tile = this:get(x,y) | |
concat(bits,itb(tile.type,5)) | |
concat(bits,itb(tile.team,3)) | |
end | |
end | |
return bits | |
end, | |
to_ints = function(this) return bits_to_ints(this:to_bits()) end, | |
to_bytes = function(this) return bits_to_bytes(this:to_bits()) end, | |
load = function(this, bits) | |
this.width = pull(bits,4)*4+4 | |
this.height = pull(bits,4)*4+4 | |
this:empty_map() | |
for y = 1, this.height do | |
for x = 1, this.width do | |
local type = pull(bits,5) | |
local team = pull(bits,3) | |
if not this.tiles[x] then this.tiles[x] = {} end | |
this.tiles[x][y] = class.tile{ | |
type = type, | |
team = team, | |
map = this, | |
x = x, | |
y = y | |
} | |
end | |
end | |
this:dirty() | |
this:dirty() | |
this:dirty() | |
end, | |
save = function(this) | |
local ints = this:to_ints() | |
local s = '{' for i in all(ints) do s = s..i..',' end | |
printh(s..'},','logs/picowars_map') | |
end, | |
get = function(this,x,y) | |
if this.tiles[x] and this.tiles[x][y] then | |
return this.tiles[x][y] | |
else | |
return class.tile{map = this} | |
end | |
end, | |
neighbors = function(this,x,y) | |
result = {} | |
for vy = y - 1, y + 1 do | |
for vx = x - 1, x + 1 do | |
if not (vx == x and vy == y) then | |
add(result,this:get(vx,vy)) | |
end | |
end | |
end | |
return result | |
end, | |
each_tile = function(this, call) | |
for y = 0, this.height+1 do | |
for x = 0, this.width+1 do | |
call(x,y) | |
end | |
end | |
end, | |
set_wave = function(this) | |
local mod = flr(this.tick/10)%4 | |
if mod == 0 then | |
fillp(0b1000000000010000) | |
-- fillp(0b1000000001000000) | |
elseif mod == 1 then | |
fillp(0b0100000000100000) | |
elseif mod == 2 then | |
fillp(0b0010000001000000) | |
else | |
fillp(0b0001000010000000) | |
-- fillp(0b0100000100000000) | |
end | |
end, | |
draw = function(this) | |
palt(0,false) | |
local w = this.width*8 | |
local h = this.height*8 | |
fillp(0b1111011111111101) | |
rectfill(-38, -38, w+37, h+37, 0x6c) | |
fillp(0b1011111010111110) | |
rectfill(-30, -30, w+29, h+29, 0x6c) | |
fillp(0b0101101001011010) | |
rectfill(-24, -24, w+23, h+23, 0x6c) | |
fillp(0b0100000101000001) | |
rectfill(-16, -16, w+15, h+15, 0x6c) | |
this.tick+=1 | |
this:set_wave() | |
rectfill(-8,-8,w+7,h+7,0x6c) | |
-- fillp() | |
this:each_tile(function(x,y) | |
this.tiles[x][y]:draw() | |
-- spr(tile.sprite,(x-1)*8,(y-1)*8) | |
end) | |
fillp() | |
end | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment