Class: Move::RiseUp

Inherits:
ComplexMove show all
Includes:
EntityConstants
Defined in:
lib/game_2d/move/rise_up.rb

Constant Summary

Constants included from EntityConstants

EntityConstants::CELL_WIDTH_IN_PIXELS, EntityConstants::MAX_VELOCITY, EntityConstants::PIXEL_WIDTH, EntityConstants::WIDTH

Instance Attribute Summary collapse

Attributes inherited from ComplexMove

#actor_id

Instance Method Summary collapse

Methods included from Serializable

#<=>, #==, as_json, #eql?, from_json, #hash, #to_json

Constructor Details

#initialize(actor = nil) ⇒ RiseUp

Returns a new instance of RiseUp.



13
14
15
16
# File 'lib/game_2d/move/rise_up.rb', line 13

def initialize(actor=nil)
  super
  @stage = :center
end

Instance Attribute Details

#distanceObject

Valid stages: :center, :rise, :reset Distance is how much further we need to go (in pixels) in stage :rise



12
13
14
# File 'lib/game_2d/move/rise_up.rb', line 12

def distance
  @distance
end

#stageObject

Valid stages: :center, :rise, :reset Distance is how much further we need to go (in pixels) in stage :rise



12
13
14
# File 'lib/game_2d/move/rise_up.rb', line 12

def stage
  @stage
end

Instance Method Details

#all_stateObject



59
60
61
# File 'lib/game_2d/move/rise_up.rb', line 59

def all_state
  super.push @stage, @distance
end

#as_jsonObject



62
63
64
65
# File 'lib/game_2d/move/rise_up.rb', line 62

def as_json
  super.merge! :stage => @stage,
    :distance => @distance
end

#on_completion(actor) ⇒ Object



18
19
20
# File 'lib/game_2d/move/rise_up.rb', line 18

def on_completion(actor)
  actor.instance_exec { @x_vel = @y_vel = 0 }
end

#to_sObject



71
72
73
# File 'lib/game_2d/move/rise_up.rb', line 71

def to_s
  "RiseUp[#{stage}, #{distance} to go]"
end

#update(actor) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/game_2d/move/rise_up.rb', line 22

def update(actor)
  # It's convenient to set 'self' to the Player
  # object, here
  actor.instance_exec(self) do |cm|
    # Abort if the build_block gets destroyed
    blok = build_block
    return false unless blok

    start_x, start_y = blok.x, blok.y
    case cm.stage
    when :center, :reset
      if x == start_x && y == start_y
        # If we're in reset, we're all done
        return false if cm.stage == :reset

        # Establish our velocity for :rise
        cm.stage = :rise
        @x_vel, @y_vel = angle_to_vector(a, PIXEL_WIDTH)
        cm.distance = CELL_WIDTH_IN_PIXELS
        return cm.update(self)
      end
      @x_vel = [[start_x - x, -PIXEL_WIDTH].max, PIXEL_WIDTH].min
      @y_vel = [[start_y - y, -PIXEL_WIDTH].max, PIXEL_WIDTH].min
      # move returns false: it failed somehow
      return move
    when :rise
      # Success
      return false if cm.distance.zero?

      cm.distance -= 1
      # move fails? Go to :reset
      move || (cm.stage = :reset)
      return true
    end
  end
end

#update_from_json(json) ⇒ Object



66
67
68
69
70
# File 'lib/game_2d/move/rise_up.rb', line 66

def update_from_json(json)
  self.stage = json[:stage].to_sym
  self.distance = json[:distance]
  super
end