Class: Player
- Inherits:
-
Chingu::GameObject
- Object
- Chingu::GameObject
- Player
- 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
-
#invulnerable ⇒ Object
readonly
Returns the value of attribute invulnerable.
-
#lives ⇒ Object
Returns the value of attribute lives.
Instance Method Summary collapse
-
#initialize(gamespace, options = {}) ⇒ Player
constructor
Initialization of player assigns gamespace to the player, their sprite and sets the input layout for the player.
-
#move_down ⇒ Object
A method changing the player’s y position by the
PLAYER_VELOCITYconstant. -
#move_left ⇒ Object
A method changing the player’s x position by the
PLAYER_VELOCITYconstant. -
#move_right ⇒ Object
A method changing the player’s x position by the
PLAYER_VELOCITYconstant. -
#move_up ⇒ Object
A method changing the player’s y position by the
PLAYER_VELOCITYconstant. -
#shoot_down ⇒ Object
Sets the shooting vector’s y part to
BULLET_VELOCITY. -
#shoot_left ⇒ Object
Sets the shooting vector’s x part to
-BULLET_VELOCITY. -
#shoot_right ⇒ Object
Sets the shooting vector’s x part to
BULLET_VELOCITY. -
#shoot_up ⇒ Object
Sets the shooting vector’s y part to
-BULLET_VELOCITY. -
#update ⇒ Object
Method updating the player each frame.
Methods included from GamespacePersistence
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, = {}) super() @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
#invulnerable ⇒ Object (readonly)
Returns the value of attribute invulnerable.
12 13 14 |
# File 'lib/prkwars/player.rb', line 12 def invulnerable @invulnerable end |
#lives ⇒ Object
Returns the value of attribute lives.
11 12 13 |
# File 'lib/prkwars/player.rb', line 11 def lives @lives end |
Instance Method Details
#move_down ⇒ Object
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_left ⇒ Object
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_right ⇒ Object
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_up ⇒ Object
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_down ⇒ Object
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_left ⇒ Object
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_right ⇒ Object
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_up ⇒ Object
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 |
#update ⇒ Object
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 |