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

Initialization of player assigns gamespace to the player, their sprite and sets the input layout for the player.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/prkwars/player.rb', line 23

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.



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

def invulnerable
  @invulnerable
end

#livesObject

Returns the value of attribute lives.



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

def lives
  @lives
end

Instance Method Details

#move_downObject

A method changing the player’s y position by the PLAYER_VELOCITY constant.



51
52
53
54
# File 'lib/prkwars/player.rb', line 51

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

#move_leftObject

A method changing the player’s x position by the PLAYER_VELOCITY constant.



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

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

#move_rightObject

A method changing the player’s x position by the PLAYER_VELOCITY constant.



69
70
71
72
# File 'lib/prkwars/player.rb', line 69

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

#move_upObject

A method changing the player’s y position by the PLAYER_VELOCITY constant.



42
43
44
45
# File 'lib/prkwars/player.rb', line 42

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

#shoot_downObject

Sets the shooting vector’s y part to BULLET_VELOCITY.



97
98
99
# File 'lib/prkwars/player.rb', line 97

def shoot_down
  @shoot_vec_y = Constants::BULLET_VELOCITY
end

#shoot_leftObject

Sets the shooting vector’s x part to -BULLET_VELOCITY.



77
78
79
# File 'lib/prkwars/player.rb', line 77

def shoot_left
  @shoot_vec_x = -Constants::BULLET_VELOCITY
end

#shoot_rightObject

Sets the shooting vector’s x part to BULLET_VELOCITY.



84
85
86
# File 'lib/prkwars/player.rb', line 84

def shoot_right
  @shoot_vec_x = Constants::BULLET_VELOCITY
end

#shoot_upObject

Sets the shooting vector’s y part to -BULLET_VELOCITY.



91
92
93
# File 'lib/prkwars/player.rb', line 91

def shoot_up
  @shoot_vec_y = -Constants::BULLET_VELOCITY
end

#updateObject

Method updating the player each frame. Player’s sprite gets rotated to correspond to his movement direction. In case they’re out of bounds the player’s coordinates are corrected and bullets are shot if possible.



106
107
108
109
110
111
112
113
114
# File 'lib/prkwars/player.rb', line 106

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