Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scr_x, scr_y = term.getSize()
- local dude = {
- x = scr_x / 2,
- y = scr_y / 2,
- oldx = scr_x / 2,
- oldy = scr_y / 2,
- xvel = 0,
- yvel = 0,
- speed = 0.6,
- friction = 1.5,
- bullets = {},
- bulletSpeed = 1
- }
- local tick = function(dude)
- dude.oldx = dude.x
- dude.oldy = dude.y
- dude.x = dude.x + dude.xvel
- dude.y = dude.y + dude.yvel
- dude.xvel = dude.xvel / dude.friction
- dude.yvel = dude.yvel / dude.friction
- local rList = {}
- for i = 1, #dude.bullets do
- if not (inRange(dude.bullets[i].x, 1, scr_x + 1) and inRange(dude.bullets[i].y, 1, scr_y + 1)) then
- if dude.bullets[i].offScreen == false then
- rList[#rList + 1] = i
- end
- else
- dude.bullets[i].offScreen = false
- end
- dude.bullets[i].oldx = dude.bullets[i].x
- dude.bullets[i].oldy = dude.bullets[i].y
- dude.bullets[i].x = dude.bullets[i].x + dude.bullets[i].xvel
- dude.bullets[i].y = dude.bullets[i].y + dude.bullets[i].yvel
- dude.bullets[i].xvel = dude.bullets[i].xvel / dude.bullets[i].friction
- dude.bullets[i].yvel = dude.bullets[i].yvel / dude.bullets[i].friction
- end
- for i = #rList, 1, -1 do
- table.remove(dude.bullets, rList[i])
- end
- end
- local render = function()
- if (math.floor(dude.oldx) ~= math.floor(dude.x)) or (math.floor(dude.oldy) ~= math.floor(dude.y)) then
- term.setCursorPos(dude.oldx, dude.oldy)
- term.write(" ")
- end
- term.setCursorPos(dude.x, dude.y)
- term.write("O")
- for i = 1, #dude.bullets do
- if (math.floor(dude.bullets[i].oldx) ~= math.floor(dude.bullets[i].x)) or (math.floor(dude.bullets[i].oldy) ~= math.floor(dude.bullets[i].y)) then
- term.setCursorPos(dude.bullets[i].oldx, dude.bullets[i].oldy)
- term.write(" ")
- end
- term.setCursorPos(dude.bullets[i].x, dude.bullets[i].y)
- term.write("x")
- end
- end
- local spawnBullet = function(dude, xMult, yMult)
- table.insert(dude.bullets, {
- x = dude.x,
- y = dude.y,
- oldx = dude.oldx,
- oldy = dude.oldy,
- xvel = dude.bulletSpeed * xMult,
- yvel = dude.bulletSpeed * yMult,
- offScreen = not (inRange(dude.x, 1, scr_x + 1) and inRange(dude.y, 1, scr_y + 1)),
- friction = 1
- })
- end
- inRange = function(num, min, max)
- return num >= min and num <= max
- end
- local tickTimer = os.startTimer(0.05)
- local keysDown = {}
- local evt
- term.clear()
- while true do
- evt = {os.pullEvent()}
- if evt[1] == "key" and not evt[3] then
- keysDown[evt[2]] = 0
- elseif evt[1] == "key_up" then
- keysDown[evt[2]] = nil
- elseif evt[1] == "timer" and evt[2] == tickTimer then
- tick(dude)
- tickTimer = os.startTimer(0.05)
- if keysDown[keys.w] then
- dude.yvel = dude.yvel - dude.speed
- end
- if keysDown[keys.s] then
- dude.yvel = dude.yvel + dude.speed
- end
- if keysDown[keys.a] then
- dude.xvel = dude.xvel - dude.speed
- end
- if keysDown[keys.d] then
- dude.xvel = dude.xvel + dude.speed
- end
- if keysDown[keys.up] == 0 then
- spawnBullet(dude, 0, -1)
- end
- if keysDown[keys.down] == 0 then
- spawnBullet(dude, 0, 1)
- end
- if keysDown[keys.left] == 0 then
- spawnBullet(dude, -1, 0)
- end
- if keysDown[keys.right] == 0 then
- spawnBullet(dude, 1, 0)
- end
- for k,v in pairs(keysDown) do
- keysDown[k] = v + 1
- end
- render()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement