Class: GamesAndRpgParadise::MazePuzzle::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cell_index_x, cell_index_y, color) ⇒ Player

Returns a new instance of Player.



10
11
12
13
14
15
16
17
18
19
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 10

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_xObject

Returns the value of attribute cell_index_x.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def cell_index_x
  @cell_index_x
end

#cell_index_yObject

Returns the value of attribute cell_index_y.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def cell_index_y
  @cell_index_y
end

#is_movingObject

Returns the value of attribute is_moving.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def is_moving
  @is_moving
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def path
  @path
end

#target_xObject

Returns the value of attribute target_x.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def target_x
  @target_x
end

#target_yObject

Returns the value of attribute target_y.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def target_y
  @target_y
end

#xObject

Returns the value of attribute x.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def x
  @x
end

#yObject

Returns the value of attribute y.



9
10
11
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 9

def y
  @y
end

Instance Method Details

#check_for_path(ignored_path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 26

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

#drawObject

draw



99
100
101
102
103
104
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 99

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

#moveObject

move



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
95
96
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 59

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



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 44

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_targetObject



21
22
23
24
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/player.rb', line 21

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