Class: Pugnacious::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/pugnacious/player.rb

Constant Summary collapse

SPEED =
10
POINTER_SIZE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Player

Returns a new instance of Player.



8
9
10
11
12
13
14
15
16
17
# File 'lib/pugnacious/player.rb', line 8

def initialize(options = {})
  @color          = options[:color] || Ray::Color.blue
  position        = options[:position] || [200, 200]

  @control_keys   = options[:control_keys]
  @control_keys ||= [:up, :right, :down, :left]

  @pointer = Ray::Polygon.circle(position, POINTER_SIZE, @color , 3, @color)
  @pointer.filled = false
end

Instance Attribute Details

#armyObject

Returns the value of attribute army.



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

def army
  @army
end

#colorObject

Returns the value of attribute color.



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

def color
  @color
end

#control_keysObject

Returns the value of attribute control_keys.



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

def control_keys
  @control_keys
end

#pointerObject

Returns the value of attribute pointer.



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

def pointer
  @pointer
end

#speedObject

Returns the value of attribute speed.



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

def speed
  @speed
end

Instance Method Details

#control_keys_downObject



44
45
46
# File 'lib/pugnacious/player.rb', line 44

def control_keys_down
  control_keys[2]
end

#control_keys_leftObject



48
49
50
# File 'lib/pugnacious/player.rb', line 48

def control_keys_left
  control_keys[3]
end

#control_keys_rightObject



40
41
42
# File 'lib/pugnacious/player.rb', line 40

def control_keys_right
  control_keys[1]
end

#control_keys_upObject

Control keys aliases



36
37
38
# File 'lib/pugnacious/player.rb', line 36

def control_keys_up
  control_keys[0]
end

#move(direction) ⇒ Object



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

def move(direction)
  case direction
    when control_keys_up
      pointer.y -= SPEED unless (pointer.pos.y -= SPEED) < 0 + POINTER_SIZE
    when control_keys_right
      pointer.x += SPEED unless (pointer.pos.x += SPEED) > MAP_SIZE - POINTER_SIZE
    when control_keys_down
      pointer.y += SPEED unless (pointer.pos.y += SPEED) > MAP_SIZE - POINTER_SIZE
    when control_keys_left
      pointer.x -= SPEED unless (pointer.pos.x -= SPEED) < 0 + POINTER_SIZE
  end
end