Player stuff

pull/1/head
E. Almqvist 3 years ago
parent 6da12198da
commit f067a830d2
  1. 45
      app.rb
  2. 75
      physobj.rb

@ -3,8 +3,8 @@ require "gosu"
load "physobj.rb"
class Window < Gosu::Window
attr_accessor :freeze, :caption, :physobjs, :planets
attr_reader :width, :height
attr_accessor :freeze, :caption, :physobjs, :planets, :controller
attr_reader :width, :height, :fonts
def initialize(title, width, height, physobjs = [], planets = [])
super width, height
@ -14,19 +14,38 @@ class Window < Gosu::Window
@physobjs = physobjs
@planets = planets
@font = Gosu::Font.new(self, Gosu::default_font_name, 12)
@font2 = Gosu::Font.new(self, Gosu::default_font_name, 18)
@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
}
@freeze = false
@controller = nil
end
def button_up(id)
super id
if( @controller != nil ) then
@controller.button_up(id)
end
if( id == Gosu::KbEscape ) then
@freeze = !@freeze
end
end
def button_down(id)
super id
if( @controller != nil ) then
@controller.button_down(id)
end
end
def update
if( !@freeze ) then
@physobjs.each do |obj|
@ -40,20 +59,18 @@ class Window < Gosu::Window
end
private def generate_debug_string(obj)
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"
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"
end
def draw
status_text = @freeze ? "FROZEN (Escape to unfreeze)" : "(Escape to freeze)"
@font2.draw(status_text, 0, 0, 1, 1.0, 1.0, Gosu::Color::WHITE)
@font2.draw("Frozen: #{@freeze}", 0, 0, 1, 1.0, 1.0, Gosu::Color::WHITE)
@physobjs.each do |obj|
obj.render
obj.draw_vector(obj.vel, 10)
obj.draw_vector(obj.accel, 500, 0xff_aaffaa)
obj.render_path
@font.draw(self.generate_debug_string(obj), obj.pos[0], obj.pos[1], 1, 1.0, 1.0, Gosu::Color.argb(0xee_aaaaff))
obj.draw_direction
end
@planets.each do |planet|
@ -67,14 +84,18 @@ window = Window.new("Physics!", 1600, 900)
planet = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 20, 120)
planet.pos = Vector[800, 450]
planet.show_info = true
cube = PhysCube.new("Cube", window, 8, 8)
cube = Player.new("Alpha", window, 8, 8)
cube.show_info = true
cube.pos = Vector[800, 450 + 200]
cube.vel = Vector[2.5, 0]
window.controller = cube
cube2 = PhysCube.new("Cube2", window, 8, 8)
cube2.pos = Vector[800, 450 - 200]
cube2 = PhysCube.new("Beta", window, 8, 8)
cube2.pos = Vector[800, 450 + 300]
cube2.vel = Vector[-2.5, 0]
planet.orbit([cube, cube2])
window.planets << planet

@ -4,7 +4,7 @@ load "gosu_plugin.rb"
GRAV_CONSTANT = 1e+1
class PhysObj
attr_accessor :world, :saved_pos, :pos, :vel, :accel, :x, :y
attr_accessor :world, :saved_pos, :pos, :vel, :accel, :x, :y, :show_info, :angle
attr_reader :name
def initialize(name, world)
@name = name
@ -14,6 +14,8 @@ class PhysObj
@accel = Vector.zero(2)
@x, @y = 0, 0
@saved_pos = []
@show_info = false
@angle = 0
end
def tick
@ -34,26 +36,14 @@ class PhysObj
end
end
end
class PhysCube < PhysObj
attr_reader :width, :height
def initialize(name, world, width, height, color=0xff_ffffff, ela=-0.8)
super name, world
@width, @height = width, height
@color = Gosu::Color.argb(color)
@ela = ela
end
def render
x, y = self.pos[0], self.pos[1]
Gosu.draw_quad(x, y, @color, x + self.width, y, @color, x, y + self.height, @color, x + self.width, y + self.height, @color)
private def debug_string
return "\n#{self.name}\nVel: #{self.vel.magnitude.round(1)} #{self.vel.round(4)}\nAccel: #{self.accel.magnitude.round(4)} #{self.accel.round(4)}\nPos: #{self.pos.round(4)}\nAngle: #{self.angle.round(1)} deg\n"
end
def physics
self.tick
def render(x_offset=0, y_offset=0, color=Gosu::Color.argb(0xaa_2222ff))
if( @show_info ) then
self.world.fonts[:normal].draw(self.debug_string, self.pos[0] + x_offset, self.pos[1] + y_offset, 1, 1.0, 1.0, color)
end
end
def draw_vector(vec, scale=2, color=0xaf_ffaaaa)
@ -75,6 +65,32 @@ class PhysCube < PhysObj
end
end
def draw_direction
rads = (self.angle * Math::PI) / 180
dir_vec = Vector[Math::cos(rads), Math::sin(rads)]
self.draw_vector(dir_vec, 25, 0xaf_aaaaff)
end
end
class PhysCube < PhysObj
attr_reader :width, :height
def initialize(name, world, width, height, color=0xff_ffffff)
super name, world
@width, @height = width, height
@color = Gosu::Color.argb(color)
end
def render
super 0, 0, @color
x, y = self.pos[0], self.pos[1]
Gosu.draw_quad(x, y, @color, x + self.width, y, @color, x, y + self.height, @color, x + self.width, y + self.height, @color)
end
def physics
self.tick
end
end
@ -107,7 +123,28 @@ class Planet < PhysObj
end
end
private def debug_string
return "\n#{self.name}\nPos: #{self.pos.round(4)}\nMass: #{self.mass.round(4)}\nRadius: #{self.radius.round(4)} p\nGravity: #{(self.mass*GRAV_CONSTANT).round(2)} p/r^2"
end
def render
super @radius*3, @radius*3, Gosu::Color.argb(0xff_ffffff)
Gosu.draw_circle(self.pos[0], self.pos[1], @radius, @color, 0, @circle_thickness)
end
end
class Player < PhysCube
def initialize(name, world, width, height, color=0xff_aaaaaa)
super name, world, width, height, color
end
def button_up(id)
end
def button_down(id)
if( id == Gosu::KbDown ) then
puts("DOWN")
end
end
end

Loading…
Cancel
Save