Class: Player

Inherits:
Entity show all
Includes:
Comparable
Defined in:
lib/game_2d/player.rb

Overview

The base Player class representing what all Players have in common Moves can be enqueued by calling add_move Calling update() causes a move to be dequeued and executed, applying forces to the game object

The server instantiates this class to represent each connected player

Constant Summary collapse

BUILD_TIME =

Game ticks it takes before a block’s HP is raised by 1

7
BRAKE_SPEED =

Amount to decelerate each tick when braking

4

Constants included from EntityConstants

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

Instance Attribute Summary collapse

Attributes inherited from Entity

#a, #moving, #space, #x, #x_vel, #y, #y_vel

Instance Method Summary collapse

Methods inherited from Entity

#accelerate, #angle_to_vector, #bottom_cell_y, bottom_cell_y_at, #direction_to, #doomed?, #drop_diagonal, #empty_above?, #empty_on_left?, #empty_on_right?, #empty_underneath?, #entities_obstructing, #going_past_entity, #harmed_by, #i_hit, #left_cell_x, left_cell_x_at, #move, #move_x, #move_y, #moving?, #next_to, #occupied_cells, #opaque, #pixel_x, #pixel_y, #right_cell_x, right_cell_x_at, #should_fall?, #top_cell_y, top_cell_y_at, #vector_to_angle, #wake!, #warp

Methods included from Registerable

#nullsafe_registry_id, #registry_id, #registry_id=, #registry_id?, #registry_id_safe

Methods included from Serializable

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

Constructor Details

#initialize(player_name = "<unknown>") ⇒ Player

Returns a new instance of Player.


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/game_2d/player.rb', line 27

def initialize(player_name = "<unknown>")
  super
  @player_name = player_name
  @score = 0
  @moves = []
  @current_move = nil
  @falling = false
  @build_block_id = nil
  @build_level = 0
  @complex_move = nil
end

Instance Attribute Details

#build_block_idObject

Returns the value of attribute build_block_id.


25
26
27
# File 'lib/game_2d/player.rb', line 25

def build_block_id
  @build_block_id
end

#player_nameObject

Returns the value of attribute player_name.


24
25
26
# File 'lib/game_2d/player.rb', line 24

def player_name
  @player_name
end

#scoreObject

Returns the value of attribute score.


24
25
26
# File 'lib/game_2d/player.rb', line 24

def score
  @score
end

Instance Method Details

#add_move(new_move) ⇒ Object

Accepts a hash, with a key :move => move_type


209
210
211
212
# File 'lib/game_2d/player.rb', line 209

def add_move(new_move)
  return unless new_move
  @moves << new_move
end

#all_stateObject


218
219
220
221
# File 'lib/game_2d/player.rb', line 218

def all_state
  super.unshift(player_name).push(
    score, build_block_id, @complex_move)
end

#as_jsonObject


223
224
225
226
227
228
229
230
# File 'lib/game_2d/player.rb', line 223

def as_json
  super.merge!(
    :player_name => player_name,
    :score => score,
    :build_block => @build_block_id,
    :complex_move => @complex_move.as_json
  )
end

#brakeObject


151
152
153
154
155
156
157
# File 'lib/game_2d/player.rb', line 151

def brake
  if @x_vel.zero?
    self.y_vel = brake_velocity(@y_vel)
  else
    self.x_vel = brake_velocity(@x_vel)
  end
end

#brake_velocity(v) ⇒ Object


159
160
161
162
163
# File 'lib/game_2d/player.rb', line 159

def brake_velocity(v)
  return 0 if v.abs < BRAKE_SPEED
  sign = v <=> 0
  sign * (v.abs - BRAKE_SPEED)
end

#buildObject

Create the actual block


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/game_2d/player.rb', line 177

def build
  if building?
    @build_level += 1
    if @build_level >= BUILD_TIME
      @build_level = 0
      build_block.hp += 1
    end
  else
    bb = Entity::Block.new(@x, @y)
    bb.owner_id = registry_id
    bb.hp = 1
    @space << bb # generates an ID
    @build_block_id = bb.registry_id
    @build_level = 0
  end
end

#build_blockObject


49
50
51
52
53
# File 'lib/game_2d/player.rb', line 49

def build_block
  return nil unless building?
  fail "Can't look up build_block when not in a space" unless @space
  @space[@build_block_id] or fail "Don't have build_block #{@build_block_id}"
end

#building?Boolean

Returns:

  • (Boolean)

47
# File 'lib/game_2d/player.rb', line 47

def building?; @build_block_id; end

#check_for_disown_blockObject


196
197
198
199
200
201
202
# File 'lib/game_2d/player.rb', line 196

def check_for_disown_block
  return unless building?
  return if @space.entities_overlapping(@x, @y).include?(build_block)
  build_block.owner_id = nil
  build_block.wake!
  disown_block
end

#destroy!Object


55
56
57
# File 'lib/game_2d/player.rb', line 55

def destroy!
  build_block.owner_id = nil if building?
end

#disown_blockObject


194
# File 'lib/game_2d/player.rb', line 194

def disown_block; $stderr.puts "#{self} disowning #{build_block}"; @build_block_id, @build_level = nil, 0; end

