Skip to content

Instantly share code, notes, and snippets.

@shawn42
Created March 25, 2009 19:54
Show Gist options
  • Save shawn42/85672 to your computer and use it in GitHub Desktop.
Save shawn42/85672 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
$: << "#{File.dirname(__FILE__)}/../config"
require 'environment'
require 'gamebox'
if $0 == __FILE__
GameboxApp.run ARGV, ENV
end
require 'actor'
require 'animated_actor_view'
class BulletView < AnimatedActorView
end
class Bullet < Actor
has_behaviors :animated, :physical => {:shape => :circle,
:mass => 90,
:radius => 3}
attr_accessor :dir
def setup
@speed = 1
# bullets live for 1/2 second
@power = 500
end
def update(time)
@power -= time
if @power <= 0
remove_self
end
physical.body.apply_impulse(@dir*time*@speed, ZeroVec2) if physical.body.v.length < 400
end
end
require 'physical_level'
require 'physical_director'
require 'ship_director'
class DemoLevel < PhysicalLevel
def setup
@ship = @actor_factory.build :ship, self
@ship.warp vec2(300,300)
@ship_dir = ShipDirector.new
@rock_dir = PhysicalDirector.new
@score_dir = Director.new
@ship_dir.when :create_bullet do |ship|
bullet = @actor_factory.build :bullet, self
bullet.warp vec2(ship.x,ship.y)
bullet.body.a += ship.body.a
bullet.dir = vec2(ship.body.rot.x,ship.body.rot.y)
@ship_dir.add_actor bullet
end
@ship_dir.add_actor @ship
@directors << @rock_dir
@directors << @ship_dir
@directors << @score_dir
@score = @actor_factory.build :score, self
@score.x = 10
@score.y = 10
@score_dir.add_actor @score
@opts[:rocks].times do
rock = @actor_factory.build :rock, self
x,y = rand(400)+200,rand(300)+200
rock.warp vec2(x,y)
@rock_dir.add_actor rock
end
left_wall = @actor_factory.build :left_wall, self
top_wall = @actor_factory.build :top_wall, self
right_wall = @actor_factory.build :right_wall, self
bottom_wall = @actor_factory.build :bottom_wall, self
right_wall.warp vec2(1023,0)
bottom_wall.warp vec2(0,799)
# TODO get this from screen config
@width = 1024
@height = 800
# setup ship torusness
@space.add_collision_func(:ship, :left_wall) do |ship, wall|
ship.body.p = vec2(@width-ship.bb.r-ship.bb.l,ship.body.p.y)
end
@space.add_collision_func(:ship, :right_wall) do |ship, wall|
ship.body.p = vec2(ship.bb.r-ship.bb.l,ship.body.p.y)
end
@space.add_collision_func(:ship, :top_wall) do |ship, wall|
ship.body.p = vec2(ship.body.p.x,@height-ship.bb.b-ship.bb.t)
end
@space.add_collision_func(:ship, :bottom_wall) do |ship, wall|
ship.body.p = vec2(ship.body.p.x,ship.bb.t-ship.bb.b)
end
# setup rock torusness
@space.add_collision_func(:rock, :left_wall) do |rock, wall|
rock.body.p = vec2(@width-rock.bb.r-rock.bb.l,rock.body.p.y)
end
@space.add_collision_func(:rock, :right_wall) do |rock, wall|
rock.body.p = vec2(rock.bb.r-rock.bb.l,rock.body.p.y)
end
@space.add_collision_func(:rock, :top_wall) do |rock, wall|
rock.body.p = vec2(rock.body.p.x,@height-rock.bb.b-rock.bb.t)
end
@space.add_collision_func(:rock, :bottom_wall) do |rock, wall|
rock.body.p = vec2(rock.body.p.x,rock.bb.t-rock.bb.b)
end
# ship rock collision
@space.add_collision_func(:rock, :ship) do |rock, ship|
@ship_dir.find_physical_obj(ship).when :remove_me do
fire :restart_level
end
@ship_dir.remove_physical_obj ship
end
@space.add_collision_func(:rock, :bullet) do |rock, bullet|
@score += 10
@ship_dir.remove_physical_obj bullet
@rock_dir.remove_physical_obj rock
end
@stars = []
20.times { @stars << vec2(rand(@width),rand(@height)) }
end
def update(time)
update_physics time
for dir in @directors
dir.update time
end
if @rock_dir.empty?
@ship.when :remove_me do
fire :next_level
end
@ship.remove_self
end
end
def draw(target)
target.fill [25,25,25,255]
for star in @stars
target.draw_circle_s([star.x,star.y],1,[255,255,255,255])
end
end
end
require 'walls'
require 'ship'
require 'director'
class Game
constructor :wrapped_screen, :input_manager, :sound_manager,
:mode_manager
def setup
@sound_manager.play :current_rider
@mode_manager.change_mode_to :default
end
def update(time)
@mode_manager.update time
draw
end
def draw
@mode_manager.draw @wrapped_screen
@wrapped_screen.flip
end
end
require 'actor'
require 'animated_actor_view'
class RockView < AnimatedActorView
end
class Rock < Actor
has_behaviors :animated, :physical => {:shape => :circle,
:mass => 200,
:radius => 20}
def setup
@behaviors[:physical].body.a -= rand(10)
@speed = (rand(2)+1)/4.0
@turn_speed = rand(2)*0.00004
@dir = vec2(rand,rand)
end
def update(time)
physical.body.w += time*@turn_speed
physical.body.apply_impulse(@dir*time*@speed, ZeroVec2) if physical.body.v.length < 400
end
end
require 'actor'
require 'publisher'
require 'animated_actor_view'
class ShipView < AnimatedActorView
end
class Ship < Actor
can_fire :shoot
has_behaviors :animated, :physical => {:shape => :circle,
:mass => 500,
:radius => 10}
attr_accessor :moving_forward, :moving_back,
:moving_left, :moving_right
def setup
@speed = 1.1
@turn_speed = 0.0045
i = @input_manager
i.reg KeyDownEvent, K_SPACE do
shoot
end
i.reg KeyDownEvent, K_RCTRL, K_LCTRL do
warp vec2(rand(400)+100,rand(400)+100)
end
i.reg KeyDownEvent, K_LEFT do
@moving_left = true
end
i.reg KeyDownEvent, K_RIGHT do
@moving_right = true
end
i.reg KeyDownEvent, K_UP do
@moving_forward = true
self.action = :thrust
end
i.reg KeyUpEvent, K_LEFT do
@moving_left = false
end
i.reg KeyUpEvent, K_RIGHT do
@moving_right = false
end
i.reg KeyUpEvent, K_UP do
@moving_forward = false
self.action = :idle
end
end
def moving_forward?;@moving_forward;end
def moving_left?;@moving_left;end
def moving_right?;@moving_right;end
def update(time)
move_forward time if moving_forward?
move_left time if moving_left?
move_right time if moving_right?
end
def shoot
fire :shoot
end
def move_right(time)
physical.body.a += time*@turn_speed
physical.body.w += time*@turn_speed/5.0 if physical.body.w > 2.5
end
def move_left(time)
physical.body.a -= time*@turn_speed
physical.body.w -= time*@turn_speed/5.0 if physical.body.w > 2.5
end
def move_forward(time)
physical.body.apply_impulse(physical.body.rot*time*@speed, ZeroVec2) if physical.body.v.length < 400
end
end
require 'physical_director'
require 'publisher'
class ShipDirector < PhysicalDirector
extend Publisher
can_fire :create_bullet
def actor_added(actor)
if actor.is_a? Ship
actor.when :shoot do
# create bullet
fire :create_bullet, actor
end
end
end
end
require 'actor'
class LeftWall < Actor
has_behaviors :physical => {:shape => :poly,
:fixed => true,
:mass => 100,
:verts => [[0,0],[0,800],[1,800],[1,0]]}
end
class TopWall < Actor
has_behaviors :physical => {:shape => :poly,
:fixed => true,
:mass => 100,
:verts => [[0,0],[0,1],[1024,1],[1024,0]]}
end
class BottomWall < Actor
has_behaviors :physical => {:shape => :poly,
:fixed => true,
:mass => 100,
:verts => [[0,0],[0,1],[1024,1],[1024,0]]}
end
class RightWall < Actor
has_behaviors :physical => {:shape => :poly,
:fixed => true,
:mass => 100,
:verts => [[0,0],[0,800],[1,800],[1,0]]}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment