openquell/handlers/player_handler.go

73 lines
1.8 KiB
Go
Raw Normal View History

package handlers
import (
"openquell/components"
. "openquell/config"
"openquell/util"
"log/slog"
"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/arche/generic"
)
// type Collider struct {
// CollisionHandler func(*Position, *Position, *Velocity) bool
// EdgeHandler func(*Position, *Position, *Velocity) bool
// PassoverHandler func(*Position, *Velocity) bool
// PassoverFinishedHandler func(*Position, *Velocity) bool
// }
func HandlePlayerCollision(world *ecs.World, pos, newpos *components.Position,
velocity *components.Velocity, target ecs.Entity) bool {
velmapper := generic.NewMap[components.Velocity](world)
wallmapper := generic.NewMap[components.Solid](world)
obsmapper := generic.NewMap[components.Obstacle](world)
if wallmapper.Has(target) {
pos.Set(newpos)
velocity.Change(Stop)
return true
}
if obsmapper.Has(target) {
obsvelocity := velmapper.Get(target)
if CheckObstacleSide(velocity, obsvelocity.Direction) {
// player died
return false
} else {
// bumped into nonlethal obstacle side, stop the
// player, set the obstacle in motion, if possible
obsvelocity.Set(velocity)
velocity.Change(Stop)
pos.Set(newpos)
}
}
return true
}
func HandlePlayerEdgeBump(world *ecs.World, playerpos,
newpos *components.Position, velocity *components.Velocity, target ecs.Entity) bool {
playerpos.Set(newpos)
return true
}
func CheckObstacleSide(playervelocity *components.Velocity, obsdirection int) bool {
movingdirection := playervelocity.InvertDirection()
if movingdirection == Stop || movingdirection == obsdirection {
slog.Debug("Damaging obstacle collision",
"playerdirection", util.DirectionStr(playervelocity.Direction),
"obsdirection", util.DirectionStr(obsdirection),
"movingdirection", util.DirectionStr(movingdirection),
)
return true
}
return false
}