#draw(window) ⇒ Object


244
245
246
247
248
249
250
# File 'lib/game_2d/player.rb', line 244

def draw(window)
  super
  window.font.draw_rel(player_name,
    pixel_x + CELL_WIDTH_IN_PIXELS / 2, pixel_y, ZOrder::Text,
    0.5, 1.0, # Centered X; above Y
    1.0, 1.0, Gosu::Color::YELLOW)
end

#draw_zorderObject


242
# File 'lib/game_2d/player.rb', line 242

def draw_zorder; ZOrder::Player end

#falling?Boolean

Returns:

  • (Boolean)

41
# File 'lib/game_2d/player.rb', line 41

def falling?; @falling; end

#fire(x_vel, y_vel) ⇒ Object

Create the actual pellet


170
171
172
173
174
# File 'lib/game_2d/player.rb', line 170

def fire(x_vel, y_vel)
  pellet = Entity::Pellet.new(@x, @y, 0, x_vel, y_vel)
  pellet.owner = self
  @space << pellet
end

#flipObject


165
166
167
# File 'lib/game_2d/player.rb', line 165

def flip
  self.a += 180
end

#image_filenameObject


240
# File 'lib/game_2d/player.rb', line 240

def image_filename; "player.png"; end

#rise_upObject


204
205
206
# File 'lib/game_2d/player.rb', line 204

def rise_up
  @complex_move = Move::RiseUp.new(self)
end

#sleep_now?Boolean

Returns:

  • (Boolean)

39
# File 'lib/game_2d/player.rb', line 39

def sleep_now?; false; end

#slide(dir) ⇒ Object


143
144
145
146
147
148
149
# File 'lib/game_2d/player.rb', line 143

def slide(dir)
  if opaque(next_to(dir)).empty?
    accelerate(*angle_to_vector(dir))
  else
    self.a = dir + 180
  end
end

#slide_leftObject


140
# File 'lib/game_2d/player.rb', line 140

def slide_left; slide(self.a - 90); end

#slide_rightObject


141
# File 'lib/game_2d/player.rb', line 141

def slide_right; slide(self.a + 90); end

#to_sObject


214
215
216
# File 'lib/game_2d/player.rb', line 214

def to_s
  "#{player_name} (#{registry_id_safe}) at #{x}x#{y}"
end

#transparent_to_me?(other) ⇒ Boolean

Pellets don’t hit the originating player

Returns:

  • (Boolean)

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

def transparent_to_me?(other)
  super ||
  (other == build_block) ||
  (other.is_a?(Pellet) && other.owner == self)
end

#updateObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/game_2d/player.rb', line 66

def update
  fail "No space set for #{self}" unless @space
  check_for_disown_block

  if @complex_move
    # returns true if more work to do
    return if @complex_move.update(self)
    @complex_move.on_completion(self)
    @complex_move = nil
  end

  underfoot = next_to(self.a + 180)
  if @falling = underfoot.empty?
    self.a = 0
    accelerate(0, 1)
  end

  args = @moves.shift
  case (current_move = args.delete(:move).to_sym)
    when :slide_left, :slide_right, :brake, :flip, :build, :rise_up
      send current_move unless @falling
    when :fire
      fire args[:x_vel], args[:y_vel]
    else
      puts "Invalid move for #{self}: #{current_move}, #{args.inspect}"
  end if args

  # Only go around corner if sitting on exactly one object
  if underfoot.size == 1
    other = underfoot.first
    # Figure out where corner is and whether we're about to reach or pass it
    corner, distance, overshoot, turn = going_past_entity(other.x, other.y)
    if corner
      original_speed = @x_vel.abs + @y_vel.abs
      original_dir = vector_to_angle
      new_dir = original_dir + turn

      # Make sure nothing occupies any space we're about to move through
      if opaque(
        @space.entities_overlapping(*corner) + next_to(new_dir, *corner)
      ).empty?
        # Move to the corner
        self.x_vel, self.y_vel = angle_to_vector(original_dir, distance)
        move

        # Turn and apply remaining velocity
        # Make sure we move at least one subpixel so we don't sit exactly at
        # the corner, and fall
        self.a += turn
        overshoot = 1 if overshoot.zero?
        self.x_vel, self.y_vel = angle_to_vector(new_dir, overshoot)
        move

        self.x_vel, self.y_vel = angle_to_vector(new_dir, original_speed)
      else
        # Something's in the way -- possibly in front of us, or possibly
        # around the corner
        move
      end
    else
      # Not yet reaching the corner -- or making a diagonal motion, for which
      # we can't support going around the corner
      move
    end
  else
    # Straddling two objects, or falling
    move
  end

  # Check again whether we've moved off of a block
  # we were building
  check_for_disown_block
end

#update_from_json(json) ⇒ Object


232
233
234
235
236
237
238
# File 'lib/game_2d/player.rb', line 232

def update_from_json(json)
  @player_name = json[:player_name]
  @score = json[:score]
  @build_block_id = json[:build_block].try(:to_sym)
  @complex_move = Serializable.from_json(json[:complex_move])
  super
end