Class: MazePuzzle::Player
- Inherits:
-
Object
- Object
- MazePuzzle::Player
- Defined in:
- lib/games_paradise/gui/gosu/maze_puzzle/player.rb
Instance Attribute Summary collapse
-
#cell_index_x ⇒ Object
Returns the value of attribute cell_index_x.
-
#cell_index_y ⇒ Object
Returns the value of attribute cell_index_y.
-
#is_moving ⇒ Object
Returns the value of attribute is_moving.
-
#path ⇒ Object
Returns the value of attribute path.
-
#target_x ⇒ Object
Returns the value of attribute target_x.
-
#target_y ⇒ Object
Returns the value of attribute target_y.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #check_for_path(ignored_path) ⇒ Object
-
#draw ⇒ Object
draw.
-
#initialize(cell_index_x, cell_index_y, color) ⇒ Player
constructor
A new instance of Player.
-
#move ⇒ Object
move.
- #set_status(path) ⇒ Object
- #set_target ⇒ Object
Constructor Details
#initialize(cell_index_x, cell_index_y, color) ⇒ Player
Returns a new instance of Player.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 8 def initialize(cell_index_x, cell_index_y, color) @color = color @cell_index_x = cell_index_x @cell_index_y = cell_index_y @is_moving = false @path = nil set_target @x = @target_x @y = @target_y end |
Instance Attribute Details
#cell_index_x ⇒ Object
Returns the value of attribute cell_index_x.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def cell_index_x @cell_index_x end |
#cell_index_y ⇒ Object
Returns the value of attribute cell_index_y.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def cell_index_y @cell_index_y end |
#is_moving ⇒ Object
Returns the value of attribute is_moving.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def is_moving @is_moving end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def path @path end |
#target_x ⇒ Object
Returns the value of attribute target_x.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def target_x @target_x end |
#target_y ⇒ Object
Returns the value of attribute target_y.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def target_y @target_y end |
#x ⇒ Object
Returns the value of attribute x.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
7 8 9 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 7 def y @y end |
Instance Method Details
#check_for_path(ignored_path) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 24 def check_for_path(ignored_path) path = nil # I store all the cell in an one dimensional array (cells) # cells[i + j * cols] is the way to refer to it's item $cells[@cell_index_x + @cell_index_y * $cols].walls.each_with_index do |wall, i| if !wall and i != ignored_path if path == nil path = i else path = nil break end end end return path end |
#draw ⇒ Object
draw
97 98 99 100 101 102 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 97 def draw draw_quad @x, @y, @color, @x + $player_size, @y, @color, @x + $player_size, @y + $player_size, @color, @x, @y + $player_size, @color end |
#move ⇒ Object
move
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 57 def move if @is_moving if @x == @target_x && @y == @target_y # check for available path, # and ignore the the opposite directions of the LAST path # cuz you don't wanna go back where you just left @path = check_for_path(@path == 0 ? 2: @path == 1 ? 3: @path == 2 ? 0: 1) if @path != nil # set new player's cell index depend on "current @path" set_status(@path) set_target else # no "available" path found, player stop moving @is_moving = false end else # target's position is different than curent position, # move to the target if @x < @target_x @x += $speed_per_tick elsif @x > @target_x @x -= $speed_per_tick end if @y < @target_y @y += $speed_per_tick elsif @y > @target_y @y -= $speed_per_tick end end end end |
#set_status(path) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 42 def set_status(path) @path = path if @path == 0 @cell_index_y -= 1 elsif @path == 1 @cell_index_x += 1 elsif @path == 2 @cell_index_y += 1 else @cell_index_x -= 1 end set_target end |
#set_target ⇒ Object
19 20 21 22 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/player.rb', line 19 def set_target @target_x = (@cell_index_x * $cell_size) + $cell_size/2 - $player_size/2 @target_y = (@cell_index_y * $cell_size) + $cell_size/2 - $player_size/2 end |