Hohmann Miner, a sandbox physics game where you mine stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hohmann-miner/app.rb

106 lines
2.1 KiB

3 years ago
#!/usr/bin/ruby -w
require "gosu"
load "physobj.rb"
class Window < Gosu::Window
3 years ago
attr_accessor :freeze, :caption, :physobjs, :planets, :controller
attr_reader :width, :height, :fonts
3 years ago
3 years ago
def initialize(title, width, height, physobjs = [], planets = [])
3 years ago
super width, height
@width, @height = width, height
self.caption = "#{title}| #{width}x#{height}"
@physobjs = physobjs
3 years ago
@planets = planets
3 years ago
3 years ago
@font = Gosu::Font.new(self, Gosu::default_font_name, 14)
@font2 = Gosu::Font.new(self, Gosu::default_font_name, 20)
@fonts = {
normal: @font,
big: @font2
}
3 years ago
@freeze = false
3 years ago
@controller = nil
3 years ago
end
3 years ago
def button_up(id)
super id
3 years ago
if( @controller != nil ) then
@controller.button_up(id)
end
3 years ago
if( id == Gosu::KbEscape ) then
@freeze = !@freeze
3 years ago
end
3 years ago
end
3 years ago
3 years ago
def button_down(id)
super id
if( @controller != nil ) then
@controller.button_down(id)
end
end
3 years ago
def update
if( !@freeze ) then
@physobjs.each do |obj|
obj.physics
end
@planets.each do |planet|
planet.orbit(@physobjs)
end
3 years ago
end
3 years ago
end
3 years ago
private def generate_debug_string(obj)
3 years ago
return "\n#{obj.name}\nVel: #{obj.vel.round(4)} (#{obj.vel.magnitude.round(1)})\nAccel: #{obj.accel.round(4)} (#{obj.accel.magnitude.round(4)})\nPos: #{obj.pos.round(4)}\n"
3 years ago
end
3 years ago
def draw
3 years ago
@font2.draw("Frozen: #{@freeze}", 0, 0, 1, 1.0, 1.0, Gosu::Color::WHITE)
3 years ago
3 years ago
@physobjs.each do |obj|
obj.render
3 years ago
obj.draw_vector(obj.vel, 10)
obj.draw_vector(obj.accel, 500, 0xff_aaffaa)
3 years ago
obj.render_path
3 years ago
obj.draw_direction
3 years ago
end
3 years ago
@planets.each do |planet|
planet.render
end
3 years ago
end
end
window = Window.new("Physics!", 1600, 900)
3 years ago
planet = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 20, 120)
3 years ago
planet.pos = Vector[800, 450]
3 years ago
planet.show_info = true
3 years ago
3 years ago
cube = Player.new("Alpha", window, 8, 8)
cube.show_info = true
3 years ago
cube.thrust = 0.0075
3 years ago
cube.pos = Vector[800, 450 + 200]
cube.vel = Vector[2.5, 0]
3 years ago
window.controller = cube
3 years ago
3 years ago
cube2 = PhysCube.new("Beta", window, 8, 8)
cube2.pos = Vector[800, 450 + 300]
3 years ago
cube2.vel = Vector[-2.5, 0]
3 years ago
3 years ago
planet.orbit([cube, cube2])
3 years ago
3 years ago
window.planets << planet
3 years ago
window.physobjs << cube
3 years ago
window.physobjs << cube2
3 years ago
window.show