-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scheduled_events, enemy_grid, some exceptions
- Loading branch information
x
committed
May 28, 2009
1 parent
99c4a99
commit f7f350a
Showing
10 changed files
with
115 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ def initialize(window,x,y) | |
private | ||
|
||
def speed | ||
5 | ||
1 | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Event | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class NoFreeCellException < Exception | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters