Resources

Summary

# Terminal
./dragonruby drifting_squares
./dragonruby-publish drifting_squares

# metadata/game_metadata.txt
devid=kobaltz
devtitle=kobaltz
gameid=driftingsquares
gametitle=Squares
version=0.1
icon=metadata/icon.png

# app/main.rb
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