class Player

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

Constants

INVUL_FRAMES
SHOOT_GAP
STARTING_BOMBS
STARTING_LIVES

Attributes

invulnerable[R]
lives[RW]

Public Class Methods

new(gamespace, options = {}) click to toggle source

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

Calls superclass method
# 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

Public Instance Methods

move_down() click to toggle source

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

# File lib/prkwars/player.rb, line 51
def move_down
  @y += Constants::PLAYER_VELOCITY
  @y_change = Constants::PLAYER_VELOCITY
end
move_left() click to toggle source

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

# File lib/prkwars/player.rb, line 60
def move_left
  @x -= Constants::PLAYER_VELOCITY
  @x_change = -Constants::PLAYER_VELOCITY
end
move_right() click to toggle source

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

# File lib/prkwars/player.rb, line 69
def move_right
  @x += Constants::PLAYER_VELOCITY
  @x_change = Constants::PLAYER_VELOCITY
end
move_up() click to toggle source

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

# File lib/prkwars/player.rb, line 42
def move_up
  @y -= Constants::PLAYER_VELOCITY
  @y_change = -Constants::PLAYER_VELOCITY
end
shoot_down() click to toggle source

Sets the shooting vector's y part to BULLET_VELOCITY.

# File lib/prkwars/player.rb, line 97
def shoot_down
  @shoot_vec_y = Constants::BULLET_VELOCITY
end
shoot_left() click to toggle source

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

# File lib/prkwars/player.rb, line 77
def shoot_left
  @shoot_vec_x = -Constants::BULLET_VELOCITY
end
shoot_right() click to toggle source

Sets the shooting vector's x part to BULLET_VELOCITY.

# File lib/prkwars/player.rb, line 84
def shoot_right
  @shoot_vec_x = Constants::BULLET_VELOCITY
end
shoot_up() click to toggle source

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

# File lib/prkwars/player.rb, line 91
def shoot_up
  @shoot_vec_y = -Constants::BULLET_VELOCITY
end
update() click to toggle source

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.

# 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

Private Instance Methods

init_vector_variables() click to toggle source

Initializes variables used by the player class to determine shooting speed as well as rotations of their sprite.

# File lib/prkwars/player.rb, line 122
def init_vector_variables
  @x_change = 0 # Used for rotating the triangular sprite
  @y_change = 0
  @shoot_timer = 0
  @shoot_vec_x = 0 # Used for deciding the shooting direction
  @shoot_vec_y = 0
end
rotate_player() click to toggle source

Rotates the player sprite if there was a change in their movement direction.

# File lib/prkwars/player.rb, line 133
def rotate_player
  return unless @x_change != 0 || @y_change != 0

  rot_to = Math.atan2(@y_change, @x_change) / Math::PI * 180 + 90

  self.angle = rot_to

  @x_change = 0
  @y_change = 0
end
shoot_bullet() click to toggle source

Method shooting bullets in the direction which was input by the player. These directions are repesented by the @shoot_vec_x and @shoot_vec_y instance variables.

# File lib/prkwars/player.rb, line 149
def shoot_bullet
  return unless @shoot_vec_x != 0 || @shoot_vec_y != 0

  bullet_angle = Math.atan2(@shoot_vec_y, @shoot_vec_x) / Math::PI * 180 - 180

  norm_x = -@shoot_vec_y
  norm_y = @shoot_vec_x

  dist_norm = Math.sqrt(norm_x * norm_x + norm_y * norm_y)

  unit_x = norm_x / dist_norm
  unit_y = norm_y / dist_norm

  Bullet.create(@gamespace,
                zorder: ZOrder::GAMEOBJECT, x: @x + unit_x * 7,
                y: @y + unit_y * 7,
                velocity_x: @shoot_vec_x, velocity_y: @shoot_vec_y,
                angle: bullet_angle)

  Bullet.create(@gamespace,
                zorder: ZOrder::GAMEOBJECT, x: @x - unit_x * 7,
                y: @y - unit_y * 7,
                velocity_x: @shoot_vec_x, velocity_y: @shoot_vec_y,
                angle: bullet_angle)
  @shoot_vec_x = 0
  @shoot_vec_y = 0
end