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.



73
74
75
# File 'lib/doom/game/player_state.rb', line 73

def initialize
  reset
end

Instance Attribute Details

#ammo_bulletsObject

Returns the value of attribute ammo_bullets.



42
43
44
# File 'lib/doom/game/player_state.rb', line 42

def ammo_bullets
  @ammo_bullets
end

#ammo_cellsObject

Returns the value of attribute ammo_cells.



42
43
44
# File 'lib/doom/game/player_state.rb', line 42

def ammo_cells
  @ammo_cells
end

#ammo_rocketsObject

Returns the value of attribute ammo_rockets.



42
43
44
# File 'lib/doom/game/player_state.rb', line 42

def ammo_rockets
  @ammo_rockets
end

#ammo_shellsObject

Returns the value of attribute ammo_shells.



42
43
44
# File 'lib/doom/game/player_state.rb', line 42

def ammo_shells
  @ammo_shells
end

#armorObject

Returns the value of attribute armor.



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

def armor
  @armor
end

#attack_frameObject

Returns the value of attribute attack_frame.



46
47
48
# File 'lib/doom/game/player_state.rb', line 46

def attack_frame
  @attack_frame
end

#attack_ticsObject

Returns the value of attribute attack_tics.



46
47
48
# File 'lib/doom/game/player_state.rb', line 46

def attack_tics
  @attack_tics
end

#attackingObject

Returns the value of attribute attacking.



46
47
48
# File 'lib/doom/game/player_state.rb', line 46

def attacking
  @attacking
end

#bob_amountObject

Returns the value of attribute bob_amount.



47
48
49
# File 'lib/doom/game/player_state.rb', line 47

def bob_amount
  @bob_amount
end

#bob_angleObject

Returns the value of attribute bob_angle.



47
48
49
# File 'lib/doom/game/player_state.rb', line 47

def bob_angle
  @bob_angle
end

#damage_countObject

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



50
51
52
# File 'lib/doom/game/player_state.rb', line 50

def damage_count
  @damage_count
end

#deadObject

Returns the value of attribute dead.



49
50
51
# File 'lib/doom/game/player_state.rb', line 49

def dead
  @dead
end

#death_ticObject

Returns the value of attribute death_tic.



49
50
51
# File 'lib/doom/game/player_state.rb', line 49

def death_tic
  @death_tic
end

#deltaviewheightObject (readonly)

Returns the value of attribute deltaviewheight.



57
58
59
# File 'lib/doom/game/player_state.rb', line 57

def deltaviewheight
  @deltaviewheight
end

#god_modeObject

Returns the value of attribute god_mode.



51
52
53
# File 'lib/doom/game/player_state.rb', line 51

def god_mode
  @god_mode
end

#has_weaponsObject

Returns the value of attribute has_weapons.



44
45
46
# File 'lib/doom/game/player_state.rb', line 44

def has_weapons
  @has_weapons
end

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#infinite_ammoObject

Returns the value of attribute infinite_ammo.



51
52
53
# File 'lib/doom/game/player_state.rb', line 51

def infinite_ammo
  @infinite_ammo
end

#is_movingObject

Returns the value of attribute is_moving.



48
49
50
# File 'lib/doom/game/player_state.rb', line 48

def is_moving
  @is_moving
end

#keysObject

Returns the value of attribute keys.



45
46
47
# File 'lib/doom/game/player_state.rb', line 45

def keys
  @keys
end

#max_armorObject

Returns the value of attribute max_armor.



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

def max_armor
  @max_armor
end

#max_bulletsObject

Returns the value of attribute max_bullets.



43
44
45
# File 'lib/doom/game/player_state.rb', line 43

def max_bullets
  @max_bullets
end

#max_cellsObject

Returns the value of attribute max_cells.



43
44
45
# File 'lib/doom/game/player_state.rb', line 43

def max_cells
  @max_cells
end

#max_healthObject

Returns the value of attribute max_health.



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

def max_health
  @max_health
end

#max_rocketsObject

Returns the value of attribute max_rockets.



43
44
45
# File 'lib/doom/game/player_state.rb', line 43

def max_rockets
  @max_rockets
end

#max_shellsObject

Returns the value of attribute max_shells.



43
44
45
# File 'lib/doom/game/player_state.rb', line 43

def max_shells
  @max_shells
end

#view_bob_offsetObject (readonly)

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



71
72
73
# File 'lib/doom/game/player_state.rb', line 71

def view_bob_offset
  @view_bob_offset
end

#viewheightObject (readonly)

Returns the value of attribute viewheight.



57
58
59
# File 'lib/doom/game/player_state.rb', line 57

def viewheight
  @viewheight
end

#weaponObject

Returns the value of attribute weapon.



44
45
46
# File 'lib/doom/game/player_state.rb', line 44

def weapon
  @weapon
end

Instance Method Details

#can_attack?Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
# File 'lib/doom/game/player_state.rb', line 174

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/doom/game/player_state.rb', line 144

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



357
358
359
360
361
362
# File 'lib/doom/game/player_state.rb', line 357

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

#health_levelObject



308
309
310
311
312
313
314
315
316
317
# File 'lib/doom/game/player_state.rb', line 308

def health_level
  # 0 = dying, 4 = full health
  case @health
  when 80..200 then 4
  when 60..79 then 3
  when 40..59 then 2
  when 20..39 then 1
  else 0
  end
end

#max_ammo_for_weaponObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/doom/game/player_state.rb', line 159

def max_ammo_for_weapon
  case @weapon
  when WEAPON_PISTOL, WEAPON_CHAINGUN
    @max_bullets
  when WEAPON_SHOTGUN
    @max_shells
  when WEAPON_ROCKET
    @max_rockets
  when WEAPON_PLASMA, WEAPON_BFG
    @max_cells
  else
    nil
  end
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.



243
244
245
246
247
# File 'lib/doom/game/player_state.rb', line 243

def notify_step(step_amount)
  return if step_amount == 0
  @viewheight -= step_amount
  @deltaviewheight = (VIEWHEIGHT - @viewheight) / 8.0
end

#resetObject



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/doom/game/player_state.rb', line 77

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
  @thrust_x = 0.0    # Per-frame thrust input (raw, before normalization)
  @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).



274
275
276
277
# File 'lib/doom/game/player_state.rb', line 274

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

#start_attackObject



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

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



319
320
321
322
323
324
325
# File 'lib/doom/game/player_state.rb', line 319

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.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/doom/game/player_state.rb', line 328

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

  if @health <= 0
    @health = 0
    @damage_count = 8
    die
  end
end

#update_attackObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/doom/game/player_state.rb', line 209

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?
  if @attack_tics >= duration
    @attacking = false
    @attack_frame = 0
    @attack_tics = 0
  end
end

#update_bob(delta_time) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/doom/game/player_state.rb', line 229

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



353
354
355
# File 'lib/doom/game/player_state.rb', line 353

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

#update_view_bob(delta_time) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/doom/game/player_state.rb', line 286

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.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/doom/game/player_state.rb', line 253

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

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

#weapon_bob_xObject



300
301
302
# File 'lib/doom/game/player_state.rb', line 300

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

#weapon_bob_yObject



304
305
306
# File 'lib/doom/game/player_state.rb', line 304

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

#weapon_nameObject



140
141
142
# File 'lib/doom/game/player_state.rb', line 140

def weapon_name
  WEAPON_NAMES[@weapon]
end