Class: Doom::Game::Player

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

Overview

A player as a first-class entity.

Before this existed the "player" was scattered across three objects: position and angle lived in Renderer, horizontal momentum lived in GosuWindow, and floor height lived in PlayerPhysics -- which made more than one of them impossible. Everything that identifies a player now lives here; Renderer is reduced to a camera that gets pointed at a pose.

Angles are radians internally (Renderer's old setters took degrees).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: 0, state: PlayerState.new) ⇒ Player

Returns a new instance of Player.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/doom/game/player.rb', line 28

def initialize(id: 0, state: PlayerState.new)
  @id = id
  @state = state
  @x = 0.0
  @y = 0.0
  @z = 41.0 # eye height
  @momx = 0.0
  @momy = 0.0
  @use_held = false
  @frags = 0
  self.angle = 0.0
  snapshot!
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



15
16
17
# File 'lib/doom/game/player.rb', line 15

def angle
  @angle
end

#cos_angleObject (readonly)

Returns the value of attribute cos_angle.



22
23
24
# File 'lib/doom/game/player.rb', line 22

def cos_angle
  @cos_angle
end

#fragsObject

Deathmatch score. It lives on the player rather than in a side table so it survives respawning, which throws away the whole PlayerState.



21
22
23
# File 'lib/doom/game/player.rb', line 21

def frags
  @frags
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/doom/game/player.rb', line 15

def id
  @id
end

#momxObject

Returns the value of attribute momx.



15
16
17
# File 'lib/doom/game/player.rb', line 15

def momx
  @momx
end

#momyObject

Returns the value of attribute momy.



15
16
17
# File 'lib/doom/game/player.rb', line 15

def momy
  @momy
end

#prev_angleObject (readonly)

Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.



26
27
28
# File 'lib/doom/game/player.rb', line 26

def prev_angle
  @prev_angle
end

#prev_xObject (readonly)

Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.



26
27
28
# File 'lib/doom/game/player.rb', line 26

def prev_x
  @prev_x
end

#prev_yObject (readonly)

Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.



26
27
28
# File 'lib/doom/game/player.rb', line 26

def prev_y
  @prev_y
end

#prev_zObject (readonly)

Previous tic's pose, kept so the renderer can interpolate between tics and stay smooth above 35 FPS.



26
27
28
# File 'lib/doom/game/player.rb', line 26

def prev_z
  @prev_z
end

#sin_angleObject (readonly)

Returns the value of attribute sin_angle.



22
23
24
# File 'lib/doom/game/player.rb', line 22

def sin_angle
  @sin_angle
end

#stateObject (readonly)

Returns the value of attribute state.



22
23
24
# File 'lib/doom/game/player.rb', line 22

def state
  @state
end

#use_heldObject

Edge-trigger latch for the use key, per player: holding the key must not spam doors, and each player needs its own latch.



18
19
20
# File 'lib/doom/game/player.rb', line 18

def use_held
  @use_held
end

#xObject

Returns the value of attribute x.



15
16
17
# File 'lib/doom/game/player.rb', line 15

def x
  @x
end

#yObject

Returns the value of attribute y.



15
16
17
# File 'lib/doom/game/player.rb', line 15

def y
  @y
end

#zObject

Returns the value of attribute z.



15
16
17
# File 'lib/doom/game/player.rb', line 15

def z
  @z
end

Instance Method Details

#angle_degrees=(degrees) ⇒ Object



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

def angle_degrees=(degrees)
  self.angle = degrees * Math::PI / 180.0
end

#dead?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/doom/game/player.rb', line 100

def dead?
  @state.dead
end

#move_to(x, y) ⇒ Object



58
59
60
61
# File 'lib/doom/game/player.rb', line 58

def move_to(x, y)
  @x = x.to_f
  @y = y.to_f
end

#place(x, y, z, angle_degrees) ⇒ Object

Place the player outright (spawn, teleport, respawn) and collapse the interpolation history so the camera doesn't sweep across the map.



65
66
67
68
69
70
71
72
73
# File 'lib/doom/game/player.rb', line 65

def place(x, y, z, angle_degrees)
  @x = x.to_f
  @y = y.to_f
  @z = z.to_f
  self.angle_degrees = angle_degrees
  @momx = 0.0
  @momy = 0.0
  snapshot!
end

#snapshot!Object

Called at the start of each tic, before the simulation moves the player.



76
77
78
79
80
81
# File 'lib/doom/game/player.rb', line 76

def snapshot!
  @prev_x = @x
  @prev_y = @y
  @prev_z = @z
  @prev_angle = @angle
end

#turn(degrees) ⇒ Object



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

def turn(degrees)
  self.angle = @angle + (degrees * Math::PI / 180.0)
end

#view_pose(alpha) ⇒ Object

Pose to render at, alpha of the way from the previous tic to this one. Angle is interpolated along the shortest arc so wrapping past +/-PI doesn't spin the view the long way round.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/doom/game/player.rb', line 86

def view_pose(alpha)
  a = alpha.clamp(0.0, 1.0)
  delta = @angle - @prev_angle
  delta -= 2 * Math::PI while delta > Math::PI
  delta += 2 * Math::PI while delta < -Math::PI

  [
    @prev_x + ((@x - @prev_x) * a),
    @prev_y + ((@y - @prev_y) * a),
    @prev_z + ((@z - @prev_z) * a),
    @prev_angle + (delta * a)
  ]
end