Skip to content

Commit

Permalink
Merge 02844e5 into a5bdb4a
Browse files Browse the repository at this point in the history
  • Loading branch information
SeeratSek committed Oct 23, 2018
2 parents a5bdb4a + 02844e5 commit bcad813
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 53 deletions.
15 changes: 8 additions & 7 deletions server/models/junk.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,25 @@ func (j *Junk) UpdatePosition(height float64, width float64) {

// HitBy Update Junks's velocity based on calculations of being hit by a player
func (j *Junk) HitBy(p *Player) {
pVelocity := p.getVelocity()
// We don't want this collision till the debounce is down.
if j.Debounce != 0 {
return
}

j.Color = p.Color //Assign junk to last recently hit player color
j.Color = p.getColor() //Assign junk to last recently hit player color
j.LastPlayerHit = p

if p.Velocity.Dx < 0 {
j.Velocity.Dx = math.Min(p.Velocity.Dx*BumpFactor, -MinimumBump)
if pVelocity.Dx < 0 {
j.Velocity.Dx = math.Min(pVelocity.Dx*BumpFactor, -MinimumBump)
} else {
j.Velocity.Dx = math.Max(p.Velocity.Dx*BumpFactor, MinimumBump)
j.Velocity.Dx = math.Max(pVelocity.Dx*BumpFactor, MinimumBump)
}

if p.Velocity.Dy < 0 {
j.Velocity.Dy = math.Min(p.Velocity.Dy*BumpFactor, -MinimumBump)
if pVelocity.Dy < 0 {
j.Velocity.Dy = math.Min(pVelocity.Dy*BumpFactor, -MinimumBump)
} else {
j.Velocity.Dy = math.Max(p.Velocity.Dy*BumpFactor, MinimumBump)
j.Velocity.Dy = math.Max(pVelocity.Dy*BumpFactor, MinimumBump)
}

p.hitJunk()
Expand Down
137 changes: 91 additions & 46 deletions server/models/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func (p *Player) GetName() string {
return p.Name
}

func (p *Player) getColor() string {
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()

return p.Color
}

func (p *Player) getVelocity() Velocity {
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
Expand All @@ -95,6 +102,13 @@ func (p *Player) getControls() KeysPressed {
return p.Controls
}

func (p *Player) getAngle() float64 {
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()

return p.Angle
}

func (p *Player) getPosition() Position {
p.rwMutex.RLock()
defer p.rwMutex.RUnlock()
Expand Down Expand Up @@ -165,6 +179,13 @@ func (p *Player) setPointsDebounce(pointsDebounce int) {
p.pointsDebounce = pointsDebounce
}

func (p *Player) setPoints(points int) {
p.rwMutex.Lock()
defer p.rwMutex.Unlock()

p.Points = points
}

func (p *Player) setLastPlayerHit(playerHit *Player) {
p.rwMutex.Lock()
defer p.rwMutex.Unlock()
Expand All @@ -180,7 +201,7 @@ func GenUniqueID() string {

// AddPoints adds numPoints to player p
func (p *Player) AddPoints(numPoints int) {
p.Points = p.Points + numPoints
p.setPoints(p.Points + numPoints)
}

// SendJSON sends JSON data through the player's websocket connection
Expand All @@ -205,16 +226,16 @@ func (p *Player) UpdatePosition(height float64, width float64) {
controlsVector := Velocity{0, 0}

if p.getControls().Left {
p.setAngle(math.Mod(p.Angle+0.1, 360))
p.setAngle(math.Mod(p.getAngle()+0.1, 360))
}

if p.getControls().Right {
p.setAngle(math.Mod(p.Angle-0.1, 360))
p.setAngle(math.Mod(p.getAngle()-0.1, 360))
}

if p.getControls().Up {
controlsVector.Dy = (0.5 * PlayerRadius * math.Cos(p.Angle))
controlsVector.Dx = (0.5 * PlayerRadius * math.Sin(p.Angle))
controlsVector.Dy = (0.5 * PlayerRadius * math.Cos(p.getAngle()))
controlsVector.Dx = (0.5 * PlayerRadius * math.Sin(p.getAngle()))
}

controlsVector.normalize()
Expand Down Expand Up @@ -260,88 +281,112 @@ func (p *Player) UpdatePosition(height float64, width float64) {
}

func (p *Player) hitJunk() {
p.Velocity.Dx *= JunkBounceFactor
p.Velocity.Dy *= JunkBounceFactor
velocityVector := p.getVelocity()
velocityVector.Dx *= JunkBounceFactor
velocityVector.Dy *= JunkBounceFactor
p.setVelocity(velocityVector)
}

// HitPlayer calculates collision, update Player's velocity based on calculation of hitting another player
func (p *Player) HitPlayer(ph *Player) {
if p.pDebounce != 0 {
if p.getPDebounce() != 0 {
return
}

initalVelocity := p.Velocity
pInitialVelocity := p.getVelocity()
pVelocity := pInitialVelocity
phVelocity := ph.getVelocity()

//Calculate player's new velocity
p.Velocity.Dx = (p.Velocity.Dx * -VelocityTransferFactor) + (ph.Velocity.Dx * VelocityTransferFactor)
p.Velocity.Dy = (p.Velocity.Dy * -VelocityTransferFactor) + (ph.Velocity.Dy * VelocityTransferFactor)

//Calculate the player you hits new velocity
ph.Velocity.Dx = (ph.Velocity.Dx * -VelocityTransferFactor) + (initalVelocity.Dx * VelocityTransferFactor)
ph.Velocity.Dy = (ph.Velocity.Dy * -VelocityTransferFactor) + (initalVelocity.Dy * VelocityTransferFactor)

ph.LastPlayerHit = p
p.LastPlayerHit = ph
p.pointsDebounce = PointsDebounceTicks
ph.pointsDebounce = PointsDebounceTicks
p.pDebounce = PlayerDebounceTicks
pVelocity.Dx = (pVelocity.Dx * -VelocityTransferFactor) + (phVelocity.Dx * VelocityTransferFactor)
pVelocity.Dy = (pVelocity.Dy * -VelocityTransferFactor) + (phVelocity.Dy * VelocityTransferFactor)

//Calculate hit player's new velocity
phVelocity.Dx = (phVelocity.Dx * -VelocityTransferFactor) + (pInitialVelocity.Dx * VelocityTransferFactor)
phVelocity.Dy = (phVelocity.Dy * -VelocityTransferFactor) + (pInitialVelocity.Dy * VelocityTransferFactor)

p.setVelocity(pVelocity)
ph.setVelocity(phVelocity)
ph.setLastPlayerHit(p)
p.setLastPlayerHit(ph)
p.setPointsDebounce(PointsDebounceTicks)
ph.setPointsDebounce(PointsDebounceTicks)
p.setPDebounce(PlayerDebounceTicks)
}

// ApplyGravity applys a vector towards given position
func (p *Player) ApplyGravity(h *Hole) {
gravityVector := Velocity{0, 0}
gravityVector.Dx = h.Position.X - p.Position.X
gravityVector.Dy = h.Position.Y - p.Position.Y
pVelocity := p.getVelocity()
pPosition := p.getPosition()

gravityVector.Dx = h.Position.X - pPosition.X
gravityVector.Dy = h.Position.Y - pPosition.Y

inverseMagnitude := 1.0 / gravityVector.magnitude()
gravityVector.normalize()

//Velocity is affected by how close you are, the size of the hole, and a damping factor.
p.Velocity.Dx += gravityVector.Dx * inverseMagnitude * h.Radius * gravityDamping
p.Velocity.Dy += gravityVector.Dy * inverseMagnitude * h.Radius * gravityDamping
pVelocity.Dx += gravityVector.Dx * inverseMagnitude * h.Radius * gravityDamping
pVelocity.Dy += gravityVector.Dy * inverseMagnitude * h.Radius * gravityDamping

p.setVelocity(pVelocity)
}

// checkWalls check if the player is attempting to exit the walls, reverse they're direction
// checkWalls if the player is attempting to exit the walls, reverse their direction
func (p *Player) checkWalls(height float64, width float64) {
if p.Position.X+PlayerRadius > width {
p.Position.X = width - PlayerRadius - 1
p.Velocity.Dx *= WallBounceFactor
} else if p.Position.X-PlayerRadius < 0 {
p.Velocity.Dx *= WallBounceFactor
p.Position.X = PlayerRadius + 1
positionVector := p.getPosition()
velocityVector := p.getVelocity()
if positionVector.X+PlayerRadius > width {
positionVector.X = width - PlayerRadius - 1
velocityVector.Dx *= WallBounceFactor
} else if positionVector.X-PlayerRadius < 0 {
positionVector.X = PlayerRadius + 1
velocityVector.Dx *= WallBounceFactor
}

if p.Position.Y+PlayerRadius > height {
p.Velocity.Dy *= WallBounceFactor
p.Position.Y = height - PlayerRadius - 1
} else if p.Position.Y-PlayerRadius < 0 {
p.Velocity.Dy *= WallBounceFactor
p.Position.Y = PlayerRadius + 1
if positionVector.Y+PlayerRadius > height {
positionVector.Y = height - PlayerRadius - 1
velocityVector.Dy *= WallBounceFactor
} else if positionVector.Y-PlayerRadius < 0 {
positionVector.Y = PlayerRadius + 1
velocityVector.Dy *= WallBounceFactor
}

p.setPosition(positionVector)
p.setVelocity(velocityVector)
}

// KeyDownHandler sets this players given key as pressed down
func (p *Player) KeyDownHandler(key int) {
pControls := p.getControls()

if key == RightKey {
p.Controls.Right = true
pControls.Right = true
} else if key == LeftKey {
p.Controls.Left = true
pControls.Left = true
} else if key == UpKey {
p.Controls.Up = true
pControls.Up = true
} else if key == DownKey {
p.Controls.Down = true
pControls.Down = true
}

p.setControls(pControls)
}

// KeyUpHandler sets this players given key as released
func (p *Player) KeyUpHandler(key int) {
pControls := p.getControls()

if key == RightKey {
p.Controls.Right = false
pControls.Right = false
} else if key == LeftKey {
p.Controls.Left = false
pControls.Left = false
} else if key == UpKey {
p.Controls.Up = false
pControls.Up = false
} else if key == DownKey {
p.Controls.Down = false
pControls.Down = false
}

p.setControls(pControls)
}

0 comments on commit bcad813

Please sign in to comment.