Class: Player

Inherits:
Chingu::GameObject
  • Object
show all
Includes:
GamespacePersistence
Defined in:
lib/prkwars/player.rb

Overview

Class holding all the logic related to the player - input controller and any manipulation of player’s sprite.

Constant Summary collapse

STARTING_LIVES =
3
SHOOT_GAP =
8
STARTING_BOMBS =
3
INVUL_FRAMES =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GamespacePersistence

#correct_coords, #in_bounds

Constructor Details

#initialize(gamespace, options = {}) ⇒ Player

Returns a new instance of Player.



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

def initialize(gamespace, options = {})
  super(options)

  @image = Image['./media/player_ship.png']
  self.input = { holding_w: :move_up, holding_a: :move_left,
                 holding_d: :move_right, holding_s: :move_down,
                 holding_left: :shoot_left, holding_right: :shoot_right,
                 holding_up: :shoot_up, holding_down: :shoot_down }
  init_vector_variables

  @gamespace = gamespace
  @lives = STARTING_LIVES
  @bombs = STARTING_BOMBS
end

Instance Attribute Details

#invulnerableObject (readonly)

Returns the value of attribute invulnerable.



11
12
13
# File 'lib/prkwars/player.rb', line 11

def invulnerable
  @invulnerable
end

#livesObject

Returns the value of attribute lives.



10
11
12
# File 'lib/prkwars/player.rb', line 10

def lives
  @lives
end

Instance Method Details

#move_downObject



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

def move_down
  @y += Constants::PLAYER_VELOCITY
  @y_change = Constants::PLAYER_VELOCITY
end

#move_leftObject



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

def move_left
  @x -= Constants::PLAYER_VELOCITY
  @x_change = -Constants::PLAYER_VELOCITY
end

#move_rightObject



49
50
51
52
# File 'lib/prkwars/player.rb', line 49

def move_right
  @x += Constants::PLAYER_VELOCITY
  @x_change = Constants::PLAYER_VELOCITY
end

#move_upObject



34
35
36
37
# File 'lib/prkwars/player.rb', line 34

def move_up
  @y -= Constants::PLAYER_VELOCITY
  @y_change = -Constants::PLAYER_VELOCITY
end

#shoot_downObject



66
67
68
# File 'lib/prkwars/player.rb', line 66

def shoot_down
  @shoot_vec_y = Constants::BULLET_VELOCITY
end

#shoot_leftObject



54
55
56
# File 'lib/prkwars/player.rb', line 54

def shoot_left
  @shoot_vec_x = -Constants::BULLET_VELOCITY
end

#shoot_rightObject



58
59
60
# File 'lib/prkwars/player.rb', line 58

def shoot_right
  @shoot_vec_x = Constants::BULLET_VELOCITY
end

#shoot_upObject



62
63
64
# File 'lib/prkwars/player.rb', line 62

def shoot_up
  @shoot_vec_y = -Constants::BULLET_VELOCITY
end

#updateObject



70
71
72
73
74
75
76
77
78
# File 'lib/prkwars/player.rb', line 70

def update
  rotate_player
  correct_coords(self, @gamespace) unless in_bounds(self, @gamespace)
  @shoot_timer += 1
  return unless @shoot_timer == SHOOT_GAP

  shoot_bullet
  @shoot_timer = 0
end