Skip to content

Commit

Permalink
common: add Player.PositionEyes() func + fix Player.Position() docs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-wa authored Jun 4, 2021
1 parent 79c99ea commit 5a23bf9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/demoinfocs/common/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ func (p *Player) ViewDirectionY() float32 {
}

// Position returns the in-game coordinates.
// Like the ones you get from cl_showpos 1.
// Note: the Z value is not on the player's eye height but instead at his feet.
// See also PositionEyes().
func (p *Player) Position() r3.Vector {
if p.Entity == nil {
return r3.Vector{}
Expand All @@ -294,6 +295,20 @@ func (p *Player) Position() r3.Vector {
return p.Entity.Position()
}

// PositionEyes returns the player's position with the Z value at eye height.
// This is what you get from cl_showpos 1.
// See lso Position().
func (p *Player) PositionEyes() r3.Vector {
if p.Entity == nil {
return r3.Vector{}
}

pos := p.Position()
pos.Z += float64(p.Entity.PropertyValueMust("localdata.m_vecViewOffset[2]").FloatVal)

return pos
}

// Velocity returns the player's velocity.
func (p *Player) Velocity() r3.Vector {
if p.Entity == nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/demoinfocs/common/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@ func TestPlayer_Position_EntityNil(t *testing.T) {
assert.Empty(t, pl.Position())
}

func TestPlayer_PositionEyes(t *testing.T) {
entity := entityWithProperty("localdata.m_vecViewOffset[2]", st.PropertyValue{FloatVal: 2})
pos := r3.Vector{X: 1, Y: 2, Z: 3}

entity.On("Position").Return(pos)

pl := &Player{Entity: entity}

assert.Equal(t, r3.Vector{X: 1, Y: 2, Z: 5}, pl.PositionEyes())
}

func TestPlayer_PositionEyes_EntityNil(t *testing.T) {
pl := new(Player)

assert.Empty(t, pl.PositionEyes())
}

func TestPlayer_Velocity(t *testing.T) {
entity := new(stfake.Entity)
entity.On("PropertyValueMust", "localdata.m_vecVelocity[0]").Return(st.PropertyValue{FloatVal: 1})
Expand Down

0 comments on commit 5a23bf9

Please sign in to comment.