Created
August 21, 2019 13:03
-
-
Save kobaltz/1deb31251c82797686cd9718fd5e48ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def tick(_args) | |
initialize_game | |
display_hud | |
set_score | |
move_cubes | |
check_collision_with_mouse | |
end | |
def reset_game | |
args.state.cubes = [cube] | |
args.state.time = 0 | |
args.state.last_split = 0 | |
end | |
def mode | |
@mode ||= { | |
size: 35, | |
speed: 0.05, | |
generate: 1 | |
} | |
end | |
def initialize_game | |
args.state.time ||= 0 | |
args.state.high_score ||= 0 | |
args.state.cubes ||= [cube] | |
args.state.last_split ||= 0 | |
end | |
def cube | |
{ | |
x: 1280 * rand, | |
y: 720 * rand, | |
x_direction: :forward, | |
y_direction: :climb, | |
w: mode[:size], | |
h: mode[:size], | |
speed: mode[:speed] | |
} | |
end | |
def display_hud | |
args.state.time += 1/60 | |
args.outputs.labels << [10, 700, "Time: #{args.state.time.to_i}"] | |
args.outputs.labels << [10, 680, "# Cubes: #{args.state.cubes.size}"] | |
args.outputs.labels << [10, 660, "# High Score: #{args.state.high_score}"] | |
end | |
def set_score | |
args.state.high_score = [args.state.high_score, args.state.time.to_i].max | |
end | |
def move_cubes | |
args.outputs.solids << args.state.cubes.map do |hash| | |
calculate_cube_movement(hash) | |
[hash[:x], hash[:y], hash[:w], hash[:h]] | |
end | |
end | |
def calculate_cube_movement(hash) | |
hash[:x_direction] = :forward if hash[:x] < 0 | |
hash[:x_direction] = :backward if hash[:x] > 1280 - hash[:w] | |
hash[:y_direction] = :climb if hash[:y] < 0 | |
hash[:y_direction] = :fall if hash[:y] > 720 - hash[:h] | |
hash[:x] += hash[:speed] * (hash[:x_direction] == :forward ? 1 : -1) | |
hash[:y] += hash[:speed] * (hash[:y_direction] == :climb ? 1 : -1) | |
end | |
def check_collision_with_mouse | |
args.state.cubes.map do |hash| | |
if ((args.inputs.mouse.x > hash[:x]) && (args.inputs.mouse.y > hash[:y])) && | |
((args.inputs.mouse.x < hash[:x] + hash[:w]) && (args.inputs.mouse.y < hash[:y] + hash[:h])) | |
reset_game | |
else | |
hash[:speed] = [hash[:speed] + 0.05, 10].min | |
split_cube if args.state.time.to_i % mode[:generate] == 0 | |
end | |
end | |
end | |
def split_cube | |
return if args.state.time - args.state.last_split < mode[:generate] + 1 | |
args.state.cubes << cube | |
args.state.last_split = args.state.time | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment