|
|
|
@ -2,6 +2,7 @@ package game
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"crypto/sha1" |
|
|
|
|
"image" |
|
|
|
|
"image/color" |
|
|
|
|
"log" |
|
|
|
|
"math" |
|
|
|
@ -204,6 +205,10 @@ func (g *Game) ReadInputs() InputBits {
|
|
|
|
|
return in |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var physicsRects = []image.Rectangle{ |
|
|
|
|
image.Rect(-world.GroundWidth/2, 0, world.GroundWidth/2, -world.GroundHeight), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (g *Game) applyPhysics() { |
|
|
|
|
for i := 0; i < 2; i++ { |
|
|
|
|
opp := 0 |
|
|
|
@ -213,21 +218,68 @@ func (g *Game) applyPhysics() {
|
|
|
|
|
p := &g.Players[i] |
|
|
|
|
o := &g.Players[opp] |
|
|
|
|
|
|
|
|
|
// Apply gravity.
|
|
|
|
|
if p.VY > -world.Gravity { |
|
|
|
|
playerRect := world.FloatRect(g.Players[i].X, g.Players[i].Y, g.Players[i].X+float64(component.PlayerSize), g.Players[i].Y+float64(component.PlayerSize)) |
|
|
|
|
oppRect := world.FloatRect(g.Players[opp].X, g.Players[opp].Y, g.Players[opp].X+float64(component.PlayerSize), g.Players[opp].Y+float64(component.PlayerSize)) |
|
|
|
|
|
|
|
|
|
isGrounded := func() (r image.Rectangle, ok bool) { |
|
|
|
|
for _, physRect := range physicsRects { |
|
|
|
|
if component.RectTouches(playerRect, physRect) { |
|
|
|
|
return physRect, true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return image.Rectangle{}, false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Apply velocity.
|
|
|
|
|
p.X, p.Y = p.X+p.VX, p.Y+p.VY |
|
|
|
|
|
|
|
|
|
// Apply ground collision.
|
|
|
|
|
groundRect, grounded := isGrounded() |
|
|
|
|
if grounded { |
|
|
|
|
// Min is left, Max is right
|
|
|
|
|
collideX := p.X+component.PlayerWidth >= float64(groundRect.Min.X) && p.X <= float64(groundRect.Max.X) |
|
|
|
|
|
|
|
|
|
// Min is bottom, Max is top
|
|
|
|
|
collideY := p.Y-component.PlayerHeight >= float64(groundRect.Min.Y) && p.Y <= float64(groundRect.Max.Y) |
|
|
|
|
|
|
|
|
|
log.Println(world.CurrentPlayer, collideX, collideY) |
|
|
|
|
if collideY { |
|
|
|
|
/*if collideX { |
|
|
|
|
if p.X+component.PlayerWidth >= float64(groundRect.Min.X) { |
|
|
|
|
p.X = float64(groundRect.Min.X) - component.PlayerWidth |
|
|
|
|
} else { |
|
|
|
|
p.X = float64(groundRect.Max.X) |
|
|
|
|
} |
|
|
|
|
}*/ |
|
|
|
|
if collideY { |
|
|
|
|
topDist := p.Y - float64(groundRect.Max.Y) |
|
|
|
|
bottomDist := p.Y - component.PlayerHeight - float64(groundRect.Min.Y) |
|
|
|
|
|
|
|
|
|
if math.Abs(topDist) < math.Abs(bottomDist) { // Closer to top.
|
|
|
|
|
p.Y = float64(groundRect.Max.Y) |
|
|
|
|
} else { // Closer to bottom.
|
|
|
|
|
p.Y = float64(groundRect.Min.Y) + component.PlayerHeight |
|
|
|
|
} |
|
|
|
|
p.VY = 0 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else if p.VY > -world.Gravity { // Apply gravity.
|
|
|
|
|
p.VY -= math.Max(math.Abs(p.VY/2.5), 0.1) |
|
|
|
|
if p.VY < -world.Gravity { |
|
|
|
|
p.VY = -world.Gravity |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Apply velocity.
|
|
|
|
|
p.X, p.Y = p.X+p.VX, p.Y+p.VY |
|
|
|
|
if p.VY != 0 { |
|
|
|
|
log.Println("VY", world.CurrentPlayer, p.VY) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Apply player collision.
|
|
|
|
|
playerRect := world.FloatRect(g.Players[i].X, g.Players[i].Y, g.Players[i].X+float64(component.PlayerSize), g.Players[i].Y+float64(component.PlayerSize)) |
|
|
|
|
oppRect := world.FloatRect(g.Players[opp].X, g.Players[opp].Y, g.Players[opp].X+float64(component.PlayerSize), g.Players[opp].Y+float64(component.PlayerSize)) |
|
|
|
|
/*if grounded { |
|
|
|
|
log.Println("grounded") |
|
|
|
|
p.VY = 0 // TODO
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
// Apply player collision.
|
|
|
|
|
if playerRect.Overlaps(oppRect) { |
|
|
|
|
if playerRect.Min.X < oppRect.Min.X { |
|
|
|
|
p.X = o.X - component.PlayerSize |
|
|
|
@ -235,11 +287,6 @@ func (g *Game) applyPhysics() {
|
|
|
|
|
p.X = o.X + component.PlayerSize |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Apply ground collision.
|
|
|
|
|
if p.Y < 0 { |
|
|
|
|
p.Y = 0 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|