Permalink
Browse files

level parsing

  • Loading branch information...
1 parent b21e2a3 commit 510c654e075c48a46193bdcfce2b6bd77b5dcf06 @txus committed Jun 15, 2009
View
BIN .DS_Store
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
Binary file not shown.
@@ -52,6 +52,17 @@ def renew_shooting_pattern
shoot
end
end
+
+ def collides?(player)
+ if @x >= player.x and @x <= player.x + player.width then
+ player.warn
+ if @y >= player.y and @y <= player.y + player.height then
+ true
+ end
+ else
+ false
+ end
+ end
def hurt(damage)
@health -= damage
File renamed without changes.
File renamed without changes.
View
@@ -1,20 +1,22 @@
class EnemyGrid
attr_accessor :table, :rows, :cols
- def initialize(w,h,rows,cols, options = {})
+ @@levels = Hash.new
+ def initialize(window,w,h,rows,cols, options = {})
defaults = {:x_padding => 40, :y_padding => 30}
defaults.merge options
+ @window = window
@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
+ @level = 0
+ @@levels = parse_levels "levels.grd"
+ set_grid_for_level Settings.START_LEVEL.to_i
+
end
@@ -29,8 +31,16 @@ def <<(enemy)
end
end
- def distribution=(array)
-
+ def set_grid_for_level(num)
+ @level = num
+ reset_table
+ @@levels[num][:grid].each_with_index do |row,rindex|
+ row.each_with_index do |col,cindex|
+ position = [@x + (cindex * @cell_width), @y + (rindex * @cell_height)]
+ @table[rindex][cindex] = eval(Settings.send(col) + '.new(@window, position)') if col
+ end
+ end
+ show
end
def show
@@ -53,6 +63,7 @@ def next_available_position
free_cell = false
@table.each_with_index do |row, rindex|
row.each_with_index do |column, cindex|
+ puts column
if column.nil? then
free_cell = true
return [@x + (cindex * @cell_width), @y + (rindex * @cell_height)]
@@ -61,6 +72,61 @@ def next_available_position
end
raise NoFreeCellException if free_cell == false
end
+
+ private
+
+ def reset_table
+ @rows = @@levels[@level][:rows]
+ @cols = @@levels[@level][: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
+ #@table.each_with_index do |row, rindex|
+ # row.each_with_index do |column, cindex|
+ # if not column.nil? and column.is_a? Enemy then
+ # column.destroy
+ # column = nil
+ # end
+ # end
+ #end
+ end
+
+ def parse_levels(filename)
+ f = File.open(filename, "r")
+ lvl = nil
+ rows = cols = 0
+ arr = Hash.new
+ level = Array.new
+ f.each do |line|
+ if line.match /Level (\d+)/ then
+ lvl = $1
+ level = Array.new
+ rows = cols = 0
+ end
+ if line.match /(n|f)+/ then
+ types = Array.new
+ rows += 1
+ chars = line.split(" ")
+ cols = chars.length unless chars.length < cols
+ chars.each_with_index do |ch, index|
+ case ch
+ when '0':
+ types << nil
+ else
+ types << ch
+ end
+ end
+ level << types
+ end
+ arr[lvl.to_i] = {:grid => level, :cols => cols, :rows => rows}
+ end
+ return arr
+ end
end
View
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -14,24 +14,24 @@ def initialize(w = 640, h = 480)
@player.place :center
@background = Gosu::Image.new(self, "media/menu.png", true)
- @grid = EnemyGrid.new(@width, @height, 6, 15)
+ @grid = EnemyGrid.new(self,@width, @height, 2, 8)
@screen = :game
initialize_events
- @grid.cols.times do |index|
- @grid << NormalEnemy.new(self, @grid.next_available_position)
- end
- @grid.cols.times do |index|
- @grid << FireEnemy.new(self, @grid.next_available_position)
- end
- @grid.cols.times do |index|
- @grid << NormalEnemy.new(self, @grid.next_available_position)
- end
- @grid.cols.times do |index|
- @grid << FireEnemy.new(self, @grid.next_available_position)
- end
+ # @grid.cols.times do |index|
+ # @grid << NormalEnemy.new(self, @grid.next_available_position)
+ # end
+ # @grid.cols.times do |index|
+ # @grid << FireEnemy.new(self, @grid.next_available_position)
+ # end
+ # @grid.cols.times do |index|
+ # @grid << NormalEnemy.new(self, @grid.next_available_position)
+ # end
+ # @grid.cols.times do |index|
+ # @grid << FireEnemy.new(self, @grid.next_available_position)
+ # end
end
@@ -50,7 +50,8 @@ def update
@player.move :left
elsif button_down? Gosu::Button::KbRight then
@player.move :right
- elsif button_down? Gosu::Button::KbSpace then
+ end
+ if button_down? Gosu::Button::KbSpace then
@player.shoot
end
else
@@ -93,6 +94,9 @@ def check_collisions
bonus.destroy
end
end
+ Enemy.all.each do |enemy|
+ @player.die if enemy.collides?(@player)
+ end
end
def initialize_events
View
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
47 init.rb
@@ -1,35 +1,40 @@
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/event'
-require 'classes/random_event'
-require 'classes/scheduled_event'
require 'classes/enemy_grid'
-require 'classes/perk'
-require 'classes/default_perk'
-require 'classes/double_shot_perk'
-require 'classes/triple_shot_perk'
-require 'classes/double_speed_perk'
-require 'classes/bullet'
-require 'classes/enemy_bullet'
-require 'classes/enemy_fire_bullet'
-require 'classes/player_bullet'
-require 'classes/normal_bullet'
-require 'classes/enemy'
-require 'classes/normal_enemy'
-require 'classes/fire_enemy'
-require 'classes/bonus'
-require 'classes/double_shot_bonus'
-require 'classes/triple_shot_bonus'
-require 'classes/double_speed_bonus'
-
require 'classes/score'
+require 'classes/event/event'
+require 'classes/event/random_event'
+require 'classes/event/scheduled_event'
+
+require 'classes/perk/perk'
+require 'classes/perk/default_perk'
+require 'classes/perk/double_shot_perk'
+require 'classes/perk/triple_shot_perk'
+require 'classes/perk/double_speed_perk'
+
+require 'classes/bullet/bullet'
+require 'classes/bullet/enemy_bullet'
+require 'classes/bullet/enemy_fire_bullet'
+require 'classes/bullet/player_bullet'
+require 'classes/bullet/normal_bullet'
+
+require 'classes/enemy/enemy'
+require 'classes/enemy/normal_enemy'
+require 'classes/enemy/fire_enemy'
+
+require 'classes/bonus/bonus'
+require 'classes/bonus/double_shot_bonus'
+require 'classes/bonus/triple_shot_bonus'
+require 'classes/bonus/double_speed_bonus'
+
window = GameWindow.new
window.show
View
@@ -0,0 +1,13 @@
+#Level 1#
+f f f n n n n n f f f
+n 0 n 0 n 0 n 0 n 0 n
+f f f f f f f f f f f
+n n n n n n n n n n n
+f f f n n n n n f f f
+n 0 n 0 n 0 n 0 n 0 n
+
+#Level 2#
+n f f f
+n 0 n
+f f f f
+n n n n
View
@@ -11,6 +11,10 @@
<name>LOGGER_VERBOSE</name>
<value>false</value>
</setting>
+ <setting>
+ <name>START_LEVEL</name>
+ <value>1</value>
+ </setting>
<setting>
<name>n</name>
<value>NormalEnemy</value>

0 comments on commit 510c654

Please sign in to comment.