Class: AMazeIng::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/a_maze_ing/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.



4
5
6
7
8
9
10
11
12
13
# File 'lib/a_maze_ing/player.rb', line 4

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.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def cell_index_x
  @cell_index_x
end

#cell_index_yObject

Returns the value of attribute cell_index_y.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def cell_index_y
  @cell_index_y
end

#is_movingObject

Returns the value of attribute is_moving.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def is_moving
  @is_moving
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def path
  @path
end

#target_xObject

Returns the value of attribute target_x.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def target_x
  @target_x
end

#target_yObject

Returns the value of attribute target_y.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def target_y
  @target_y
end

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/a_maze_ing/player.rb', line 3

def y
  @y
end

Instance Method Details

#check_for_path(ignored_path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/a_maze_ing/player.rb', line 20

def check_for_path(ignored_path)
  path = nil
  $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



88
89
90
91
92
93
# File 'lib/a_maze_ing/player.rb', line 88

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



49
50
51
52
53
54
55
56
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
# File 'lib/a_maze_ing/player.rb', line 49

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



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/a_maze_ing/player.rb', line 35

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



15
16
17
18
# File 'lib/a_maze_ing/player.rb', line 15

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