Permalink
Browse files

+ complete rewrite for a new game

  • Loading branch information...
floere committed May 16, 2009
1 parent 1047787 commit 48b43b5aad7d0070d7566a40cac382f5cb3958ef
View
68 init.rb
@@ -0,0 +1,68 @@
+## File: ChipmunkIntegration.rb
+## Author: Dirk Johnson
+## Version: 1.0.0
+## Date: 2007-10-05
+## License: Same as for Gosu (MIT)
+## Comments: Based on the Gosu Ruby Tutorial, but incorporating the Chipmunk Physics Engine
+## See http://code.google.com/p/gosu/wiki/RubyChipmunkIntegration for the accompanying text.
+
+require 'rubygems'
+require 'activesupport'
+require 'gosu'
+require 'chipmunk' # A physics framework.
+
+SCREEN_WIDTH = 1200
+SCREEN_HEIGHT = 700
+
+# The number of steps to process every Gosu update
+# The Player ship can get going so fast as to "move through" a
+# star without triggering a collision; an increased number of
+# Chipmunk step calls per update will effectively avoid this issue
+SUBSTEPS = 10
+
+require 'lib/numeric'
+require 'lib/zorder'
+require 'lib/scheduling'
+require 'lib/initializer_hooks'
+
+require 'lib/thing'
+require 'lib/targeting'
+require 'lib/moveable'
+require 'lib/turnable'
+require 'lib/accelerateable'
+require 'lib/earth_oriented'
+require 'lib/horizon_oriented'
+require 'lib/top_down_oriented'
+require 'lib/targetable'
+require 'lib/shooter'
+require 'lib/shot'
+require 'lib/hurting'
+require 'lib/generator'
+require 'lib/lives'
+require 'lib/mother_ship'
+require 'lib/child'
+require 'lib/waves'
+
+require 'lib/short_lived'
+require 'lib/controls'
+
+require 'lib/ambient/puff'
+require 'lib/ambient/small_explosion'
+
+require 'lib/units/gun'
+require 'lib/units/torpedo'
+
+require 'lib/units/player'
+require 'lib/units/first_mate'
+require 'lib/units/captain'
+require 'lib/units/admiral'
+
+require 'lib/units/enemy'
+require 'lib/units/cow'
+require 'lib/units/nuke_launcher'
+require 'lib/units/asteroid'
+require 'lib/units/bullet'
+require 'lib/units/earth'
+require 'lib/units/city'
+require 'lib/units/nuke'
+require 'lib/units/ray'
View
@@ -24,13 +24,15 @@ def init
@controls = []
@remove_shapes = []
@players = []
+ @waves = Waves.new self
@scheduling = Scheduling.new
+ @step = 0
@dt = 1.0 / 60.0
end
def setup_space
@space = CP::Space.new
- @space.damping = 0.8
+ @space.damping = 1.0 # no damping
end
def threaded time, code
@@ -40,22 +42,24 @@ def threaded time, code
def randomly_add type
thing = type.new self
- thing.warp_to rand*SCREEN_WIDTH, rand*SCREEN_HEIGHT
+ thing.warp_to SCREEN_WIDTH, rand*SCREEN_HEIGHT
- thing.put_on_surface
register thing
end
def setup_objects
- # register Earth.new(self)
- #
- # 7.times { randomly_add Cow }
- # 2.times { randomly_add NukeLauncher }
- # 3.times { randomly_add Gun }
+ wave 10, Enemy, 100
+ wave 10, Enemy, 400
+ wave 10, Enemy, 700
+ wave 10, Enemy, 1000
- add_player1
- add_player2
- add_player3
+ add_admiral
+ # add_captain
+ # add_first_mate
+ end
+
+ def wave amount, type, time
+ @waves.add amount, type, time
end
def small_explosion shape
@@ -66,12 +70,24 @@ def small_explosion shape
end
def setup_collisions
+ @space.add_collision_func :bullet, :bullet, &nil
+ @space.add_collision_func :bullet, :gun, &nil
+ @space.add_collision_func :bullet, :enemy do |bullet_shape, enemy_shape|
+ small_explosion bullet_shape
+ end
+ @space.add_collision_func :enemy, :explosion do |enemy_shape, explosion_shape|
+ @moveables.each { |enemy| enemy.shape == enemy_shape && enemy.lives -= 100 }
+ end
+
@space.add_collision_func :ship, :bullet, &nil
@space.add_collision_func :ship, :gun, &nil
@space.add_collision_func :ship, :nuke do |ship_shape, nuke_shape|
small_explosion nuke_shape
end
- @space.add_collision_func :ship, :ambient do |ship_shape, ambient_shape|
+ @space.add_collision_func :ship, :ambient do |_, _|
+ # just push it away
+ end
+ @space.add_collision_func :ship, :enemy do |_, _|
# just push it away
end
@@ -122,15 +138,15 @@ def remove shape
# Adds the first player.
#
- def add_player1
+ def add_admiral
@player1 = Admiral.new self
@player1.warp_to 150, 320 # move to the center of the window
@controls << Controls.new(self, @player1,
Gosu::Button::KbA => :left,
Gosu::Button::KbD => :right,
- Gosu::Button::KbW => :up,
- Gosu::Button::KbS => :down,
+ Gosu::Button::KbW => :full_speed_ahead,
+ Gosu::Button::KbS => :reverse,
Gosu::Button::KbLeftShift => :shoot,
Gosu::Button::Kb1 => :revive
)
@@ -142,7 +158,7 @@ def add_player1
# Adds the second player.
#
- def add_player2
+ def add_captain
@player2 = Captain.new self
@player2.warp_to 100, SCREEN_HEIGHT - 150
@@ -162,7 +178,7 @@ def add_player2
# Adds the third player.
#
- def add_player3
+ def add_first_mate
@player3 = FirstMate.new self
@player3.warp_to 50, 100 # move to the center of the window
@@ -226,8 +242,8 @@ def step_once
end
def targeting
- @moveables.select { |m| m.respond_to? :target }.each do |moveable|
- moveable.target @player2, @player3
+ @moveables.select { |m| m.respond_to? :target }.each do |gun|
+ gun.target *@moveables.select { |m| m.kind_of? Enemy }
end
end
@@ -239,6 +255,7 @@ def revive player
#
#
def update
+ @step += 1
# Step the physics environment SUBSTEPS times each update.
#
SUBSTEPS.times do
@@ -249,6 +266,7 @@ def update
handle_input
step_once
end
+ @waves.check @step
@scheduling.step
end
@@ -262,8 +280,8 @@ def draw_moveables
def draw_ui
@font.draw "P1 Score: #{@player1.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffff0000
- @font.draw "P2 Score: #{@player2.score}", SCREEN_WIDTH/2-50, 10, ZOrder::UI, 1.0, 1.0, 0xff00ff00
- @font.draw "P3 Score: #{@player3.score}", SCREEN_WIDTH-110, 10, ZOrder::UI, 1.0, 1.0, 0xff0000ff
+ # @font.draw "P2 Score: #{@player2.score}", SCREEN_WIDTH/2-50, 10, ZOrder::UI, 1.0, 1.0, 0xff00ff00
+ # @font.draw "P3 Score: #{@player3.score}", SCREEN_WIDTH-110, 10, ZOrder::UI, 1.0, 1.0, 0xff0000ff
end
def draw
View
@@ -0,0 +1,22 @@
+# def initialize
+# after_initialize
+# end
+#
+module InitializerHooks
+
+ mattr_accessor :hooks # { class => [blocks] }
+ self.hooks = {}
+
+ def after_initialize
+ hooks = InitializerHooks.hooks[self.class]
+ hooks && hooks.each do |hook|
+ self.instance_eval &hook
+ end
+ end
+
+ def self.register klass, hook
+ self.hooks[klass] ||= []
+ self.hooks[klass] << hook
+ end
+
+end
View
@@ -1,13 +1,47 @@
+# A thing is destroyed if a number of lives has been passed.
+#
module Lives
- def lives= lives
- @lives = lives
+ def self.included target_class
+ target_class.extend IncludeMethods
end
- def hit!
- @lives -= 1
- @score -= 1 if @score
- destroy if @lives <= 0
+ module IncludeMethods
+
+ def lives amount
+ include InstanceMethods
+ class_inheritable_accessor :prototype_lives
+ self.prototype_lives = amount
+
+ hook = lambda { self.lives = self.class.prototype_lives }
+ InitializerHooks.register self, hook
+ end
+
end
-end
+ module InstanceMethods
+
+ attr_accessor :lives
+
+ def hit!
+ self.lives -= 1
+ destroy if self.lives <= 0
+ end
+
+ end
+
+end
+
+# module Lives
+#
+# def lives= lives
+# @lives = lives
+# end
+#
+# def hit!
+# @lives -= 1
+# @score -= 1 if @score
+# destroy if @lives <= 0
+# end
+#
+# end
View
@@ -3,14 +3,31 @@
module MotherShip
def self.included target_class
- target_class.extend ClassMethods
+ target_class.extend IncludeMethods
end
- module ClassMethods
+ module IncludeMethods
def acts_as_mothership
include InstanceMethods
alias_method_chain :validate_position, :children
+ class_inheritable_accessor :prototype_children
+ extend ClassMethods
+ hook = lambda do
+ self.class.prototype_children.each do |type, x, y|
+ add_child type.new(window), x, y
+ end
+ end
+ InitializerHooks.register self, hook
+ end
+
+ end
+
+ module ClassMethods
+
+ def with type, x, y
+ self.prototype_children ||= []
+ self.prototype_children << [type, x, y]
end
end
@@ -23,6 +40,7 @@ def add_child child, x, y
self.children ||= []
child.extend Child
window.register child
+ child.rotation = self.rotation
child.relative_child_position = CP::Vec2.new(x, y)
self.children << child
end
View
@@ -39,7 +39,7 @@ def current_speed
# Directly set the rotation of our Moveable.
#
def rotation= rotation
- @shape.body.a = rotation
+ @shape.body.a = rotation % (2*Math::PI)
end
def rotation
@shape.body.a
View
@@ -1,5 +1,7 @@
class Thing
+ include InitializerHooks
+
attr_reader :window, :shape
def initialize window
Oops, something went wrong.

0 comments on commit 48b43b5

Please sign in to comment.