Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Test of top-down shooter
- pastebin get QVDcNiud test
- --]]
- local channel = 1990
- local modem = peripheral.find("modem")
- if not modem then error("Get a modem.") end
- modem.open(channel)
- --local tsv = term.current().setVisible
- local scr_x, scr_y = term.getSize()
- local keysDown = {}
- local netKeysDown = {}
- local miceDown = {}
- local netMiceDown = {}
- local p --network player
- control = {
- UP = keys.w,
- DOWN = keys.s,
- LEFT = keys.a,
- RIGHT = keys.d,
- SHOOTUP = keys.up,
- SHOOTDOWN = keys.down,
- SHOOTLEFT = keys.left,
- SHOOTRIGHT = keys.right,
- BLOCKPUT = keys.space,
- }
- chVis = function(vis)
- if tsv then tsv(vis) end
- end
- bulletMap = {} --map of all bullets onscreen
- backMap = {} --the background prettyness
- solidMap = {} --the actual hitbox
- blockMap = {} --blocks that block bullets, but not players
- makePlayer = function(name,x,y,color,number)
- return {
- name = name,
- x = x,
- y = y,
- color = color or colors.gray,
- health = 99,
- maxhealth = 99,
- number = number or 1,
- cooldown = 0,
- }
- end
- makeBullet = function(x,y,xspeed,yspeed,char)
- local boolet = {
- x = x,
- y = y,
- xspeed = xspeed,
- yspeed = yspeed,
- char = char or "o",
- }
- bulletMap[#bulletMap+1] = boolet
- end
- placeBlock = function(x,y,char,txt,bg)
- local block = {
- x = x,
- y = y,
- char = char or "%",
- txt = txt or colors.white,
- bg = bg or colors.cyan,
- }
- blockMap[#blockMap+1] = block
- end
- deleteBlock = function(x,y)
- for a = 1, #blockMap do
- if blockMap[a].x == x and blockMap[a].y == y then
- table.remove(blockMap,a)
- break
- end
- end
- end
- scrollX,scrollY = 0,0
- makeMap = function(preset)
- preset = preset or 1
- local output = {}
- presets = {
- [1] = function()
- solidMap = {{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0}}
- backMap = {{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},{0,1,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,1,1,0,0},{0,1,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,0},{0,1,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,32,32,32,32,32,32,1,1},{0,1,1,1,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,1},{0,0,0,1,256,256,256,256,256,256,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,1},{0,256,0,1,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,1},{0,256,0,1,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,1,1,1,256,256,256,256,256,256,256,256,256,64,64,1},{0,256,0,1,1,1,1,1,1,256,256,256,256,256,256,256,256,256,256,256,256,256,256,1,1,1,0,1,1,256,256,256,256,256,256,256,256,256,256,1},{0,256,0,0,0,0,0,0,1,1,1,1,128,128,128,128,256,256,256,256,256,1,1,1,0,0,0,1,256,256,256,256,256,256,256,256,256,256,256,1},{0,256,0,256,0,256,0,0,0,0,0,1,1,1,1,128,128,128,128,1,1,1,0,0,0,0,1,1,256,256,256,256,256,256,256,256,256,256,256,1},{0,256,0,256,0,256,0,256,0,256,0,0,0,0,1,1,1,1,1,0,0,0,0,256,0,0,1,128,128,128,128,128,128,128,256,256,256,256,1,1},{0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,0,0,0,0,0,0,256,0,256,0,0,1,1,128,128,128,128,128,128,128,1,1,1,1,0},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,1,1,1,1,128,128,1,1,1,0,0,0,0},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,0,1,1,1,1,0,0,0,256,0,256},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,0,0,0,0,256,0,256,0,256},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256}}
- end
- }
- presets[preset]()
- end
- makeMap(1)
- P = 1 --player one, so far...
- pys = { --p' yourself
- makePlayer("Lapi'wun",3,3,colors.blue,1),
- makePlayer("Anpi'tew",5,3,colors.orange,2),
- }
- renderMap = function()
- chVis(false)
- term.setBackgroundColor(colors.black)
- term.clear()
- paintutils.drawImage(backMap,(-scrollX)+1,(-scrollY)+1)
- for k,v in pairs(blockMap) do
- if (v.x-scrollX >= 1) and (v.x-scrollX <= scr_x) and (v.y-scrollY >= 1) and (v.y-scrollY <= scr_y) then
- term.setCursorPos(v.x-scrollX,v.y-scrollY)
- term.setBackgroundColor(v.bg)
- term.setTextColor(v.txt)
- term.write("%")
- end
- end
- for k,v in pairs(bulletMap) do
- if (v.x-scrollX >= 1) and (v.x-scrollX <= scr_x) and (v.y-scrollY >= 1) and (v.y-scrollY <= scr_y) then
- term.setCursorPos(v.x-scrollX,v.y-scrollY)
- local beegee = backMap[math.floor(v.y)][math.floor(v.x)]
- if beegee == 0 then beegee = colors.black end
- term.setBackgroundColor(beegee or colors.pink)
- term.setTextColor(colors.white)
- term.write(v.char)
- end
- end
- for k,v in pairs(pys) do
- if (v.x-scrollX >= 1) and (v.x-scrollX <= scr_x) and (v.y-scrollY >= 1) and (v.y-scrollY <= scr_y) then
- term.setCursorPos(v.x-scrollX,v.y-scrollY)
- term.setBackgroundColor(v.color or colors.white)
- term.setTextColor(colors.black)
- term.write(v.number)
- end
- end
- chVis(true)
- end
- getInput = function()
- while true do
- local evt = {os.pullEvent()}
- if evt[1] == "key" then
- keysDown[evt[2]] = true
- elseif evt[1] == "key_up" then
- keysDown[evt[2]] = false
- elseif evt[1] == "mouse_click" then
- miceDown[evt[2]] = true
- elseif evt[1] == "mouse_up" then
- miceDown[evt[2]] = false
- end
- end
- end
- between = function(num,min,max)
- if num > max then
- return max
- elseif num < min then
- return min
- else
- return num
- end
- end
- checkIfInMap = function(x,y)
- if solidMap[y] then
- if (solidMap[y][x]) and (solidMap[y][x] ~= 0) then
- return true
- end
- end
- return false
- end
- checkIfInPlayer = function(x,y,exceptionP) --exceptionP is so that bullets aren't stopped by those who shoot them
- for k,v in pairs(pys) do
- if v.x == x and v.y == y and v.number ~= (exceptionP or 0) then
- return true
- end
- end
- return false
- end
- checkIfInBlock = function(x,y)
- for k,v in pairs(blockMap) do
- if v.x == x and v.y == y then
- return true
- end
- end
- return false
- end
- doMovementTick = function()
- for a = #bulletMap, 1, -1 do
- for b = 1, math.max(math.abs(bulletMap[a].xspeed),math.abs(bulletMap[a].yspeed)) do
- if checkIfInMap(bulletMap[a].x,bulletMap[a].y) or checkIfInPlayer(bulletMap[a].x,bulletMap[a].y,P) or checkIfInBlock(bulletMap[a].x,bulletMap[a].y) then
- if checkIfInBlock(bulletMap[a].x,bulletMap[a].y) then
- deleteBlock(bulletMap[a].x,bulletMap[a].y)
- end
- table.remove(bulletMap,a)
- break
- else
- bulletMap[a].x = bulletMap[a].x + between(bulletMap[a].xspeed, -1, 1)
- bulletMap[a].y = bulletMap[a].y + between(bulletMap[a].yspeed, -1, 1)
- end
- end
- end
- --manual camera controls
- if keysDown[keys.t] then
- scrollY = scrollY - 1
- elseif keysDown[keys.g] then
- scrollY = scrollY + 1
- end
- if keysDown[keys.f] then
- scrollX = scrollX - 1
- elseif keysDown[keys.h] then
- scrollX = scrollX + 1
- end
- --player movement
- if keysDown[control.UP] then
- if not checkIfInMap(pys[P].x, pys[P].y - 1) then
- pys[P].y = pys[P].y - 1
- end
- end
- if keysDown[control.DOWN] then
- if not checkIfInMap(pys[P].x, pys[P].y + 1) then
- pys[P].y = pys[P].y + 1
- end
- end
- if keysDown[control.LEFT] then
- if not checkIfInMap(pys[P].x - 1, pys[P].y) then
- pys[P].x = pys[P].x - 1
- end
- end
- if keysDown[control.RIGHT] then
- if not checkIfInMap(pys[P].x + 1, pys[P].y) then
- pys[P].x = pys[P].x + 1
- end
- end
- if keysDown[control.BLOCKPUT] then
- if not checkIfInBlock(pys[P].x,pys[P].y) then
- placeBlock(pys[P].x,pys[P].y)
- end
- end
- --making projectiles
- if pys[P].cooldown > 0 then
- pys[P].cooldown = pys[P].cooldown - 1
- if pys[P].cooldown < 0 then pys[P].cooldown = 0 end
- elseif keysDown[control.SHOOTUP] or keysDown[control.SHOOTDOWN] or keysDown[control.SHOOTLEFT] or keysDown[control.SHOOTRIGHT] then
- pys[P].cooldown = 2
- local xspeed,yspeed
- local speedMod = 2
- if keysDown[control.SHOOTUP] then
- yspeed = -1
- elseif keysDown[control.SHOOTDOWN] then
- yspeed = 1
- else
- yspeed = 0
- end
- if keysDown[control.SHOOTLEFT] then
- xspeed = -1
- elseif keysDown[control.SHOOTRIGHT] then
- xspeed = 1
- else
- xspeed = 0
- end
- xspeed = xspeed * speedMod
- yspeed = yspeed * speedMod
- makeBullet(pys[P].x,pys[P].y,xspeed,yspeed,"o")
- end
- end
- doIt = function()
- while true do
- doMovementTick()
- doNetMovement(p)
- renderMap()
- sleep(0)
- end
- end
- local companionID
- getNetInput = function()
- while true do
- local event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
- if frequency == channel and replyFrequency == channel and type(message) == "table" then
- --if message[1] == companionID and type(message[2]) == "table" then
- netKeysDown = message[2] or {}
- --end
- end
- end
- end
- doNetMovement = function(P)
- --player movement
- if netKeysDown[control.UP] then
- if not checkIfInMap(pys[P].x, pys[P].y - 1) then
- pys[P].y = pys[P].y - 1
- end
- end
- if netKeysDown[control.DOWN] then
- if not checkIfInMap(pys[P].x, pys[P].y + 1) then
- pys[P].y = pys[P].y + 1
- end
- end
- if netKeysDown[control.LEFT] then
- if not checkIfInMap(pys[P].x - 1, pys[P].y) then
- pys[P].x = pys[P].x - 1
- end
- end
- if netKeysDown[control.RIGHT] then
- if not checkIfInMap(pys[P].x + 1, pys[P].y) then
- pys[P].x = pys[P].x + 1
- end
- end
- if netKeysDown[control.BLOCKPUT] then
- if not checkIfInBlock(pys[P].x,pys[P].y) then
- placeBlock(pys[P].x,pys[P].y)
- end
- end
- --making projectiles
- if pys[P].cooldown > 0 then
- pys[P].cooldown = pys[P].cooldown - 1
- if pys[P].cooldown < 0 then pys[P].cooldown = 0 end
- elseif netKeysDown[control.SHOOTUP] or netKeysDown[control.SHOOTDOWN] or netKeysDown[control.SHOOTLEFT] or netKeysDown[control.SHOOTRIGHT] then
- pys[P].cooldown = 2
- local xspeed,yspeed
- local speedMod = 2
- if netKeysDown[control.SHOOTUP] then
- yspeed = -1
- elseif netKeysDown[control.SHOOTDOWN] then
- yspeed = 1
- else
- yspeed = 0
- end
- if netKeysDown[control.SHOOTLEFT] then
- xspeed = -1
- elseif netKeysDown[control.SHOOTRIGHT] then
- xspeed = 1
- else
- xspeed = 0
- end
- xspeed = xspeed * speedMod
- yspeed = yspeed * speedMod
- makeBullet(pys[P].x,pys[P].y,xspeed,yspeed,"o")
- end
- end
- sendNetOutput = function()
- while true do
- modem.transmit(channel,channel,{os.getComputerID(),keysDown})
- os.queueEvent("yield")
- os.pullEvent("yield")
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- local ctime = os.time()
- modem.transmit(channel,channel,{"JOIN",os.getComputerID(),ctime})
- --[[term.write("Waiting for player...")
- local event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
- while true do
- event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
- if frequency == channel and replyFrequency == channel then
- if type(message) == "table" then
- if message == "JOIN" and type(message[2]) == "number" and type(message[3]) == "number" then
- if message[3] > ctime then
- P = 1
- else
- P = 2
- end
- companionID = message[2]
- break
- end
- end
- end
- end
- --]]
- print("player:")
- P = tonumber(read())
- if P == 1 then p = 2 else p = 1 end
- parallel.waitForAny(doIt,getInput,sendNetOutput, getNetInput)
Add Comment
Please, Sign In to add comment