Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --]] RedGame API By: Redxone[[--
- function init()
- w,h = term.getSize()
- pmap = {}
- for i = 1, h do
- pmap[i] = {}
- for c = 1, w do
- pmap[i][c] = 0
- end
- end
- gmap = {}
- emp={}
- gplayer={}
- gblocks={}
- doors={}
- maps={}
- mapName = ""
- end
- function getGMAP()
- return gmap
- end
- function getPMAP()
- return pmap
- end
- function getGBLOCKS()
- return gblocks
- end
- function setPMAP(x,y,id)
- pmap[y][x] = id
- end
- function setGMAP(x,y,id)
- gmap[y][x] = id
- end
- function openTextbox()
- paintutils.drawBox(1,h-5,w,h,colors.gray)
- paintutils.drawFilledBox(2,h-4,w-1,h-1,colors.blue)
- end
- clearTextbox = openTextbox
- function drawTextbox(text,ln,speed)
- term.setCursorPos(6,h- ( 4 - ( ln-1 ) ) )
- term.setTextColor(colors.white)
- textutils.slowPrint(text,speed)
- end
- function createYesNo(YText,NText,ln)
- local sel = 1
- local waiting = true
- local opt = {
- {name=YText};
- {name=NText};
- }
- function waitResult()
- while waiting do
- for i = 1, #opt do
- term.setCursorPos((w/2) - #opt[i].name/2,h- ( 4 - ( (ln+i)-1 ) ) )
- if(sel == i )then
- write("["..opt[i].name.."]")
- else
- write(" "..opt[i].name.." ")
- end
- end
- a = {os.pullEvent("key")}
- if(a[2] == keys.w and sel > 1)then
- sel = sel - 1
- end
- if(a[2] == keys.s and sel < #opt)then
- sel = sel + 1
- end
- if(a[2] == keys.space)then
- if(sel == 1)then waiting = false return "ok" end
- if(sel == 2)then waiting = false return "no" end
- end
- end
- end
- return waitResult()
- end
- function addDoor(fromx,fromy,tox,toy,fromMap,toMap)
- doors[#doors+1] = {fx=fromx,fy=fromy,tx=tox,ty=toy,fMap=fromMap,tMap=toMap}
- end
- function draw()
- for i = 1, #gmap do
- for c = 1, #gmap[i] do
- term.setCursorPos(c,i)
- term.setTextColor(colors[ gblocks[ gmap[i][c] ].color ])
- term.setBackgroundColor(colors[ gblocks[ gmap[i][c] ].bcolor ])
- write(gblocks[ gmap[i][c] ].graphic)
- end
- end
- for i = 1, #pmap do
- for c = 1, #pmap[i] do
- if(pmap[i][c] ~= 0)then
- term.setCursorPos(c,i)
- term.setTextColor(colors[ gblocks[ pmap[i][c] ].color ])
- term.setBackgroundColor(colors[ gblocks[ pmap[i][c] ].bcolor ])
- write(gblocks[ pmap[i][c] ].graphic)
- end
- end
- end
- end
- function closeTextbox()
- os.pullEvent("key")
- draw()
- term.setCursorPos(1,h)
- term.setBackgroundColor(colors[gblocks[0].bcolor])
- term.clearLine()
- end
- function closeTextboxRaw()
- draw()
- term.setCursorPos(1,h)
- term.setBackgroundColor(colors[gblocks[0].bcolor])
- term.clearLine()
- end
- function drawMapPixel(x,y)
- term.setCursorPos(y,x)
- term.setTextColor(colors[ gblocks[ gmap[x][y] ].color ])
- term.setBackgroundColor(colors[ gblocks[ gmap[x][y] ].bcolor ])
- write(gblocks[ gmap[x][y] ].graphic)
- if(pmap[x][y] ~= 0)then
- term.setCursorPos(y,x)
- term.setTextColor(colors[ gblocks[ pmap[x][y] ].color ])
- term.setBackgroundColor(colors[ gblocks[ pmap[x][y] ].bcolor ])
- write(gblocks[ pmap[x][y] ].graphic)
- end
- end
- function lookupBlock(id)
- for i = 1, #gmap do
- for c = 1, #gmap[i] do
- if(gmap[i][c] == id)then return true end
- end
- end
- return false
- end
- function findBlockAt(id)
- inf = {}
- for i = 1, #gmap do
- for c = 1, #gmap[i] do
- if(gmap[i][c] == tonumber(id))then
- return i,c
- end
- end
- end
- return nil
- end
- function getBlockAt(x,y)
- return gmap[y][x]
- end
- function setBlockAt(id,x,y)
- gmap[y][x] = id
- drawMapPixel(y,x)
- end
- function breakBlockAt(x,y)
- gmap[y][x] = 0
- drawMapPixel(y,x)
- end
- function editBlock(id,color,bcolor,graphic,solid)
- gblocks[id].color = color
- gblocks[id].bcolor = bcolor
- gblocks[id].graphic = graphic
- gblocks[id].solid = solid
- draw()
- end
- function getCurrentMap()
- return mapName
- end
- function addMap(name,levmap,blocks)
- if(maps[name] ~= nil)then error("[Redgame] - > AddMap - > Map already added!") end
- maps[name] = {level=levmap,blockmap=blocks}
- end
- function setSolid(id,solid)
- gblocks[id].solid = solid
- end
- function getResource(file)
- f = fs.open(file,"r")
- res = textutils.unserialize(f.readAll())
- f.close()
- if(type(res) ~= "table")then error("[RedGame]: getResource -> file ["..file.."] must contain only a table!") end
- return res
- end
- function setMap(map)
- if(maps[map] == nil)then
- error("[RedGame] -> setMap -> No such map.")
- end
- gmap=maps[map].level
- mapName = map
- gblocks = maps[map].blockmap
- draw()
- end
- function getMap()
- return gmap
- end
- function createPlayer(block,startX,startY)
- local player = {
- x=startX,
- y=startY,
- tickrate=tonumber(0.1),
- physicsTick = os.startTimer(0.1),
- jump_height=2,
- hspd = 0,
- vspd = 0,
- blockid = block,
- multidir = false,
- physic_control= {
- ["direction_up"] = keys.w,
- ["direction_down"] = keys.s,
- ["direction_left"] = keys.a,
- ["direction_right"] = keys.d,
- },
- lastMove = "player_up",
- events = {},
- interactions ={},
- addInteraction = function(self,mydmap,x,y,func)
- self.interactions[#self.interactions+1] = {onmap=mydmap,x=x,y=y,active=true,event=func}
- end,
- removeInteraction = function(self,mydmap,x,y)
- for i = 1, #self.interactions do
- if(self.interactions[i].x == x and self.interactions[i].y == y and self.interactions[i].onmap == mydmap)then
- self.interactions[i].active=false;
- end
- end
- end,
- setPhysicsTick = function(self,time)
- self.tickrate = tonumber(time)
- self.physicsTick = os.startTimer(time)
- end,
- addEvent = function(self,check,evfunc)
- table.insert(self.events,{type=check,run=evfunc})
- end,
- getBlockUnder = function(self)
- if(gmap[self.y][self.x] ~= 0)then
- return gmap[self.y][self.x]
- end
- return 0
- end,
- checkCollision = function(self,x,y)
- hasColl = false
- if( gblocks[gmap[y][x]].solid or pmap[y][x] ~= 0)then hasColl = true end
- if( gmap[y][x] == 0 and pmap[y][x] == 0)then hasColl = false end
- return hasColl
- end,
- jump = function(self,ammount)
- if(self:checkCollision(self.x,self.y+1))then
- for i = 1, ammount do
- if(not self:checkCollision(self.x,self.y-1))then
- pmap[self.y][self.x] = 0
- drawMapPixel(self.y,self.x)
- self.y = self.y - 1
- pmap[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- end
- end
- end,
- moveUp=function(self,ammount)
- if(not self:checkCollision(self.x,self.y-1) and not self:checkPhysics("gravity"))then
- pmap[self.y][self.x] = 0
- drawMapPixel(self.y,self.x)
- self.y = self.y - ammount
- pmap[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- if(not self:checkCollision(self.x,self.y-1) and self:checkPhysics("gravity"))then
- self:jump(self.jump_height)
- end
- end,
- moveDown=function(self,ammount)
- if(not self:checkCollision(self.x,self.y+1))then
- pmap[self.y][self.x] = 0
- drawMapPixel(self.y,self.x)
- self.y = self.y + ammount
- pmap[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- end,
- moveLeft=function(self,ammount)
- --pmap[self.y][self.x-1] == 0
- if(not self:checkCollision(self.x-1,self.y))then
- pmap[self.y][self.x] = 0
- drawMapPixel(self.y,self.x)
- self.x = self.x - ammount
- pmap[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- end,
- moveRight=function(self,ammount)
- if(not self:checkCollision(self.x+1,self.y))then
- pmap[self.y][self.x] = 0
- drawMapPixel(self.y,self.x)
- self.x = self.x + ammount
- pmap[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- end,
- physics = {
- ["gravity"] = {has=false,func=function(self)
- if(not self:checkCollision(self.x,self.y+1))then
- self:moveDown(1)
- sleep(0.01)
- end
- end};
- ["smooth_experimental"] = {has=false,func=function(self)
- self.multidir = true
- self.physics["smooth_experimental"].has = false
- end};
- },
- checkPhysics = function(self,element)
- for k, v in pairs(self.physics) do
- if(k == element and v.has)then
- return true
- end
- end
- return false
- end,
- checkInteract = function(self)
- for i = 1, #self.interactions do
- if( ((self.y == self.interactions[i].y-1)
- and (self.x == self.interactions[i].x))
- or ((self.y == self.interactions[i].y+1)
- and (self.x == self.interactions[i].x))
- or ((self.y == self.interactions[i].y)
- and (self.x == self.interactions[i].x+1))
- or ((self.y == self.interactions[i].y)
- and (self.x == self.interactions[i].x-1))
- )then
- if(self.interactions[i].onmap == getCurrentMap() and self.interactions[i].active)then
- self.interactions[i].event()
- end
- end
- end
- end,
- controls = {
- {name = "player_interact",event="key",key=keys.space,func=function(self) self:checkInteract() end};
- {name="player_up",event="key",key=keys.w,func=function(self) self.lastMove = "player_up" self:moveUp(1) end};
- {name="player_down",event="key",key=keys.s,func=function(self) self.lastMove = "player_down" self:moveDown(1) end};
- {name="player_left",event="key",key=keys.a,func=function(self) self.lastMove = "player_left" self:moveLeft(1) end};
- {name="player_right",event="key",key=keys.d,func=function(self)self.lastMove = "player_right" self:moveRight(1) end};
- },
- --]] movement functions
- put = function(self)
- pmap[self.y][self.x] = self.blockid
- end,
- unput = function(self)
- pmap[self.y][self.x] = 0;
- drawMapPixel(self.y,self.x)
- end,
- applyPhysics = function(self,element)
- if(self.physics[element] ~= nil)then
- if(self.physics[element].has == false)then
- self.physics[element].has = true
- end
- else
- error("[RedGame]: player -> addPhysicsElement -> no such element!")
- end
- end,
- setJumpHeight = function(self,h)
- self.jump_height = h
- end,
- createPhysics = function(self,element,physic)
- if(self.physics[element] ~= nil)then error("[RedGame]: player - > createPhysicsElement -> element already exists!") end
- if(type(physic) ~= "table")then error("[RedGame]: player -> createPhysicsElement -> must be in table format eg {func=function(self) physics stuff end};") end
- metaindex = {has=false,func=function(self) end};
- physic = setmetatable(physic, {__index = metaindex})
- self.physics[element] = physic
- end,
- remapPhysicsControl = function(self,name,to)
- if(self.physic_control[name])then
- self.physic_control[name] = to
- else
- error("[RedGame]: player -> remapPhysicsControl invalid control!")
- end
- end,
- remapControl = function(self,name,to)
- for i = 1, #self.controls do
- if(self.controls[i].name == name)then
- self.controls[i].key = to
- end
- end
- end,
- addControl=function(self,controltable)
- if(type(controltable) ~= "table")then
- error("[RedGame]:addControl -> control must be a table in this format: {name=<control name>,event=<event>,key=<key in string format>,func=function() <actions> end}")
- else
- table.insert(self.controls,controltable)
- end
- end,
- importControls=function(self,controltable)
- if(type(controltable) ~= "table")then
- error("[RedGame]:importControls -> control must be a table in this format: controls = { {name=<control name>,event=<event>,key=<key in string format>,func=function() <actions> end}, ect... }")
- else
- self.controls = controltable
- end
- end,
- setPos = function(self,x,y)
- pmap[self.y][self.x] = 0
- drawMapPixel(self.y,self.x)
- self.x = x
- self.y = y
- self:checkCollision(self.x,self.y)
- pmap[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end,
- update=function(self)
- for i = 1, #doors do
- if(getCurrentMap() == doors[i].fMap and self.x == doors[i].fx and self.y == doors[i].fy)then
- setMap(doors[i].tMap)
- self:setPos(doors[i].tx,doors[i].ty)
- end
- end
- if(self.multidir)then
- pmap[self.y][self.x] = 0
- drawMapPixel(self.y,self.x)
- if(not self:checkCollision(self.x+self.hspd,self.y))then
- self.x = self.x + self.hspd
- end
- if(not self:checkCollision(self.x,self.y+self.vspd))then
- self.y = self.y + self.vspd
- end
- pmap[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- a = {os.pullEvent()}
- for k, v in pairs(self.events) do
- if(v.type == a[1])then
- v.run(a)
- end
- end
- if(a[1]=="timer")then
- self.physicsTick=os.startTimer(self.tickrate)
- for k, v in pairs(self.physics) do
- if(v.has)then v.func(self) end
- end
- end
- if(not self.multidir)then
- for i = 1, #self.controls do
- if(a[1] == self.controls[i].event and a[2] == self.controls[i].key)then
- self.controls[i].func(self)
- end
- end
- end
- if(self.multidir)then
- if(a[2] ~= self.physic_control["direction_up"] and a[2] ~= self.physic_control["direction_down"])then
- self.vspd = 0
- end
- if(a[2] ~= self.physic_control["direction_left"] and a[2] ~= self.physic_control["direction_right"])then
- self.hspd = 0
- end
- if(a[2] == self.physic_control["direction_up"])then
- if(self:checkPhysics("gravity"))then
- if(self:checkCollision(self.x,self.y+1))then
- self.vspd = -self.jump_height
- end
- else
- self.vspd = -self.jump_height
- end
- end
- if(a[2] == self.physic_control["direction_down"])then
- self.vspd = 1
- end
- if(a[2] == self.physic_control["direction_left"])then
- self.hspd = -1
- end
- if(a[2] == self.physic_control["direction_right"])then
- self.hspd = 1
- end
- end
- end,
- }
- return player
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement