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

#can_i_move_there?(direction) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pugnacious/movable.rb', line 31

def can_i_move_there?(direction)
  xw, yw = DIRECTIONS[direction]

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

  if @game_map[x][y] == :empty
    return true
  elsif @game_map[x][y].class == Molecule
    if @game_map[x][y].player != self.player
      @game_map[x][y].receive_damage()
    end
    return false
  else
    return false
  end
end

#hereObject



106
107
108
# File 'lib/pugnacious/movable.rb', line 106

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

#moveObject



13
14
15
# File 'lib/pugnacious/movable.rb', line 13

def move
  try_to_go(where_is_the_pointer?) unless where_is_the_pointer? == :here
end

#move_there(direction) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pugnacious/movable.rb', line 49

def move_there(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



82
83
84
# File 'lib/pugnacious/movable.rb', line 82

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

#pointer_at_northObject



78
79
80
# File 'lib/pugnacious/movable.rb', line 78

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

#pointer_at_north_eastObject



94
95
96
# File 'lib/pugnacious/movable.rb', line 94

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

#pointer_at_north_westObject



102
103
104
# File 'lib/pugnacious/movable.rb', line 102

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

#pointer_at_southObject



74
75
76
# File 'lib/pugnacious/movable.rb', line 74

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

#pointer_at_south_eastObject



90
91
92
# File 'lib/pugnacious/movable.rb', line 90

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

#pointer_at_south_westObject



98
99
100
# File 'lib/pugnacious/movable.rb', line 98

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

#pointer_at_westObject



86
87
88
# File 'lib/pugnacious/movable.rb', line 86

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

#try_to_go(direction) ⇒ Object



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

def try_to_go(direction)
  directions = DIRECTIONS.keys
  intention_index = directions.find_index(direction)
  directions.push(:north)

  if can_i_move_there?(direction)
    move_there(direction)
  elsif can_i_move_there?(directions[intention_index+1])
    move_there(directions[intention_index+1])
  elsif can_i_move_there?(directions[intention_index-1])
    move_there(directions[intention_index-1])
  end
end

#where_is_the_pointer?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pugnacious/movable.rb', line 62

def where_is_the_pointer?
  if pointer_at_south then return :south end
  if pointer_at_north then return :north end
  if pointer_at_east then return :east end
  if pointer_at_west then return :west end
  if pointer_at_south_east then return :south_east end
  if pointer_at_north_east then return :north_east end
  if pointer_at_south_west then return :south_west end
  if pointer_at_north_west then return :north_west end
  if here then return :here end
end