diff --git a/lib/demo/bowling.lua b/lib/demo/bowling.lua index 23d7bd5..ed69ec0 100644 --- a/lib/demo/bowling.lua +++ b/lib/demo/bowling.lua @@ -67,7 +67,7 @@ function init_event() reference_pin:setPosition(0, 0, lane_bottom - maxz - 0.1) reference_ball = ag.createSphere(0.3) reference_ball:setColor(0.7, 0.1, 0.9) - reference_ball:setMass(50) + reference_ball:setMass(10) reference_ball:setPosition(0, -2, -1.5) resetPins() setupStars() @@ -76,10 +76,15 @@ function init_event() target_x = 0 game_state = "waiting" last_update_time = ag.elapsedTime() + mouse_moved = false + setCamera() +end + +function setCamera() + ag.setCamera(position_x, -10, 5, target_x, 20, 0) end function update_event() - ag.setCamera(position_x, -10, 5, target_x, 20, 0) local now = ag.elapsedTime() if (ag.isKeyDown("a") or ag.isKeyDown("left")) then position_x = position_x - (now - last_update_time) / 300 @@ -94,17 +99,21 @@ function update_event() end end last_update_time = now + setCamera() ag.callList(stars) end function mouse_motion_event(x, y, xrel, yrel) - target_x = target_x + xrel / 100 - if (target_x < -2) then - target_x = -2 - elseif (target_x > 2) then - target_x = 2 + if (mouse_moved) then + target_x = target_x + xrel / 100 + if (target_x < -2) then + target_x = -2 + elseif (target_x > 2) then + target_x = 2 + end end + mouse_moved = true end function drawFrameScore(width, height, frame, fs) @@ -148,9 +157,12 @@ end function update_overlay_event(width, height) drawScore(width, height) - if (game_state == "get_power" or game_state == "launched") then + if (game_state == "get_power" or game_state == "get_accuracy" or game_state == "launched") then drawPowerBar(width, height) end + if (game_state == "get_accuracy" or game_state == "launched") then + drawAccuracyBar(width, height) + end end function mousebutton_down_event(button, x, y) @@ -158,6 +170,8 @@ function mousebutton_down_event(button, x, y) if (game_state == "waiting") then getPower() elseif (game_state == "get_power") then + getAccuracy() + elseif (game_state == "get_accuracy") then launchBall() end end @@ -170,11 +184,26 @@ function getPower() power_direction = 1 end +function getAccuracy() + game_state = "get_accuracy" + accuracy = 0 + accuracy_last_time = ag.elapsedTime() + accuracy_direction = 1 +end + function launchBall() game_state = "launched" launch_time = ag.elapsedTime() bowling_ball = reference_ball:clone() - bowling_ball:setPosition(position_x, -10, 4) + bowling_ball:setPosition(position_x, -10, 1.31) + local fx = target_x + accuracy - position_x + local fy = 30 -- should match setCamera()'s dy + local d = math.sqrt(fx*fx + fy*fy) + fx = fx / d + fy = fy / d + local force = 200000 + force = force * 0.5 + force * 0.5 * power + bowling_ball:addForce(fx * force, fy * force, 0) end function drawPowerBar(width, height) @@ -197,3 +226,23 @@ function drawPowerBar(width, height) ag.fillRect(0.1, 1, 0.1, bar_width - 2, (bar_height - 2) * power, width - 50 - bar_width / 2, height / 2) end + +function drawAccuracyBar(width, height) + local bar_width = 400 + local bar_height = 60 + if (game_state == "get_accuracy") then + local now = ag.elapsedTime() + accuracy = accuracy + (now - accuracy_last_time) / 250 * accuracy_direction + if (accuracy > 1) then + accuracy = 1 + accuracy_direction = -1 + elseif (accuracy < -1) then + accuracy = -1 + accuracy_direction = 1 + end + accuracy_last_time = now + end + ag.drawRect(1, 1, 1, bar_width, bar_height, width / 2, 50 + bar_height / 2) + ag.drawLine(1, 1, 1, width / 2, 50, width / 2, 50 + bar_height) + ag.fillRect(0.1, 1, 0.1, 3, bar_height - 2, width / 2 + (bar_width - 4) / 2 * accuracy, 50 + bar_height / 2) +end