bouncing/app/main.rb

159 lines
3.4 KiB
Ruby
Raw Normal View History

2023-11-26 16:21:32 +01:00
require "app/bounce.rb"
BOUNCE_ACCL = 1.02
2023-11-26 17:58:07 +01:00
GRAVITY = 0.5
MIN_GRAVITY = 0.1
MAX_GRAVITY = 2.0
2023-11-26 18:19:58 +01:00
STEP_GRAVITY = 0.02
2023-11-26 17:58:07 +01:00
MIN_BOUNCE = 1.0
MAX_BOUNCE = 1.1
2023-11-26 18:19:58 +01:00
STEP_BOUNCE = 0.0011
2023-11-26 17:58:07 +01:00
2023-11-26 16:21:32 +01:00
CX = 636
CY = 399
CR = 205
def tick args
# INIT
# circle
cx = CX
cy = CY
cr = CR
# pause
2023-11-26 17:58:07 +01:00
if args.state.paused.nil?
args.state.paused = true
2023-11-26 16:21:32 +01:00
args.state.ball = {
x: 500,
y: 500,
}
args.state.ball_vector = {
x: 0,
y: 0
}
2023-11-26 17:58:07 +01:00
args.state.bounce = BOUNCE_ACCL
args.state.gravity = GRAVITY
2023-11-26 16:21:32 +01:00
end
# INPUT
if args.inputs.keyboard.key_up.space
args.state.paused = !args.state.paused
2023-11-26 17:58:07 +01:00
if !args.state.paused
#REINIT with inputs
args.state.ball = {
x: 500+Math.rand(150),
y: 400+Math.rand(50),
}
args.state.ball_vector = {
x: 0,
y: 0
}
end
2023-11-26 16:21:32 +01:00
end
2023-11-26 18:19:58 +01:00
# MENU CONTROLS
if args.inputs.right && args.state.paused
args.state.gravity += STEP_GRAVITY if args.state.gravity < MAX_GRAVITY
elsif args.inputs.left && args.state.paused
args.state.gravity -= STEP_GRAVITY if args.state.gravity > MIN_GRAVITY
end
2023-11-26 16:21:32 +01:00
2023-11-26 18:19:58 +01:00
if args.inputs.up && args.state.paused
args.state.bounce += STEP_BOUNCE if args.state.bounce < MAX_BOUNCE
elsif args.inputs.down && args.state.paused
args.state.bounce -= STEP_BOUNCE if args.state.bounce > MIN_BOUNCE
end
2023-11-26 16:21:32 +01:00
# LOOP
if !args.state.paused
#ball vector
move_x = args.state.ball_vector[:x]
move_y = args.state.ball_vector[:y]
# apply gravity
2023-11-26 18:19:58 +01:00
move_y = move_y - args.state.gravity
2023-11-26 16:21:32 +01:00
args.state.ball_vector[:x] = move_x
args.state.ball_vector[:y] = move_y
# bounce
x = args.state.ball[:x]
y = args.state.ball[:y]
new_x = x + move_x
new_y = y + move_y
if bouncing?(new_x,new_y,cx,cy,cr)
new_move_x, new_move_y = reflection_vector(x,y,cx,cy,move_x,move_y)
2023-11-26 18:19:58 +01:00
args.state.ball_vector[:x] = new_move_x * args.state.bounce
args.state.ball_vector[:y] = new_move_y * args.state.bounce
2023-11-26 17:58:07 +01:00
# play sound
sound = Math.rand(3)+1
args.outputs.sounds << "sounds/bounce#{sound}.wav"
2023-11-26 16:21:32 +01:00
end
# move the ball
move_x = args.state.ball_vector[:x]
move_y = args.state.ball_vector[:y]
args.state.ball[:x] = args.state.ball[:x] + move_x
args.state.ball[:y] = args.state.ball[:y] + move_y
end
# END?
if !args.state.paused
if too_fast?(move_x, move_y, cr)
#RESET state and pause
args.state.ball = {
x: 500,
y: 500,
}
args.state.ball_vector = {
x: 0,
y: 0
}
args.state.paused = true
end
end
# RENDER
#
# BG
2023-11-26 17:58:07 +01:00
bg = args.state.paused ? 'sprites/BG1_menu.png' : 'sprites/BG1.png'
2023-11-26 16:21:32 +01:00
args.outputs.sprites << {
x: 0,
y: 0,
w: 1280,
h: 720,
2023-11-26 17:58:07 +01:00
path: bg }
2023-11-26 16:21:32 +01:00
if args.state.paused
2023-11-26 17:58:07 +01:00
# Render menu
2023-11-26 18:19:58 +01:00
args.outputs.labels << [640, 40, "Press <space> to start the game", 6, 1]
2023-11-26 17:58:07 +01:00
# min_x = 440
# mid = 615
# max_x = 780
2023-11-26 18:19:58 +01:00
# l = 340
gravity_x = 440 + args.state.gravity * 179
2023-11-26 17:58:07 +01:00
args.outputs.sprites << {
2023-11-26 18:19:58 +01:00
x: gravity_x,
2023-11-26 17:58:07 +01:00
y: 445,
w: 32,
h: 32,
path: 'sprites/slider.png'
}
2023-11-26 18:19:58 +01:00
bounce_x = 440 + (args.state.bounce - 1)*3400
2023-11-26 17:58:07 +01:00
args.outputs.sprites << {
2023-11-26 18:19:58 +01:00
x: bounce_x,
2023-11-26 17:58:07 +01:00
y: 325,
w: 32,
h: 32,
path: 'sprites/slider.png'
}
2023-11-26 16:21:32 +01:00
end
# BALL
2023-11-26 17:58:07 +01:00
2023-11-26 16:21:32 +01:00
args.outputs.sprites << {
x: args.state.ball[:x]-16,
y: args.state.ball[:y]-16,
w: 32,
h: 32,
2023-11-26 17:58:07 +01:00
path: 'sprites/ball.png' } if !args.state.paused
2023-11-26 16:21:32 +01:00
end