Class: Acceleration

Inherits:
Action show all
Defined in:
lib/software_challenge_client/action.rb

Overview

Accelerate by #acceleration. To decelerate, use a negative value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Action

#invalid

Constructor Details

#initialize(acceleration) ⇒ Acceleration

Returns a new instance of Acceleration.



35
36
37
# File 'lib/software_challenge_client/action.rb', line 35

def initialize(acceleration)
  @acceleration = acceleration
end

Instance Attribute Details

#accelerationObject (readonly)

Returns the value of attribute acceleration.



33
34
35
# File 'lib/software_challenge_client/action.rb', line 33

def acceleration
  @acceleration
end

Instance Method Details

#==(other) ⇒ Object



73
74
75
# File 'lib/software_challenge_client/action.rb', line 73

def ==(other)
  other.type == type && other.acceleration == acceleration
end

#perform!(gamestate, current_player) ⇒ Object

Perform the action.

Parameters:

  • gamestate (GameState)

    The game state on which the action will be performed. Performing may change the game state.

  • current_player (Player)

    The player for which the action will be performed.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/software_challenge_client/action.rb', line 43

def perform!(gamestate, current_player)
  new_velocity = current_player.velocity + acceleration
  if new_velocity < 1
    invalid 'Geschwindigkeit darf nicht unter 1 verringert werden.'
  end
  if new_velocity > 6
    invalid 'Geschwindigkeit darf nicht über 6 erhöht werden.'
  end
  acceleration.abs.times do
    if gamestate.free_acceleration?
      gamestate.free_acceleration = false
    elsif current_player.coal.zero?
      invalid 'Nicht genug Kohle zum Beschleunigen.'
    else
      current_player.coal -= 1
    end
  end
  if gamestate.board.field(current_player.x, current_player.y).type == FieldType::SANDBANK
    invalid 'Auf einer Sandbank kann nicht beschleunigt werden.'
  end
  current_player.velocity = new_velocity
  # This works only when acceleration is the first action in a move. The move
  # class has to check that.
  current_player.movement = new_velocity
end

#typeObject



69
70
71
# File 'lib/software_challenge_client/action.rb', line 69

def type
  :acceleration
end