Class: Doom::Game::PlayerState

Inherits:
Object
  • Object
show all
Defined in:
lib/doom/game/player_state.rb

Overview

Tracks player state for HUD display and weapon rendering

Constant Summary collapse

WEAPON_FIST =

Weapons

0
WEAPON_PISTOL =
1
WEAPON_SHOTGUN =
2
WEAPON_CHAINGUN =
3
WEAPON_ROCKET =
4
WEAPON_PLASMA =
5
WEAPON_BFG =
6
WEAPON_CHAINSAW =
7
WEAPON_NAMES =

Weapon symbols for graphics lookup

{
  WEAPON_FIST => :fist,
  WEAPON_PISTOL => :pistol,
  WEAPON_SHOTGUN => :shotgun,
  WEAPON_CHAINGUN => :chaingun,
  WEAPON_ROCKET => :rocket,
  WEAPON_PLASMA => :plasma,
  WEAPON_BFG => :bfg,
  WEAPON_CHAINSAW => :chainsaw
}.freeze
ATTACK_DURATIONS =

Attack durations in tics (at 35fps), matching DOOM's weapon state sequences

{
  WEAPON_FIST => 14,       # Punch windup + swing
  WEAPON_PISTOL => 16,     # S_PISTOL: 6+4+5+1 tics
  WEAPON_SHOTGUN => 40,    # Pump action cycle
  WEAPON_CHAINGUN => 8,    # Rapid fire (2 shots per cycle)
  WEAPON_ROCKET => 20,     # Rocket launch + recovery
  WEAPON_PLASMA => 8,      # Fast energy weapon
  WEAPON_BFG => 60,        # Long charge + fire
  WEAPON_CHAINSAW => 6     # Fast melee
}.freeze
VIEWHEIGHT =

