started with generic collision_system (not working yet)
This commit is contained in:
25
handlers/collectible_handler.go
Normal file
25
handlers/collectible_handler.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"openquell/components"
|
||||
. "openquell/config"
|
||||
)
|
||||
|
||||
// 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 HandleCollectibleCollision(pos, newpos *components.Position, velocity *components.Velocity) bool {
|
||||
pos.Set(newpos)
|
||||
velocity.Change(Stop)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func HandleCollectibleEdgeBump(pos, newpos *components.Position, velocity *components.Velocity) bool {
|
||||
pos.Set(newpos)
|
||||
return true
|
||||
}
|
||||
72
handlers/player_handler.go
Normal file
72
handlers/player_handler.go
Normal file
@@ -0,0 +1,72 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user