0,ÿÿÿ±>Sï}WÿÍu§ðp8·d%qy)6o;]ÉA¦ösï÷ôôô”°ÂVl†3 1) then burn = 1 end -- trace("burn "..burn) end if not btn(PAD_A) then burn = burn - 0.3 if (burn < 0) then burn = 0 end -- if (burn > 0) then trace("burn "..burn) end end if btn(PAD_LEFT) then rot = rot + 5 end if btn(PAD_RIGHT) then rot = rot - 5 end if (keyp(BTN_SPACE)) then pause = not pause end end function s_play_update() if (not pause) then -- calc thrust, gravity dx = math.cos(rot * deg2rad) dy = math.sin(rot * deg2rad) vx = vx + THRUST_FORCE * burn * dx vy = vy + THRUST_FORCE * -burn * dy + gy * dt -- Avoid shooting off the screen if (vy < MAX_VY) then vy = MAX_VY end x = x + vx * dt y = y + vy * dt res = hit_surface() if (res.hit) then -- TODO: Refactor if (res.flat == false) then s_crash_init() last_landing = STATUS_CRASHED sfx(0, "C-1", 40, 0, 15, 0) elseif ((math.abs(vx) < CRASH_VX) and (vy < CRASH_VY) and (math.abs(rot - 90) < CRASH_ROT)) then s_end_wait_init() if (vy < GREAT_VY) then last_landing = STATUS_GREAT music(1, -1, -1, false) else last_landing = STATUS_GOOD music(0, -1, -1, false) end else s_crash_init() last_landing = STATUS_CRASHED sfx(0, "C-1", 40, 0, 15, 0) end burn = 0 end end end function s_play_draw() if (not pause) then -- Draw if (burn > 0) then sfx(1, "D#1", -1, 1, 7 + 8 * burn) else sfx(-1, nil, nil, 1) end -- Draw cls(0) draw_lander() draw_surface() draw_velocity() draw_status() end end ------------------------------ function s_crash_init() state = STATE_CRASH t = 0 shrapnel = {} for i=1, math.random(8,12) do shrapnel[i] = { x = x, y = y, l = math.random(1,4), r = math.random() * 360, vx = 2 - 4*math.random(), vy = -2 + -2 * math.random(), vr = 10 - 20*math.random() } end end function s_crash_update() t = t + 1 if (t/60 > 5) then s_end_wait_init() end for i,s in ipairs(shrapnel) do s.vy = s.vy + gy * dt s.x = s.x + s.vx * dt s.y = s.y + s.vy * dt s.r = s.r + s.vr end end function s_crash_draw() cls(0) for i,s in ipairs(shrapnel) do dx = math.cos(s.r * deg2rad) dy = math.sin(s.r * deg2rad) line(s.x + s.l * dx, s.y - s.l * dy, s.x - s.l * dx, s.y + s.l * dy, 1) end draw_surface() draw_velocity() draw_status() end ------------------------------ function s_end_wait_init() state = STATE_END_WAIT end function s_end_wait_input() if (keyp(BTN_SPACE)) then s_play_init() end end function s_end_wait_draw() cls(0) if (last_landing ~= STATUS_CRASHED) then draw_lander() end draw_surface() draw_status() draw_start() end ------------------------------ function draw_status() if (last_landing == STATUS_GOOD) then print_center("Nice Landing!", 60, 1) elseif (last_landing == STATUS_GREAT) then print_center("Great Landing!", 60, 1) elseif (last_landing == STATUS_CRASHED) then print_center("CRASHED", 60, 1) end end function draw_start() print_center( "Press SPACE to start", 70, 3) end function draw_velocity() print("vx "..string.format("%1.2f", vx), 190, 0, (math.abs(vx) < CRASH_VX) and 1 or 2, true) print("vy "..string.format("%1.2f", vy), 190, 8, (vy < CRASH_VY) and 1 or 2, true) end function print_center(m, y, c) l = string.len(m) * 6 / 2 print(m, 120 - l, y, c) end function draw_lander() dx = math.cos(rot * deg2rad) dy = math.sin(rot * deg2rad) dpx = math.cos((rot-90) * deg2rad) dpy = math.sin((rot-90) * deg2rad) bw = 3 bh = 1 -- Return Module r = 3 circb(x + (bh + 1 + r) * dx, y - (bh + 1 + r) * dy, r, 1) -- pix(x + bh * dx + bw * dpx, y - bh * dy - bw * dpy, 2) -- top right -- pix(x - bh * dx + bw * dpx, y + bh * dy - bw * dpy, 2) -- bottom right -- pix(x + bh * dx - bw * dpx, y - bh * dy + bw * dpy, 2) -- top left -- pix(x - bh * dx - bw * dpx, y + bh * dy + w * dpy, 2) -- bottom left -- body housing -- top line line(x + bh * dx + bw * dpx, y - bh * dy - bw * dpy, x + bh * dx - bw * dpx, y - bh * dy + bw * dpy, 1) -- bottom line line(x - bh * dx + bw * dpx, y + bh * dy - bw * dpy, x - bh * dx - bw * dpx, y + bh * dy + bw * dpy, 1) -- left line line(x + bh * dx + bw * dpx, y - bh * dy - bw * dpy, x - bh * dx + bw * dpx, y + bh * dy - bw * dpy, 1) -- right line line(x + bh * dx - bw * dpx, y - bh * dy + bw * dpy, x - bh * dx - bw * dpx, y + bh * dy + bw * dpy, 1) -- Thruster tw = 1 twe = 3 th = 3 -- pix(x - bh * dx - tw * dpx, y + bh * dy + tw * dpy, 2) -- top left -- pix(x - bh * dx + tw * dpx, y + bh * dy - tw * dpy, 2) -- top right -- pix(x - (bh+th) * dx - twe * dpx, y + (bh+th) * dy + twe * dpy, 2) -- bottom left -- pix(x - (bh+th) * dx + twe * dpx, y + (bh+th) * dy - twe * dpy, 2) -- bottom right -- left line(x - bh * dx - tw * dpx, y + bh * dy + tw * dpy, x - (bh+th) * dx - twe * dpx, y + (bh+th) * dy + twe * dpy, 1) -- bottom line(x - (bh+th) * dx - twe * dpx, y + (bh+th) * dy + twe * dpy, x - (bh+th) * dx + twe * dpx, y + (bh+th) * dy - twe * dpy, 1) -- right line(x - bh * dx + tw * dpx, y + bh * dy - tw * dpy, x - (bh+th) * dx + twe * dpx, y + (bh+th) * dy - twe * dpy, 1) -- landing gear gw = 3 gwe = 5 gh = 4 line(x - bh * dx - gw * dpx, y + bh * dy + gw * dpy, x - (bh+gh) * dx - gwe * dpx, y + (bh+gh) * dy + gwe * dpy, 1) line(x - bh * dx + gw * dpx, y + bh * dy - gw * dpy, x - (bh+gh) * dx + gwe * dpx, y + (bh+gh) * dy - gwe * dpy, 1) -- burn -- pix( x - (bh+th) * dx, y + (bh+th) * dy, 2) -- pix( x - (bh+th) * dx, y + (bh+th + burn*10) * dy, 2) line(x - (bh+th) * dx, y + (bh+th) * dy, x - (bh+th + burn*10) * dx, y + (bh+th + burn*10) * dy, 1) -- center pixel pix(x, y, 2) end surface = {} function create_surface() sy = 100 sx = 0 seg = 5 surface = {} surface[1] = { x = sx, y = sy } while (seg > 0) do seg = seg - 1 ex = sx + (240 - sx) / math.random(seg, seg*2) ey = sy + math.random(-10, 10) if (ey > 135) then ey = 135 end table.insert(surface, { x = ex, y = ey }) sx = ex sy = ey end end function draw_surface() -- line(0, 135, 239, 135, 1) for i = 1, #surface - 1 do line(surface[i].x, surface[i].y, surface[i+1].x, surface[i+1].y, 1) end end function hit_surface() l_dist = 999 r_dist = 999 if (pix(math.ceil(x) + LANDER_WIDTH, math.ceil(y) + LANDER_HEIGHT) ~= 0) then if (pix(math.floor(x) - LANDER_WIDTH, math.ceil(y) + LANDER_HEIGHT) ~= 0) then return { hit = true, flat = true } else return { hit = true, flat = false } end end return { hit = false } end -------------------- -------------------- -- Main Initialization Routine s_end_wait_init() -- s_play_init() -- vx = 0 -- vy = 0 -- x = 120 -- y = 120 -------------------- --------------------