Skip to content

Commit

Permalink
scheduled_events, enemy_grid, some exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
x committed May 28, 2009
1 parent 99c4a99 commit f7f350a
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 16 deletions.
2 changes: 1 addition & 1 deletion classes/double_shot_bonus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(window,x,y)
private

def speed
5
1
end

end
Expand Down
4 changes: 2 additions & 2 deletions classes/enemy.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Enemy
attr_reader :x, :y
@@enemies = Array.new
def initialize(window,x,y)
def initialize(window,x_y)
@width, @height = 20,20
@x, @y = x, y
@x, @y = x_y[0], x_y[1]
@window = window
@img = nil
@pattern = nil
Expand Down
62 changes: 62 additions & 0 deletions classes/enemy_grid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class EnemyGrid
attr_accessor :table
def initialize(w,h,rows,cols, options = {})
defaults = {:x_padding => 40, :y_padding => 30}
defaults.merge options
@x, @y = defaults[:x_padding], defaults[:y_padding]
@width = w - (defaults[:x_padding] * 2)
@height = (h - defaults[:y_padding]) * 0.5
@rows, @cols = rows, cols

@cell_width = @width / cols
@cell_height = @height / rows

@table = Array.new(rows)
@table.each_with_index do |row,rindex|
@table[rindex] = Array.new(cols)
end

end

def <<(enemy)
@table.each_with_index do |row, rindex|
row.each_with_index do |column, cindex|
if column.nil? then
@table[rindex][cindex] = enemy
return
end
end
end
end

def show
out = ""
@table.each_with_index do |row, rindex|
out += "#{rindex} | "
row.each_with_index do |column, cindex|
if @table[rindex][cindex].nil? then
out += " | "
else
out += "x | "
end
end
out += "\n"
end
puts out
end

def next_available_position
free_cell = false
@table.each_with_index do |row, rindex|
row.each_with_index do |column, cindex|
if column.nil? then
free_cell = true
return [@x + (cindex * @cell_width), @y + (rindex * @cell_height)]
end
end
end
raise NoFreeCellException if free_cell == false
end

end

4 changes: 4 additions & 0 deletions classes/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Event

end

4 changes: 4 additions & 0 deletions classes/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class NoFreeCellException < Exception

end

18 changes: 10 additions & 8 deletions classes/game_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ def initialize(w = 640, h = 480)
@player.place :center
@background = Gosu::Image.new(self, "media/menu.png", true)

a = NormalEnemy.new(self,50,180)
@grid = EnemyGrid.new(@width, @height, 6, 15)

b = NormalEnemy.new(self,200,190)

b.shoot
@screen = :game

a.shoot
double_shot_bonus_event = ScheduledEvent.new(20) do
DoubleShotBonus.new(self,rand * @width, -10)
end

@screen = :game
create_enemies_element = ScheduledEvent.new(0.2) do
@grid << NormalEnemy.new(self, @grid.next_available_position)
end

#bonus_caller = RandomCaller.new(10) do

#end

end

Expand Down Expand Up @@ -54,6 +54,8 @@ def update
close
end

ScheduledEvent.call_all

Bullet.move_all
Enemy.move_all
Bonus.move_all
Expand Down
2 changes: 1 addition & 1 deletion classes/normal_enemy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class NormalEnemy < Enemy
def initialize(window,x,y)
def initialize(window,x_y)
super
@width = 26
@height = 23
Expand Down
6 changes: 3 additions & 3 deletions classes/random_event.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class RandomEvent
class RandomEvent < Event
@@random_events = Array.new
def initialize (pps, &block)
def initialize (ppm, &block)
@ppm = ppm
@block = block
@@random_events << self
end

def call
yield @block if (rand * 60).to_i < @ppm
@block.call if (rand * 10000).to_i < @ppm
end

def self.call_all
Expand Down
23 changes: 23 additions & 0 deletions classes/scheduled_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ScheduledEvent < Event
@@scheduled_events = Array.new
def initialize (seconds, &block)
@seconds = seconds
@block = block
@last_called = Gosu::milliseconds
@@scheduled_events << self
end

def call
if (Gosu::milliseconds - @last_called) >= (@seconds * 1000) then
@block.call
@last_called = Gosu::milliseconds
end
end

def self.call_all
@@scheduled_events.each do |scheduled_event|
scheduled_event.call
end
end
end

6 changes: 5 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require 'rubygems'
require 'gosu'
require 'rexml/document'
require 'classes/exceptions'
require 'classes/player'
require 'classes/settings'
require 'classes/game_window'
require 'classes/logger'
require 'classes/random_caller'
require 'classes/event'
require 'classes/random_event'
require 'classes/scheduled_event'
require 'classes/enemy_grid'
require 'classes/perk'
require 'classes/default_perk'
require 'classes/bullet'
Expand Down

0 comments on commit f7f350a

Please sign in to comment.