Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created February 27, 2019 21:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/0508016bd1d165138b708a9abc9a7b4e to your computer and use it in GitHub Desktop.
Save amirrajan/0508016bd1d165138b708a9abc9a7b4e to your computer and use it in GitHub Desktop.
Initial work for a subspace clone written on top of a game engine I'm working out that hasn't been released yet.
class Game
attr_accessor :_
def default_ship x, y, heading
_.new(:ship) do |s|
s.x = x
s.y = y
s.dy = 0
s.dx = 0
s.heading = heading
end
end
def defaults
_.particles ||= []
_.stars ||= 100.times.map do |i|
[rand * _.screen.w_half * [1, -1].sample,
rand * _.screen.h_half * [1, -1].sample, 1, 1, 255 * rand, 255, 255]
end
_.ship_1 ||= default_ship(-400, 0, 0)
_.ship_2 ||= default_ship( 400, 0, 180)
_.origin_x = 640
_.origin_y = 360
_.pos_y_direction = -1
_.pos_x_direction = 1
end
def render_background
_.solids << [-_.screen.w, -_.screen.h, _.screen.w * 2, _.screen.h * 2, 0, 0, 0]
_.solids += _.stars
end
def render_ship
_.sprites << ['ship_1.png', _.ship_1.x - 16, _.ship_1.y - 16, 33, 33, _.ship_1.heading]
_.sprites << ['ship_2.png', _.ship_2.x - 16, _.ship_2.y - 16, 33, 33, _.ship_2.heading]
end
def render_thrusts
_.solids += _.particles.map do |p|
[-1 + p.x, -1 + p.y, 3, 3, 255, 128, 0, 255 * p.lifetime.minus(p.__elapsed_ticks_since_creation__) / p.lifetime]
end
end
def calc_wrap_location location
location.x = _.screen.w_half * -1 if location.x > _.screen.w_half
location.x = _.screen.w_half if location.x < _.screen.w_half * -1
location.y = _.screen.h_half * -1 if location.y > _.screen.h_half
location.y = _.screen.h_half if location.y < _.screen.h_half * -1
end
def calc_thrusts
_.particles.each do |p|
p.speed *= 0.9
p.y += p.speed * p.heading.y_vector
p.x += p.speed * p.heading.x_vector
calc_wrap_location p
end
_.particles.reject! { |p| p.__elapsed_ticks_since_creation__ >= p.lifetime }
end
def calc_ship_for ship
ship.x += ship.dx
ship.y += ship.dy
calc_wrap_location ship
end
def calc_ship
calc_ship_for _.ship_1
calc_ship_for _.ship_2
end
def input_particles ship
_.particles << _.new(:ship_thruster) do |s|
s.heading = ship.heading + 180 + [1, -1].sample.mult(rand * 20)
s.speed = 10 * rand
s.lifetime = 30
s.x = ship.x
s.y = ship.y
end
end
def input_accelerate_ship should_move_ship, ship
if should_move_ship
input_particles ship
ship.dx += 0.025 * ship.heading.x_vector
ship.dy += 0.025 * ship.heading.y_vector
else
ship.dx *= 0.99
ship.dy *= 0.99
end
end
def input_accelerate
input_accelerate_ship _.player_1_r1_key_held, _.ship_1
input_accelerate_ship _.player_2_r1_key_held, _.ship_2
end
def input_turn_ship direction, ship
ship.heading += 2 * direction
end
def input_turn
input_turn_ship( 1, _.ship_1) if _.player_1_left_key_held
input_turn_ship(-1, _.ship_1) if _.player_1_right_key_held
input_turn_ship( 1, _.ship_2) if _.player_2_left_key_held
input_turn_ship(-1, _.ship_2) if _.player_2_right_key_held
end
def tick
defaults
render_background
render_ship
render_thrusts
calc_thrusts
calc_ship
input_accelerate
input_turn
end
end
$game = Game.new
def tick game_state
$game._ = game_state
$game.tick
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment