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



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

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

#deltaviewheightObject (readonly)

Returns the value of attribute deltaviewheight.



54
55
56
# File 'lib/doom/game/player_state.rb', line 54

def deltaviewheight
  @deltaviewheight
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

#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



68
69
70
# File 'lib/doom/game/player_state.rb', line 68

def view_bob_offset
  @view_bob_offset
end

#viewheightObject (readonly)

Returns the value of attribute viewheight.



54
55
56
# File 'lib/doom/game/player_state.rb', line 54

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



162
163
164
165
166
167
# File 'lib/doom/game/player_state.rb', line 162

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

  ammo = current_ammo
  ammo && ammo > 0
end

#current_ammoObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/doom/game/player_state.rb', line 132

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

#health_levelObject



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

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/doom/game/player_state.rb', line 147

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.



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

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

#resetObject



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

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

  # 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).



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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/doom/game/player_state.rb', line 169

def start_attack
  return unless can_attack?
  return if @attacking

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

  # Consume 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



304
305
306
307
308
309
310
# File 'lib/doom/game/player_state.rb', line 304

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

#update_attackObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/doom/game/player_state.rb', line 194

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



214
215
216
217
218
219
220
221
222
223
# File 'lib/doom/game/player_state.rb', line 214

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_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

  if @deltaviewheight != 0
    @deltaviewheight += DELTA_ACCEL
    @deltaviewheight = 0.0 if @deltaviewheight.abs < 0.01 && (@viewheight - VIEWHEIGHT).abs < 0.5
  end
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



128
129
130
# File 'lib/doom/game/player_state.rb', line 128

def weapon_name
  WEAPON_NAMES[@weapon]
end