Module: Pugnacious::Movable

Included in:
Molecule
Defined in:
lib/pugnacious/movable.rb

Constant Summary collapse

DIRECTIONS =
{:north => [0, -1] ,
:north_east => [1, -1],
:east => [1, 0],
:south_east => [1, 1],
:south => [0, 1],
:south_west => [-1, 1],
:west => [-1, 0],
:north_west => [-1, -1]}

Instance Method Summary collapse

Instance Method Details

#check_tile(direction) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pugnacious/movable.rb', line 64

def check_tile(direction)
  xw, yw = DIRECTIONS[direction]

  x = xw + @position[0]
  y = yw + @position[1]

  tile = @game_map[x][y]

  if tile == :empty
    return :empty
  elsif tile.class == Molecule
    if tile.player != self.player
      return :enemy
    else
      return :friend
    end 
  end
end

#fight(direction) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/pugnacious/movable.rb', line 30

def fight(direction)
  xw, yw = DIRECTIONS[direction]

  x = xw + @position[0]
  y = yw + @position[1]

  tile = @game_map[x][y]
  
  tile.receive_damage      
end

#hereObject



151
152
153
# File 'lib/pugnacious/movable.rb', line 151

def here()
  @pointer.x == body.x and @pointer.y == body.y
end

#moveObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pugnacious/movable.rb', line 13

def move
  unless pointer_direction() == :here 
    posible_directions = posible_directions(pointer_direction())
  
    posible_directions.each do |direction|
      case check_tile(direction)
        when :empty
          move!(direction)
          return
        when :enemy
          fight(direction)
          return
      end
    end 
  end
end

#move!(direction) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pugnacious/movable.rb', line 83

def move!(direction)
  xw, yw =  DIRECTIONS[direction]

  x = xw + @position[0]
  y = yw + @position[1]

  @game_map[@position[0]][@position[1]] = :empty
  @game_map[x][y] = self

  body.pos = [x * MOLECULE_SIZE, y * MOLECULE_SIZE]
  @position = [x, y]
end

#pointer_at_eastObject



127
128
129
# File 'lib/pugnacious/movable.rb', line 127

def pointer_at_east()
  @pointer.x > body.x and @pointer.y == body.y
end

#pointer_at_northObject



123
124
125
# File 'lib/pugnacious/movable.rb', line 123

def pointer_at_north()
  @pointer.x == body.x and @pointer.y < body.y
end

#pointer_at_north_eastObject



139
140
141
# File 'lib/pugnacious/movable.rb', line 139

def pointer_at_north_east()
  @pointer.x > body.x and @pointer.y < body.y
end

#pointer_at_north_westObject



147
148
149
# File 'lib/pugnacious/movable.rb', line 147

def pointer_at_north_west()
  @pointer.x < body.x and @pointer.y < body.y
end

#pointer_at_southObject



119
120
121
# File 'lib/pugnacious/movable.rb', line 119

def pointer_at_south()
  @pointer.x == body.x and @pointer.y > body.y
end

#pointer_at_south_eastObject



135
136
137
# File 'lib/pugnacious/movable.rb', line 135

def pointer_at_south_east()
  @pointer.x > body.x and @pointer.y > body.y
end

#pointer_at_south_westObject



143
144
145
# File 'lib/pugnacious/movable.rb', line 143

def pointer_at_south_west()
  @pointer.x < body.x and @pointer.y > body.y
end

#pointer_at_westObject



131
132
133
# File 'lib/pugnacious/movable.rb', line 131

def pointer_at_west()
  @pointer.x < body.x and @pointer.y == body.y
end

#pointer_directionObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pugnacious/movable.rb', line 96

def pointer_direction()
  case
    when pointer_at_south
      :south
    when pointer_at_north 
      :north
    when pointer_at_east 
      :east
    when pointer_at_west 
      :west
    when pointer_at_south_east
      :south_east
    when pointer_at_north_east 
      :north_east
    when pointer_at_south_west
      :south_west
    when pointer_at_north_west
      :north_west
    when here
      :here
  end
end

#posible_directions(direction) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pugnacious/movable.rb', line 41

def posible_directions(direction)
  directions = []
  index = DIRECTIONS.keys.find_index(direction)
  indexes = []
               
  # The order is important. 
  indexes << index
  indexes << index - 1
  indexes << index + 1
  indexes << index - 2
  indexes << index + 2
  
  directions = indexes.map do |i|
    if i>7  
      DIRECTIONS.keys[i-7]          
    else
      DIRECTIONS.keys[i]
    end
  end

  directions
end