Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- megaman battle network game test for ComputerCraft
- pastebin get aFuFVA2R bntest
- to do:
- +get the game working
- +add battle chips
- +add independently moving/acting enemies
- +make it play megaman battle network NBS music
- --]]
- --BEGIN PUE API
- local round = function(x) return x + 0.5 - (x + 0.5) % 1 end local deepCopy = function(tbl) return {table.unpack(tbl)} end local bcol, ccol = { [0]=" ",[colors.white]="0",[colors.orange]="1",[colors.magenta]="2",[colors.lightBlue]="3",[colors.yellow]="4",[colors.lime]="5",[colors.pink]="6",[colors.gray]="7",[colors.lightGray]="8",[colors.cyan]="9",[colors.purple]="a",[colors.blue]="b",[colors.brown]="c",[colors.green]="d",[colors.red]="e",[colors.black]="f"}, {} for k,v in pairs(bcol) do ccol[v] = k end paintutils.flipY = function(image) local output = {} for y = #image, 1, -1 do output[#output+1] = image[y] end return output end paintutils.flipX = function(image) local output,sizeX = {}, 0 for y = 1, #image do sizeX = math.max(sizeX,#image[y]) end for y = 1, #image do output[y] = {} for x = 1, sizeX do output[y][x] = deepCopy(image)[y][sizeX-(x-1)] or 0 end end return output end paintutils.grayOut = function(image) local output,grays = {},{[0] = 0,[colors.white]=colors.white,[colors.orange]=colors.lightGray,[colors.magenta]=colors.lightGray,[colors.lightBlue]=colors.lightGray,[colors.yellow]=colors.white,[colors.lime]=colors.lightGray,[colors.pink]=colors.lightGray,[colors.gray]=colors.gray,[colors.lightGray]=colors.lightGray,[colors.cyan]=colors.lightGray,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.gray,[colors.green]=colors.lightGray,[colors.red]=colors.gray,[colors.black]=colors.black} for y = 1, #image do output[y] = {} for x = 1, #image[y] do output[y][x] = grays[image[y][x]] or image[y][x] end end return output end paintutils.lighten = function(image) local output,lighters = {},{[0] = 0,[colors.white] = colors.white,[colors.orange] = colors.yellow,[colors.magenta] = colors.pink,[colors.lightBlue] = colors.white,[colors.yellow] = colors.white,[colors.lime] = colors.white,[colors.pink] = colors.white,[colors.gray] = colors.lightGray,[colors.lightGray] = colors.white,[colors.cyan] = colors.lightBlue,[colors.purple] = colors.magenta,[colors.blue] = colors.lightBlue,[colors.brown] = colors.orange,[colors.green] = colors.lime,[colors.red] = colors.magenta,[colors.black] = colors.gray} for y = 1, #image do output[y] = {} for x = 1, #image[y] do output[y][x] = lighters[image[y][x]] or image[y][x] end end return output end paintutils.darken = function(image) local output, darkers = {},{[0]=0,[colors.white]=colors.lightGray,[colors.orange]=colors.brown,[colors.magenta]=colors.purple,[colors.lightBlue]=colors.cyan,[colors.yellow]=colors.orange,[colors.lime]=colors.green,[colors.pink]=colors.magenta,[colors.gray]=colors.black,[colors.lightGray]=colors.gray,[colors.cyan]=colors.blue,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.black,[colors.green]=colors.gray,[colors.red]=colors.gray,[colors.black]=colors.black} for y = 1, #image do output[y] = {} for x = 1, #image[y] do output[y][x] = darkers[image[y][x]] or image[y][x] end end return output end paintutils.getSize = function(image) local xsize = 0 if type(image) ~= "table" then return 0,0 end for y = 1, #image do xsize = math.max(xsize,#image[y]) end return xsize, #image end paintutils.stretchImage = function(_image,sx,sy) local image,output = deepCopy(_image), {} if sx == 0 or sy == 0 then return {{}} end if sx < 0 then image = paintutils.flipX(image) end if sy < 0 then image = paintutils.flipY(image) end sx,sy = round(math.abs(sx)), round(math.abs(sy)) if sx == 0 or sy == 0 then return {{}} end local imX,imY = paintutils.getSize(image) local xcur,ycur for y = 1, sy do output[y] = {} for x = 1, sx do output[y][x] = image[math.max(1,round((y/sy)*imY))][math.max(1,round((x/sx)*imX))] or nil end end return output end paintutils.drawImageBlit = function(image,ix,iy) if (type(image) ~= "table") or (type(ix) ~= "number") or (type(iy) ~= "number") then error("Expected image, x, y", 2) else ix, iy = round(ix), round(iy) local buff for y = 1, #image do buff = "" for x = 1, #image[y] do buff = buff..bcol[image[y][x]] end term.setCursorPos(ix,iy+(y-1)) term.blit(buff,buff,buff) end end end paintutils.centerWithBlankSpace = function(_image,ix,iy,object) local image,scX,scY = deepCopy(_image) if object then scX,scY = object.getSize() else scX,scY = term.getSize() end local imgXsize,imgYsize = paintutils.getSize(image) if imgXsize == 0 or imgYsize == 0 then return {{}} end local incX,incY = math.floor((ix or (scX/2)) - (imgXsize/2)), math.floor((iy or (scY/2)) - (imgYsize/2)) local output = {} for y = 1, imgYsize + incY do output[y] = {} for x = 1, imgXsize + incX do if (x > incX) and (y > incY) then output[y][x] = image[y-incY][x-incX] or 0 else output[y][x] = 0 end end end return output end paintutils.drawImageBlitCenter = function(image,ix,iy,object) local scX,scY if object then scX,scY = object.getSize() else scX,scY = term.getSize() end local imgXsize,imgYsize = paintutils.getSize(image) return paintutils.drawImageBlit(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2)) end paintutils.drawImageCenter = function(image,ix,iy,object) local scX,scY if object then scX,scY = object.getSize() else scX,scY = term.getSize() end local imgXsize,imgYsize = paintutils.getSize(image) return paintutils.drawImage(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2)) end paintutils.merge = function(...) local output,arg,imgXsize,imgYsize,image,xdiff,ydiff = {}, table.pack(...), 0, 0 for a = 1, #arg do local x,y = paintutils.getSize(arg[a][1]) if not (x == 0 or y == 0) then x, y = x+arg[a][2], y+arg[a][3] imgXsize = math.max(imgXsize,x) imgYsize = math.max(imgYsize,y) end end for a = #arg, 1, -1 do image,xdiff,ydiff = table.unpack(arg[a]) xdiff, ydiff = xdiff - 1, ydiff - 1 for y = 1, imgYsize do output[y] = output[y] or {} for x = 1, imgXsize do output[y][x] = output[y][x] or 0 if image[y-ydiff] then if image[y-ydiff][x-xdiff] and (image[y-ydiff][x-xdiff] ~= 0) then output[y][x] = image[y-ydiff][x-xdiff] end end end end end return output end paintutils.fullScreen = function(bgcol,object) local scr_x,scr_y = (object or term).getSize() local output,l = {},{} bgcol = bgcol or colors.black for x = 1, scr_x do l[x] = bgcol end for y = 1, scr_y do output[y] = l end return output end paintutils.unloadImage = function(image) local output = "" for y = 1, #image do for x = 1, #image[y] do output = output..bcol[image[y][x]] end if y < #image then output = output.."\n" end end return output end paintutils.autocrop = function(image) local output,top,leftings = {},1,{} local isTopped = false for a = 1, #image do if (not isTopped) and (#image[a] > 0) then top = a isTopped = true end if isTopped then output[#output+1] = deepCopy(image[a]) end end for y = 1, #output do leftings[y] = 0 for x = 1, #output[y] do if (output[y][x] == 0) or (output[y][x] == nil) then leftings[y] = leftings[y] + 1 else break end end end local left = math.min(table.unpack(leftings)) for y = 1, #output do for l = 1, left do table.remove(output[y],1) end end return output end
- --END PUE API
- local stage = {}
- for y = 1, 3 do
- stage[y] = {}
- for x = 1, 6 do
- stage[y][x] = {
- exists = true,
- cracked = false,
- state = "normal",
- damage = 0,
- owner = 1,
- }
- end
- end
- panels = {
- [0] = { --blue
- ["normal"] = {{2048,256,256,256},{2048,256,256,256},{2048,2048,2048,2048}},
- ["cracked"] = {{2048,0,128,0},{2048,128,0,128},{2048,2048,2048,2048}},
- ["broken"] = {{2048,0,0,0},{2048,0,0,0},{2048,2048,2048,2048}},
- },
- [1] = { --red
- ["normal"] = {{256,256,256,16384},{256,256,256,16384},{16384,16384,16384,16384}},
- ["cracked"] = {{0,128,0,16384},{128,0,128,16384},{16384,16384,16384,16384}},
- ["broken"] = {{0,0,0,16384},{0,0,0,16384},{16384,16384,16384,16384}},
- },
- }
- local bgobject = {{16,16,16,16,16,16,16,16},{16,1,32768,32768,32768,32768,32768,16},{16,32768,32768,32768,32768,32768,32768,16},{16,32768,32768,32768,32768,32768,32768,16},{16,32768,32768,32768,32768,32768,32768,16},{16,16,16,16,16,16,16384,16},{16,16,16,16,16,16,16,16}}
- local backgroundObjects = {}
- local alternatePosition = false
- local addBGObject = function()
- local scr_x,scr_y = term.getSize()
- if alternatePosition then
- backgroundObjects[#backgroundObjects+1] = {x=scr_x+4,y=scr_y-4}
- else
- backgroundObjects[#backgroundObjects+1] = {x=scr_x-4,y=scr_y+4}
- end
- alternatePosition = not alternatePosition
- end
- local backgroundSpin = 1
- moveBGobjects = function()
- for a = 1, #backgroundObjects do
- backgroundObjects[a].x = backgroundObjects[a].x - 1
- backgroundObjects[a].y = backgroundObjects[a].y - 1
- end
- backgroundSpin = (backgroundSpin + 10) % 360
- end
- makeObjectBackground = function()
- local mergeInto = {}
- for a = 1, #backgroundObjects do
- mergeInto[#mergeInto] = {paintutils.stretchImage(bgobject,math.sin(math.rad(backgroundSpin or 90))*8,8),backgroundObjects[a].x,backgroundObjects[a].y}
- end
- return paintutils.merge(table.unpack(mergeInto),{paintutils.fullScreen(colors.gray),1,1})
- end
- local playerX, playerY = 1, 2 --relative to square grid
- local playerHP = 100
- local maxPlayerHP = 100
- local playerSprite = "stand"
- player = {
- ["stand"] = {{0,2048,2048,0},{2048,16,16,0},{0,512,512,0},{512,512,0,512},{0,2048,0,0},{512,0,512,0},{2048,0,0,2048}},
- ["hurt"] = {{0,2048,2048,2048},{0,2048,16,16},{512,512,512,512},{0,512,512,0},{0,2048,2048,0},{512,0,0,512},{2048,0,0,2048}},
- ["shoot"] = {{0,2048,2048,0},{2048,16,16,0},{0,512,512,2048},{512,512,0,0},{0,2048,0,0},{512,0,512,0},{2048,0,0,2048}},
- }
- hpbar = {
- ["high"] = {{256,256,256,256,256,256,256,256,2048,2048,2048},{256,256,256,256,256,256,256,256,2048,2048,16},{256,256,256,256,256,256,256,256,2048,16,16}},
- ["low"] = {{16384,16384,16384,16384,16384,16384,16384,16384,512,512,512},{16384,16384,16384,16384,16384,16384,16384,16384,512,512,256},{16384,16384,16384,16384,16384,16384,16384,16384,512,256,256}}
- }
- palate = {
- panel = {
- ownerTrim = {
- [0] = colors.blue,
- [1] = colors.red,
- },
- normal = colors.lightGray,
- poison = colors.purple,
- fire = colors.red,
- ice = colors.cyan,
- sand = colors.yellow,
- metal = colors.gray,
- },
- }
- local discolorSquare = function(square,condition)
- local output = {}
- for y = 1, #square do
- output[y] = {}
- for x = 1, #square[y] do
- if square[y][x] ~= palate.panel.ownerTrim[0] and square[y][x] ~= palate.panel.ownerTrim[1] and square[y][x] ~= 0 then
- output[y][x] = palate.panel[condition]
- else
- output[y][x] = square[y][x]
- end
- end
- end
- return output
- end
- render = function()
- local scene, square, panel, owner
- local scrollY = 10
- local mergeInto = {}
- local hasDecentHP = playerHP > (maxPlayerHP / 5)
- mergeInto[#mergeInto+1] = {hasDecentHP and hpbar.high or hpbar.low,1,1}
- mergeInto[#mergeInto+1] = {player[playerSprite],((playerX-1)*4)+2, ((playerY-1)*3)-5+scrollY}
- for y = 1, #stage do
- for x = 1, #stage[y] do
- square = stage[y][x]
- owner = stage[y][x].owner
- if square.exists then
- if square.cracked then
- panel = panels[owner].cracked
- else
- panel = panels[owner].normal
- end
- else
- panel = panels[owner].broken
- end
- mergeInto[#mergeInto+1] = {panel, ((x-1)*4)+2, ((y-1)*3)+scrollY}
- end
- end
- --mergeInto[#mergeInto+1] = {paintutils.fullScreen(colors.black),1,1}
- mergeInto[#mergeInto+1] = {makeObjectBackground(),1,1}
- scene = paintutils.merge(table.unpack(mergeInto))
- paintutils.drawImageBlit(scene,1,1)
- term.setCursorPos(2,2)
- if hasDecentHP then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.lightGray)
- else
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.red)
- end
- term.write(playerHP)
- end
- for y = 1, #stage do
- for x = 1, #stage[y] do
- if x <= #stage[y]/2 then
- stage[y][x].owner = 1
- else
- stage[y][x].owner = 0
- end
- end
- end
- local timers = {}
- local tickTimer = function()
- for k,v in pairs(timers) do
- if v > 0 then
- timers[k] = v-1
- if timers[k] == 0 then
- os.queueEvent(k)
- end
- end
- end
- end
- local startTimer = function(name, time)
- timers[name or "timer"] = time or 0
- end
- local canShoot = true
- startTimer("moveBackground",2)
- startTimer("makeNewBGObject",32)
- local doIt = function()
- local cpx,cpy
- while true do
- render()
- local evt = {os.pullEvent()}
- if evt[1] == "key" then
- local key = evt[2]
- cpx, cpy = playerX, playerY
- if key == keys.up then playerY = math.max(playerY-1,1)
- elseif key == keys.down then playerY = math.min(playerY+1,3)
- elseif key == keys.left then playerX = math.max(playerX-1,1)
- elseif key == keys.right then playerX = math.min(playerX+1,6) end
- if stage[playerY][playerX].owner == 0 then
- playerX, playerY = cpx, cpy
- end
- if key == keys.q then
- return sleep(0)
- elseif key == keys.x then
- if canShoot then
- playerSprite = "shoot"
- startTimer("canShootNow",5)
- startTimer("backToStand",3)
- canShoot = false
- end
- end
- elseif evt[1] == "backToStand" then
- playerSprite = "stand"
- elseif evt[1] == "moveBackground" then
- moveBGobjects()
- startTimer("moveBackground",2)
- elseif evt[1] == "makeNewBGObject" then
- addBGObject()
- startTimer("makeNewBGObject",32)
- elseif evt[1] == "canShootNow" then
- canShoot = true
- end
- end
- end
- runTheTimer = function()
- while true do
- sleep(0.05)
- tickTimer()
- end
- end
- parallel.waitForAny(doIt,runTheTimer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement