Tons of experimental updates.
This commit is contained in:
126
updater.go
Normal file
126
updater.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"loading/objects"
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
)
|
||||
|
||||
// Update proceeds the game state.
|
||||
// Update is called every tick (1/60 [s] by default).
|
||||
func (g *Game) Update() error {
|
||||
// Write your game's logical update.
|
||||
g.counter++
|
||||
|
||||
g.UpdatePlayer()
|
||||
//g.UpdateEnemies()
|
||||
g.UpdateBobbles()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) UpdateBobbles() {
|
||||
|
||||
for i := 0; i < len(g.bobbles); i++ {
|
||||
bobby := &g.bobbles[i]
|
||||
bobby.Radius = objects.GetDefaultRadius() + float32(10*math.Sin(float64(g.counter)/(math.Pi*4)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (g *Game) UpdatePlayer() {
|
||||
|
||||
//update player angle based on mouse position
|
||||
mx, my := ebiten.CursorPosition()
|
||||
playerAngle := float32(math.Atan2(float64(float32(my)-g.player.Pose.Y), float64(float32(mx)-g.player.Pose.X)))
|
||||
g.player.Angle = playerAngle
|
||||
|
||||
//update position based on wasd input
|
||||
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
|
||||
step := float32(10)
|
||||
for _, k := range g.keys {
|
||||
switch k {
|
||||
case ebiten.KeyW:
|
||||
g.player.Pose.Y = g.player.Pose.Y - step
|
||||
case ebiten.KeyA:
|
||||
g.player.Pose.X = g.player.Pose.X - step
|
||||
case ebiten.KeyS:
|
||||
g.player.Pose.Y = g.player.Pose.Y + step
|
||||
case ebiten.KeyD:
|
||||
g.player.Pose.X = g.player.Pose.X + step
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (g *Game) UpdateEnemies() {
|
||||
/*
|
||||
we're going to rotate the enemies so they slowly face the player, to do this we'll find the angle delta
|
||||
between the player and the enemy via atan2, then add approximately one quarter of this value to the
|
||||
current enemy angle
|
||||
*/
|
||||
/*
|
||||
for _, e := range g.enemies {
|
||||
//diff := float32(math.Atan2(float64(g.player.Pose.Y-e.Pose.Y), float64(g.player.Pose.X-e.Pose.X)))
|
||||
e.Angle = e.Angle + math.Pi/4
|
||||
}
|
||||
*/
|
||||
|
||||
for i := 0; i < len(g.enemies); i++ {
|
||||
enemy := &g.enemies[i]
|
||||
|
||||
//find distancees between enemy and player
|
||||
dx := float64(g.player.Pose.X - enemy.Pose.X)
|
||||
dy := float64(g.player.Pose.Y - enemy.Pose.Y)
|
||||
|
||||
//current sign
|
||||
currentSign := enemy.DeltaAngle / float32(math.Abs(float64(enemy.DeltaAngle)))
|
||||
|
||||
targetAngle := float32(math.Atan2(dy, dx)) + math.Pi*2
|
||||
|
||||
//_, absTargetAngle := math.Modf(float64(targetAngle))
|
||||
|
||||
phi := float32(math.Mod(float64(targetAngle-enemy.Angle), float64(2*math.Pi)))
|
||||
if phi > math.Pi {
|
||||
enemy.DeltaAngle = math.Pi - phi
|
||||
} else {
|
||||
enemy.DeltaAngle = phi
|
||||
}
|
||||
|
||||
//enemy.DeltaAngle = float32(math.Min(2*math.Pi-math.Abs(float64(enemy.Angle-targetAngle)), math.Abs(float64(enemy.Angle-targetAngle))))
|
||||
|
||||
enemy.Angle = enemy.Angle + enemy.DeltaAngle/16
|
||||
|
||||
enemy.SignFlip = currentSign
|
||||
|
||||
for j := 0; j < len(g.enemies); j++ {
|
||||
if j == i {
|
||||
continue
|
||||
}
|
||||
|
||||
xdist := (g.enemies[j].Pose.X - enemy.Pose.X)
|
||||
ydist := (g.enemies[j].Pose.Y - enemy.Pose.Y)
|
||||
|
||||
//quick boundary check before we get to the algebra
|
||||
if xdist > enemy.Buffer || ydist > enemy.Buffer {
|
||||
continue
|
||||
}
|
||||
|
||||
dist := float32(math.Sqrt(float64(xdist*xdist + ydist*ydist)))
|
||||
if dist < enemy.Buffer {
|
||||
enemy.Pose.X = enemy.Pose.X + dist*float32(math.Cos(float64(targetAngle)))
|
||||
enemy.Pose.Y = enemy.Pose.Y + dist*float32(math.Sin(float64(targetAngle)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enemy.Pose.X = enemy.Pose.X + float32(dx)/360
|
||||
enemy.Pose.Y = enemy.Pose.Y + float32(dy)/360
|
||||
|
||||
//enemy.Angle = enemy.Angle + enemy.DeltaAngle/(math.Pi/4)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user