Smooth step-up/down (matching Chocolate Doom's P_CalcHeight / P_ZMovement)

41.0
VIEWHEIGHT_HALF =
VIEWHEIGHT / 2.0
DELTA_ACCEL =

deltaviewheight += FRACUNIT/4 per tic

0.25
MAXBOB =

View bob (camera bounce when walking, matching Chocolate Doom's P_CalcHeight + P_XYMovement + P_Thrust from p_user.c / p_mobj.c)

16.0
STOPSPEED =

Maximum bob amplitude (0x100000 in fixed-point = 16 map units)

0.0625
BOB_DECAY_RATE =

Continuous-time equivalents of DOOM's per-tic constants (35 fps tic rate):

FRICTION = 0xE800/0x10000 = 0.90625 per tic
decay_rate = -ln(0.90625) * 35 = 3.44/sec
walk thrust = forwardmove(25) * 2048 / 65536 = 0.78 map units/tic = 27.3/sec
terminal velocity = 27.3 / 3.44 = 7.56 -> bob = 7.56^2/4 = 14.3 (89% of MAXBOB)
3.44
BOB_THRUST =

Friction as continuous decay rate (1/sec)

26.0
BOB_FREQUENCY =

Walk thrust (map units/sec), gives terminal ~7.5

11.0
BOB_MOM_SCALE =

Compute view bob from actual movement momentum. Matches Chocolate Doom P_CalcHeight:

bob = (momx*momx + momy*momy) >> 2, capped at MAXBOB
viewz += finesine[angle] * bob/2

Momentum is in units/sec; DOOM's bob uses units/tic (divide by 35).

1.0 / (35.0 * 35.0 * 4.0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayerState

Returns a new instance of PlayerState.



62
63
64
# File 'lib/doom/game/player_state.rb', line 62

def initialize
  reset
end

Instance Attribute Details

#ammo_bulletsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def ammo_bullets
  @ammo_bullets
end

#ammo_cellsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def ammo_cells
  @ammo_cells
end

#ammo_rocketsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def ammo_rockets
  @ammo_rockets
end

#ammo_shellsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def ammo_shells
  @ammo_shells
end

#armorObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def armor
  @armor
end

#attack_frameObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def attack_frame
  @attack_frame
end

#attack_ticsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def attack_tics
  @attack_tics
end

#attackingObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def attacking
  @attacking
end

#bob_amountObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def bob_amount
  @bob_amount
end

#bob_angleObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def bob_angle
  @bob_angle
end

#damage_countObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def damage_count
  @damage_count
end

#deadObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def dead
  @dead
end

#death_ticObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def death_tic
  @death_tic
end

#deltaviewheightObject (readonly)

Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI



60
61
62
# File 'lib/doom/game/player_state.rb', line 60

def deltaviewheight
  @deltaviewheight
end

#god_modeObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def god_mode
  @god_mode
end

#has_weaponsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def has_weapons
  @has_weapons
end

#healthObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def health
  @health
end

#infinite_ammoObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def infinite_ammo
  @infinite_ammo
end

#is_movingObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def is_moving
  @is_moving
end

#keysObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def keys
  @keys
end

#max_armorObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def max_armor
  @max_armor
end

#max_bulletsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def max_bullets
  @max_bullets
end

#max_cellsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def max_cells
  @max_cells
end

#max_healthObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def max_health
  @max_health
end

#max_rocketsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def max_rockets
  @max_rockets
end

#max_shellsObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def max_shells
  @max_shells
end

#view_bob_offsetObject (readonly)

Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI



60
61
62
# File 'lib/doom/game/player_state.rb', line 60

def view_bob_offset
  @view_bob_offset
end

#viewheightObject (readonly)

Bob cycle frequency (rad/sec): FINEANGLES/20 * 35 / 8192 * 2*PI



60
61
62
# File 'lib/doom/game/player_state.rb', line 60

def viewheight
  @viewheight
end

#weaponObject

Red flash intensity (0-8), decays each tic



40
41
42
# File 'lib/doom/game/player_state.rb', line 40

def weapon
  @weapon
end

Instance Method Details

#apply_fall_impact(momz) ⇒ Object

Called on landing after a fall. Matches Chocolate Doom P_ZMovement: deltaviewheight = momz >> 3, producing a squat that update_viewheight then recovers via DELTA_ACCEL.



230
231
232
# File 'lib/doom/game/player_state.rb', line 230

def apply_fall_impact(momz)
  @deltaviewheight = momz / 8.0
end

#can_attack?Boolean

Returns:



151
152
153
154
155
156
157
# File 'lib/doom/game/player_state.rb', line 151

def can_attack?
  return true if @weapon == WEAPON_FIST || @weapon == WEAPON_CHAINSAW
  return true if @infinite_ammo

  ammo = current_ammo
  ammo && ammo > 0
end

#current_ammoObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/doom/game/player_state.rb', line 136

def current_ammo
  case @weapon
  when WEAPON_PISTOL, WEAPON_CHAINGUN
    @ammo_bullets
  when WEAPON_SHOTGUN
    @ammo_shells
  when WEAPON_ROCKET
    @ammo_rockets
  when WEAPON_PLASMA, WEAPON_BFG
    @ammo_cells
  else
    nil # Fist/chainsaw don't use ammo
  end
end

#dieObject



331
332
333
334
335
336
# File 'lib/doom/game/player_state.rb', line 331

def die
  @dead = true
  @death_tic = 0
  @attacking = false
  @deltaviewheight = -VIEWHEIGHT / 8.0 # View drops to ground
end

#notify_step(step_amount) ⇒ Object

Called when player moves onto a different floor height. Matches Chocolate Doom P_ZMovement: reduce viewheight by the step amount so the camera doesn't snap, then let P_CalcHeight recover it smoothly.



220
221
222
223
224
225
# File 'lib/doom/game/player_state.rb', line 220

def notify_step(step_amount)
  return if step_amount == 0

  @viewheight -= step_amount
  @deltaviewheight = (VIEWHEIGHT - @viewheight) / 8.0
end

#resetObject



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
# File 'lib/doom/game/player_state.rb', line 66

def reset
  @health = 100
  @armor = 0
  @max_health = 100
  @max_armor = 200

  # Ammo
  @ammo_bullets = 50
  @ammo_shells = 0
  @ammo_rockets = 0
  @ammo_cells = 0

  @max_bullets = 200
  @max_shells = 50
  @max_rockets = 50
  @max_cells = 300

  # Start with fist and pistol
  @weapon = WEAPON_PISTOL
  @has_weapons = [true, true, false, false, false, false, false, false]

  # No keys
  @keys = {
    blue_card: false,
    yellow_card: false,
    red_card: false,
    blue_skull: false,
    yellow_skull: false,
    red_skull: false
  }

  # Attack state
  @attacking = false
  @attack_frame = 0
  @attack_tics = 0

  # Death state
  @dead = false
  @death_tic = 0
  @damage_count = 0

  # Cheats
  @god_mode = false
  @infinite_ammo = false

  # Weapon bob
  @bob_angle = 0.0
  @bob_amount = 0.0
  @is_moving = false

  # Smooth step height (P_CalcHeight viewheight/deltaviewheight)
  @viewheight = VIEWHEIGHT
  @deltaviewheight = 0.0

  # View bob (camera bounce) - simulated momentum for P_CalcHeight
  @view_bob_offset = 0.0
  @momx = 0.0        # Simulated X momentum (map units/sec, not actual movement)
  @momy = 0.0        # Simulated Y momentum
  # Held at zero and never read by game logic, but part of the snapshot
  # wire format and state fingerprint (PLAYER_STATE_FLOATS), so removing
  # them would need a snapshot version bump. Left in place deliberately.
  @thrust_x = 0.0
  @thrust_y = 0.0
  @view_bob_angle = 0.0
end

#set_movement_momentum(momx, momy) ⇒ Object

Set movement momentum directly (called from GosuWindow with actual movement momentum, which already has thrust + friction applied).



259
260
261
262
# File 'lib/doom/game/player_state.rb', line 259

def set_movement_momentum(momx, momy)
  @momx = momx
  @momy = momy
end

#start_attackObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/doom/game/player_state.rb', line 159

def start_attack
  return unless can_attack?
  return if @attacking

  @attacking = true
  @attack_frame = 0
  @attack_tics = 0

  # Consume ammo (skipped with infinite ammo)
  return if @infinite_ammo

  case @weapon
  when WEAPON_PISTOL
    @ammo_bullets -= 1 if @ammo_bullets > 0
  when WEAPON_SHOTGUN
    @ammo_shells -= 1 if @ammo_shells > 0
  when WEAPON_CHAINGUN
    @ammo_bullets -= 1 if @ammo_bullets > 0
  when WEAPON_ROCKET
    @ammo_rockets -= 1 if @ammo_rockets > 0
  when WEAPON_PLASMA
    @ammo_cells -= 1 if @ammo_cells > 0
  when WEAPON_BFG
    @ammo_cells -= 40 if @ammo_cells >= 40
  end
end

#switch_weapon(weapon_num) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/doom/game/player_state.rb', line 293

def switch_weapon(weapon_num)
  return unless weapon_num >= 0 && weapon_num < 8
  return unless @has_weapons[weapon_num]
  return if @attacking

  @weapon = weapon_num
end

#take_damage(amount) ⇒ Object

Apply damage (from environment or enemies). Armor absorbs some.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/doom/game/player_state.rb', line 302

def take_damage(amount)
  return if @dead
  return if @god_mode

  absorbed = 0
  if @armor > 0
    absorbed = amount / 3 # Green armor absorbs 1/3
    absorbed = @armor if absorbed > @armor
    @armor -= absorbed
  end

  actual = amount - absorbed
  @health -= actual

  # Red flash proportional to damage (capped at palette 8)
  @damage_count = [(@damage_count + (actual / 2.0)).ceil, 8].min

  return unless @health <= 0

  @health = 0
  @damage_count = 8
  die
end

#update_attackObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/doom/game/player_state.rb', line 186

def update_attack
  return unless @attacking

  @attack_tics += 1

  # Calculate which frame we're on based on tics
  duration = ATTACK_DURATIONS[@weapon] || 8
  frame_count = @weapon == WEAPON_FIST ? 3 : 4

  tics_per_frame = duration / frame_count
  @attack_frame = (@attack_tics / tics_per_frame).to_i

  # Attack finished?
  return unless @attack_tics >= duration

  @attacking = false
  @attack_frame = 0
  @attack_tics = 0
end

#update_bob(delta_time) ⇒ Object



206
207
208
209
210
211
212
213
214
215
# File 'lib/doom/game/player_state.rb', line 206

def update_bob(delta_time)
  if @is_moving
    # Increase bob while moving
    @bob_angle += delta_time * 10.0
    @bob_amount = [@bob_amount + (delta_time * 16.0), 6.0].min
  else
    # Decay bob when stopped
    @bob_amount = [@bob_amount - (delta_time * 12.0), 0.0].max
  end
end

#update_damage_countObject

Decay damage flash each tic



327
328
329
# File 'lib/doom/game/player_state.rb', line 327

def update_damage_count
  @damage_count -= 1 if @damage_count > 0
end

#update_view_bob(delta_time) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/doom/game/player_state.rb', line 271

def update_view_bob(delta_time)
  dt = delta_time.clamp(0.001, 0.05)

  # P_CalcHeight: bob = (momx_per_tic^2 + momy_per_tic^2) / 4, capped at MAXBOB
  bob = ((@momx * @momx) + (@momy * @momy)) * BOB_MOM_SCALE
  bob = MAXBOB if bob > MAXBOB

  # Advance bob sine wave (FINEANGLES/20 per tic = ~11 rad/sec)
  @view_bob_angle += BOB_FREQUENCY * dt

  # viewz offset: sin(angle) * bob/2
  @view_bob_offset = Math.sin(@view_bob_angle) * bob / 2.0
end

#update_viewheightObject

Gradually restore viewheight to VIEWHEIGHT (called each tic). Matches Chocolate Doom P_CalcHeight viewheight recovery loop. For step-up: viewheight < 41, delta > 0, accelerates upward. For step-down: viewheight > 41, delta < 0, decelerates then recovers.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/doom/game/player_state.rb', line 238

def update_viewheight
  @viewheight += @deltaviewheight

  if @viewheight > VIEWHEIGHT && @deltaviewheight >= 0
    @viewheight = VIEWHEIGHT
    @deltaviewheight = 0.0
  end

  if @viewheight < VIEWHEIGHT_HALF
    @viewheight = VIEWHEIGHT_HALF
    @deltaviewheight = 1.0 if @deltaviewheight <= 0
  end

  return unless @deltaviewheight != 0

  @deltaviewheight += DELTA_ACCEL
  @deltaviewheight = 0.0 if @deltaviewheight.abs < 0.01 && (@viewheight - VIEWHEIGHT).abs < 0.5
end

#weapon_bob_xObject



285
286
287
# File 'lib/doom/game/player_state.rb', line 285

def weapon_bob_x
  Math.cos(@bob_angle) * @bob_amount
end

#weapon_bob_yObject



289
290
291
# File 'lib/doom/game/player_state.rb', line 289

def weapon_bob_y
  Math.sin(@bob_angle * 2) * @bob_amount * 0.5
end

#weapon_nameObject



132
133
134
# File 'lib/doom/game/player_state.rb', line 132

def weapon_name
  WEAPON_NAMES[@weapon]
end