Created
February 5, 2019 18:04
-
-
Save arachonteur/425fa61ced0ce239bedb052023e26e3a to your computer and use it in GitHub Desktop.
sokobangarang, a pretty mediocre prototype for something i instantly realized i didn't want to make
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
| -- title: sokobangarang | |
| -- author: victoria lacroix | |
| -- desc: a pretty mediocre prototype for something i instantly realized i didn't want to make | |
| -- script: lua | |
| t = 0 | |
| lt = 0 | |
| dt = 0 | |
| dts = {} | |
| avg_dt = 0 | |
| Button = { | |
| UP = 0, | |
| DOWN = 1, | |
| LEFT = 2, | |
| RIGHT = 3, | |
| A = 4, | |
| B = 5, | |
| X = 6, | |
| Y = 7 | |
| } | |
| game = { | |
| width = 240, | |
| height = 138, | |
| prompts = {}, | |
| scenes = {}, | |
| activeScene = "menu", | |
| queuedScene = false, | |
| setScene = function(self, _scene) | |
| self.queuedScene = _scene | |
| if self.scenes[_scene].resetOnLoad then | |
| self.scenes[_scene] = scene(self.scenes[_scene].init, true) | |
| end | |
| end, | |
| playing = 1, | |
| queuePlay = false, | |
| play = function(self, id) | |
| if id ~= self.playing then | |
| self.queuePlay = id | |
| end | |
| end, | |
| add = function(self, ...) | |
| return self.scenes[self.activeScene]:add(...) | |
| end, | |
| get = function(self, ...) | |
| return self.scenes[self.activeScene]:get(...) | |
| end, | |
| remove = function(self, ...) | |
| return self.scenes[self.activeScene]:remove(...) | |
| end, | |
| update = function(self, ...) | |
| return self.scenes[self.activeScene]:update(...) | |
| end, | |
| draw = function(self, ...) | |
| return self.scenes[self.activeScene]:draw(...) | |
| end, | |
| cleanup = function(self) | |
| return self.scenes[self.activeScene]:cleanup() | |
| end | |
| } | |
| Camera = { | |
| x = 0, | |
| y = 0, | |
| w = 30, | |
| h = 17, | |
| sx = 0, | |
| sy = 0, | |
| transparent = 0, | |
| scale = 1, | |
| visible = false, | |
| bg = 0 | |
| } | |
| function cam() | |
| return Camera.x, Camera.y, Camera.w, Camera.h, Camera.sx, Camera.sy, Camera.transparent, Camera.scale | |
| end | |
| function scene(init, resetOnLoad) | |
| return { | |
| queuedToRemove = {}, | |
| entityOrder = {}, | |
| doneInit = false, | |
| init = init, | |
| resetOnLoad = resetOnLoad, | |
| entities = {}, | |
| add = function(self, entity, name) | |
| if entity == nil then | |
| return false | |
| end | |
| if entity.name == nil then | |
| if name == nil then | |
| entity.name = "entity_" .. math.random(1000) .. time() | |
| else | |
| entity.name = name | |
| end | |
| end | |
| self.entities[entity.name] = entity | |
| table.insert(self.entityOrder, entity.name) | |
| end, | |
| get = function(self, entity) | |
| return self.entities[entity] | |
| end, | |
| remove = function(self, name) | |
| table.insert(self.queuedToRemove, name) | |
| end, | |
| _remove = function(self, name) | |
| local entity = nil | |
| if type(name) == "string" then | |
| entity = self.entities[name] | |
| else | |
| for _name, _e in pairs(self.entities) do | |
| if _e == name then | |
| name = _name | |
| entity = _e | |
| end | |
| end | |
| end | |
| self.entities[name] = nil | |
| return entity | |
| end, | |
| update = function(self, dt) | |
| if not self.doneInit then | |
| self:init() | |
| self.doneInit = true | |
| end | |
| for name, entity in pairs(self.entityOrder) do | |
| local entity = self:get(entity) | |
| entity:input(dt) | |
| entity:update(dt) | |
| entity:animate(dt) | |
| end | |
| end, | |
| draw = function(self) | |
| for name, entity in pairs(self.entityOrder) do | |
| local entity = self:get(entity) | |
| if entity.visible then | |
| entity:draw() | |
| end | |
| end | |
| end, | |
| cleanup = function(self) | |
| for index, name in pairs(self.queuedToRemove) do | |
| for index, entity in ipairs(self.entityOrder) do | |
| if entity == name then | |
| table.remove(self.entityOrder, index) | |
| end | |
| end | |
| self:_remove(name) | |
| end | |
| self.queuedToRemove = {} | |
| end | |
| } | |
| end | |
| function entity(options) | |
| local e = { | |
| id = 0, | |
| x = 0, | |
| y = 0, | |
| -- sprite stuff | |
| sprite = 0, | |
| dir = 1, | |
| flip = 0, | |
| rotate = 0, | |
| w = 1, | |
| h = 1, | |
| scale = 1, | |
| transparent = 0, | |
| visible = true, | |
| -- animation code | |
| animated = true, | |
| frames = {}, | |
| frameRate = 0.2, | |
| frameLife = 0, | |
| frameIndex = 1, | |
| -- stats | |
| health = 5, | |
| -- update code | |
| input = function(self, dt) | |
| end, | |
| update = function(self, dt) | |
| end, | |
| animate = function(self, dt) | |
| if(self.animated) then | |
| self.frameLife = self.frameLife + dt | |
| end | |
| if(self.frameLife > self.frameRate) then | |
| self.frameIndex = self.frameIndex + 1 | |
| self.frameLife = 0 | |
| if(self.frameIndex > #self.frames) then | |
| self.frameIndex = 1 | |
| end | |
| end | |
| self.sprite = self.frames[self.frameIndex] | |
| end, | |
| draw = function(self, dt) | |
| spr(self.sprite, self.x, self.y, self.transparent, self.scale, self.flip, self.rotate, self.w, self.h) | |
| end | |
| } | |
| if options ~= nil then | |
| for key, value in pairs(options) do | |
| e[key] = value | |
| end | |
| end | |
| return e | |
| end | |
| function TIC() | |
| lt = t | |
| t = time() | |
| dt = (t - lt) / 1000 | |
| table.insert(dts, dt) | |
| if #dts > 60 then | |
| while #dts > 60 do | |
| table.remove(dts, 1) | |
| end | |
| end | |
| local totalDT = 0 | |
| for index, timer in pairs(dts) do | |
| totalDT = totalDT + timer | |
| end | |
| avg_dt = totalDT / #dts | |
| if game.queuePlay then | |
| music(game.queuePlay) | |
| game.playing = game.queuePlay | |
| game.queuePlay = false | |
| end | |
| game:update(dt) | |
| cls(Camera.bg) | |
| if Camera.visible then | |
| map(cam()) | |
| end | |
| game:draw() | |
| end | |
| function cursor(x, y) | |
| return entity({ | |
| tileX = 1, | |
| tileY = 1, | |
| frames = {17, 18}, | |
| input = function(self, dt, speed) | |
| if btnp(Button.UP, 15, 5) then | |
| self.tileY = self.tileY - 1 | |
| end | |
| if btnp(Button.DOWN, 15, 5) then | |
| self.tileY = self.tileY + 1 | |
| end | |
| if btnp(Button.LEFT, 15, 5) then | |
| self.tileX = self.tileX - 1 | |
| end | |
| if btnp(Button.RIGHT, 15, 5) then | |
| self.tileX = self.tileX + 1 | |
| end | |
| if btnp(Button.A) then | |
| for y = -1, 1 do | |
| for x = -1, 1 do | |
| local tile = mget(self.tileX + x, self.tileY + y) | |
| if tile == 1 then | |
| mset(self.tileX + x, self.tileY + y, 2) | |
| elseif tile == 2 then | |
| mset(self.tileX + x, self.tileY + y, 1) | |
| elseif tile == 4 then | |
| local targetTile = mget(self.tileX + (x * 2), self.tileY + (y * 2)) | |
| local move = true | |
| if targetTile == 1 then | |
| move = false | |
| end | |
| if targetTile == 3 then | |
| game:add(winrar()) | |
| end | |
| if move then | |
| mset(self.tileX + x, self.tileY + y, 2) | |
| mset(self.tileX + (x * 2), self.tileY + (y * 2), 4) | |
| end | |
| end | |
| end | |
| end | |
| end | |
| self.x = self.tileX * 8 | |
| self.y = self.tileY * 8 | |
| end | |
| }) | |
| end | |
| function winrar() | |
| return entity{ | |
| draw = function(self) | |
| print "congratulations. you won." | |
| end | |
| } | |
| end | |
| function rock(x, y) | |
| return entity({ | |
| frames = {4} | |
| }) | |
| end | |
| function init() | |
| game.scenes.main = scene(function(self) | |
| Camera.visible = true | |
| game:add(cursor(1, 1)) | |
| end) | |
| game.activeScene = "main" | |
| end | |
| init() | |
| -- <TILES> | |
| -- 001:0777777077777777777777777777777737777773333333331333333101111110 | |
| -- 002:0000000000000000000000000000000000000000000000001000000101111110 | |
| -- 003:0000000000ab0eb00035bb500035555000305500003000000030000000300000 | |
| -- 004:0000000000000000008dd80002dddd2002dddd20028dd8200122221000122100 | |
| -- 017:0660066060000006600000060000000000000000600000066000000606600660 | |
| -- 018:0ee00ee0e000000ee000000e0000000000000000e000000ee000000e0ee00ee0 | |
| -- </TILES> | |
| -- <MAP> | |
| -- 002:000000002020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 003:000000002020101010101010101010202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 004:000000002010202020202020202020102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 005:000000002010202020202020202020102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 006:000000002010202040202020202020102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 007:000000002010204020202020203020102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 008:000000002010202040202020202020102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 009:000000002010202020202020202020102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 010:000000002010202020202020202020102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 011:000000002020101010101010101010202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- 012:000000002020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 | |
| -- </MAP> | |
| -- <WAVES> | |
| -- 000:00000000ffffffff00000000ffffffff | |
| -- 001:0123456789abcdeffedcba9876543210 | |
| -- 002:0123456789abcdef0123456789abcdef | |
| -- </WAVES> | |
| -- <SFX> | |
| -- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000 | |
| -- </SFX> | |
| -- <PALETTE> | |
| -- 000:140c1c44243430346d4e4a4e854c30346524d04648757161597dced27d2c8595a16daa2cd2aa996dc2cadad45edeeed6 | |
| -- </PALETTE> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment