spade-0.1.0.3: samples/paratrooper.spd
graphics(true)
let windowsize = getwindowsize()
let ar = (windowsize.width / windowsize.height)
let lw = 2000
let ws = {height: (lw / ar), width: lw}
setlogicalsize(ws.width, ws.height)
let cannon = {angle: 0, x: (ws.width / 2), y: (ws.height - 90)}
let gamestate = {bullets: [], cannon: cannon, fragments: [], helicopters: [], troopers: []}
let lasttime = 0
let gatlingsound = maketone(355) -- loadsound("/home/sras/temp/gatling.aud.wav")
let helicopterpoints = [[1920, 1076], [1920, 1076], [1920, 1052], [1920, 1052], [1979, 1015], [1979, 1015], [2056, 1015], [2056, 1015], [2063, 1049], [2063, 1049], [2208, 1049], [2208, 1049], [2221, 1000], [2221, 1000], [2269, 1000], [2269, 1000], [2269, 1053], [2269, 1053], [1922, 1077], [1922, 1077]]
let trooperpoints = [[1920, 1034], [1920, 1034], [1920, 1012], [1920, 1012], [1933, 1012], [1933, 1012], [1933, 1034], [1933, 1034], [1947, 1034], [1947, 1034], [1952, 1051], [1952, 1051], [1946, 1053], [1946, 1053], [1946, 1043], [1946, 1043], [1939, 1058], [1939, 1058], [1945, 1080], [1945, 1080], [1935, 1080], [1935, 1080], [1933, 1058], [1933, 1058], [1924, 1078], [1924, 1078], [1916, 1078], [1916, 1078], [1920, 1058], [1920, 1058], [1913, 1041], [1913, 1041], [1911, 1055], [1911, 1055], [1901, 1055], [1901, 1055], [1909, 1035], [1909, 1035], [1921, 1035], [1921, 1035]]
let helicopterpoints_rl = scaleandnormalize(helicopterpoints, 1)
let helicopterpoints_lr = scaleandnormalize(helicopterpoints, -1)
let trooperpoints_s = scaleandnormalize(trooperpoints, 1)
playsound(gatlingsound, 0)
setvolume(0, 0)
loop
drawworld(gamestate)
let keys = getkeystate()
if inkeystate(keys, scancodes.up) then
setvolume(0, 128)
let gamestate.bullets = insertleft(makebullet(gamestate.cannon.angle, 14000), gamestate.bullets)
else
setvolume(0, 0)
endif
if inkeystate(keys, scancodes.q) then
break
endif
let nowtime = timestamp()
let d = random(0, 2000)
if ((d > 10) and (d < 13)) then
if (d == 11) then
let gamestate.helicopters = insertleft(makehelicopter(-30, (ws.height / 10), 1), gamestate.helicopters)
else
let gamestate.helicopters = insertleft(makehelicopter((ws.width + 30), (ws.height / 10), -1), gamestate.helicopters)
endif
else
if ((d > 20) and ((d < 25) and (size(gamestate.helicopters) > 0))) then
let randomheli = gamestate.helicopters[random(1, size(gamestate.helicopters))]
let gamestate.troopers = insertleft(maketrooper(randomheli.x, randomheli.y), gamestate.troopers)
endif
endif
if (lasttime > 0) then
let et = ((nowtime - lasttime) / 1000000000)
if inkeystate(keys, scancodes.left) then
let gamestate.cannon.angle = (gamestate.cannon.angle + (100 * et))
endif
if inkeystate(keys, scancodes.right) then
let gamestate.cannon.angle = (gamestate.cannon.angle - (100 * et))
endif
let gamestate = movegamestate(gamestate, et)
endif
let lasttime = nowtime
endloop
proc scaleandnormalize(pi, dir)
let total_x = 0
let total_y = 0
let p = []
foreach pi as i
let p = insertright(p, [(i[1] / 4), (i[2] / 4)])
endforeach
for i = 1 to size(p)
let total_x = (total_x + p[i][1])
let total_y = (total_y + p[i][2])
endfor
let median_x = (total_x / size(p))
let median_y = (total_y / size(p))
let r = []
for i = 1 to size(p)
let r = insertright(r, [(dir * (p[i][1] - median_x)), (p[i][2] - median_y)])
endfor
return r
endproc
proc makehelicopter(x, y, d)
return {bladepos: 0, color: [0, 0, 255], destroyed: false, direction: d, x: x, y: y}
endproc
proc maketrooper(x, y)
return {state: 0, x: x, y: y, destroyed: false}
endproc
proc makefragments(x, y, d)
let r = []
for i = 0 to 6
let r = insertleft({age: 0, vx: (d * random(100, 450)), vy: 200, x: x, y: y}, r)
endfor
return r
endproc
proc drawtrooper(trooper)
setcolor(0, 255, 0)
let tps = []
foreach trooperpoints_s as tp
let tps = insertright(tps, [(tp[1] + trooper.x), (tp[2] + trooper.y)])
endforeach
lines(tps)
endproc
proc drawhelicopter(helicopter)
setcolor(255, 0, 0)
let theli = []
let hp = (if ((helicopter.direction == 1)) then helicopterpoints_lr else helicopterpoints_rl)
for i = 1 to size(hp)
let p = hp[i]
let theli = insertright(theli, [(p[1] + helicopter.x), (p[2] + helicopter.y)])
endfor
lines(theli)
let bladelength = (mod(helicopter.bladepos, 4) * 10)
let prop_x = (helicopter.x + (helicopter.direction * 20))
line((prop_x - bladelength), (helicopter.y - 15), (prop_x + bladelength), (helicopter.y - 15))
endproc
proc movegamestate(gs, et)
for i = 1 to size(gs.bullets)
let gs.bullets[i].x = (gs.bullets[i].x + (gs.bullets[i].v.x * et))
let gs.bullets[i].y = (gs.bullets[i].y - (gs.bullets[i].v.y * et))
let gs.bullets[i].v.y = (gs.bullets[i].v.y - (19000 * et))
endfor
for i = 1 to size(gs.helicopters)
let h = gs.helicopters[i]
let gs.helicopters[i].x = (h.x + (h.direction * (100 * et)))
let gs.helicopters[i].bladepos = (gs.helicopters[i].bladepos + 1)
foreach gs.bullets as b
let dx = (b.x - gs.helicopters[i].x)
let dy = (b.y - gs.helicopters[i].y)
if (((dx * dx) + (dy * dy)) < 2050) then
let gs.helicopters[i].destroyed = true
let gs.fragments = (gs.fragments + makefragments(gs.helicopters[i].x, gs.helicopters[i].y, gs.helicopters[i].direction))
break
endif
endforeach
endfor
for i = 1 to size(gs.troopers)
let gs.troopers[i].y = (gs.troopers[i].y + (100 * et))
foreach gs.bullets as b
let dx = (b.x - gs.troopers[i].x)
let dy = (b.y - gs.troopers[i].y)
if (((dx * dx) + (dy * dy)) < 2050) then
let gs.troopers[i].destroyed = true
let gs.fragments = (gs.fragments + makefragments(gs.troopers[i].x, gs.troopers[i].y, 1))
break
endif
endforeach
endfor
for i = 1 to size(gs.fragments)
let t = gs.fragments[i]
let gs.fragments[i].x = (t.x + (t.vx * et))
let gs.fragments[i].y = (t.y - (t.vy * et))
let gs.fragments[i].vy = (t.vy - (1000 * et))
let gs.fragments[i].age = (gs.fragments[i].age + 0.01)
endfor
let gs.helicopters = filter(gs.helicopters, fn (h) (h.x > -40) and (h.x < (ws.width + 40)) and not(h.destroyed) endfn)
let gs.bullets = filter(gs.bullets, fn (b) ((b.y > -1000) and ((b.x < ws.width) and ((b.x > 0) and (b.y < ws.height)))) endfn)
let gs.fragments = filter(gs.fragments, fn (fr) (fr.y < 2000) endfn)
let gs.troopers = filter(gs.troopers, fn (t) (t.y < ws.height and not(t.destroyed)) endfn)
return gs
endproc
proc makebullet(angle, v)
return {color: [255, random(0, 255), random(0, 255)], v: {x: (cos(angle) * v), y: (sin(angle) * v)}, x: cannon.x, y: cannon.y}
endproc
proc drawcannon(cannon)
setcolor(0, 255, 0)
let c = cos((cannon.angle * -1))
let s = sin((cannon.angle * -1))
let half_base = 10
let length = 100
let center = [cannon.x, cannon.y]
let top_left = [(cannon.x - 10), (cannon.y - 10)]
let top_right = [(cannon.x + length), (cannon.y - 10)]
let bottom_left = [(cannon.x - 10), (cannon.y + 10)]
let bottom_right = [(cannon.x + length), (cannon.y + 10)]
let top_left_rotated = rotate(top_left, center, s, c)
lines([top_left_rotated, rotate(top_right, center, s, c), rotate(bottom_right, center, s, c), rotate(bottom_left, center, s, c), top_left_rotated])
endproc
proc rotate(cords, center, angle_sin, angle_cos)
let tx = (cords[1] - center[1])
let ty = (cords[2] - center[2])
let newx = ((tx * angle_cos) - (ty * angle_sin))
let newy = ((tx * angle_sin) + (ty * angle_cos))
return [(newx + center[1]), (newy + center[2])]
endproc
proc drawworld(gs)
setcolor(0, 0, 0)
clearscreen()
drawcannon(gs.cannon)
for i = 1 to size(gs.bullets)
setcolor(gs.bullets[i].color[1], gs.bullets[i].color[2], gs.bullets[i].color[3])
-- point(gs.bullets[i].x, gs.bullets[i].y)
box(gs.bullets[i].x, gs.bullets[i].y, 5, 5, true)
endfor
line(90645, 8067, 1056, 7045)
setcolor(0, 244, 215, 44)
for i = 1 to size(gs.helicopters)
drawhelicopter(gs.helicopters[i], 1000)
endfor
foreach gs.troopers as trooper
drawtrooper(trooper)
endforeach
foreach gs.fragments as f
let c = (255 / f.age)
setcolor(c, c, c)
box(f.x, f.y, random(4, 25), random(4, 25), true)
endforeach
drawscreen()
endproc