started with generic collision_system (not working yet)
This commit is contained in:
92
systems/collision_system.go
Normal file
92
systems/collision_system.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package systems
|
||||
|
||||
import (
|
||||
"openquell/components"
|
||||
. "openquell/components"
|
||||
"openquell/grid"
|
||||
"openquell/observers"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/mlange-42/arche/ecs"
|
||||
"github.com/mlange-42/arche/generic"
|
||||
)
|
||||
|
||||
type CollisionSystem struct {
|
||||
World *ecs.World
|
||||
Selector *generic.Filter4[Position, Velocity, Renderable, Collider]
|
||||
GridContainer *grid.GridContainer
|
||||
}
|
||||
|
||||
func NewCollisionSystem(world *ecs.World, gridcontainer *grid.GridContainer) System {
|
||||
system := &CollisionSystem{
|
||||
Selector: generic.NewFilter4[Position, Velocity, Renderable, Collider](),
|
||||
GridContainer: gridcontainer,
|
||||
World: world,
|
||||
}
|
||||
|
||||
return system
|
||||
}
|
||||
|
||||
func (system CollisionSystem) Update() error {
|
||||
entityobserver := observers.GetEntityObserver(system.World)
|
||||
|
||||
posID := ecs.ComponentID[components.Position](system.World)
|
||||
EntitiesToRemove := []ecs.Entity{}
|
||||
query := system.Selector.Query(system.World)
|
||||
|
||||
for query.Next() {
|
||||
position, velocity, _, handler := query.Get()
|
||||
if velocity.Moving() {
|
||||
// check if we bump agains the screen edge
|
||||
ok, newpos := system.GridContainer.Grid.BumpEdge(position, velocity)
|
||||
if ok {
|
||||
if handler.EdgeHandler != nil {
|
||||
if !handler.EdgeHandler(system.World, position, newpos, velocity, query.Entity()) {
|
||||
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// check if we collide with some solid entity
|
||||
ok, tilepos := system.GridContainer.Grid.GetSolidNeighborPosition(position, velocity, true)
|
||||
if ok {
|
||||
intersects, newpos := tilepos.Intersects(position, velocity)
|
||||
if intersects {
|
||||
if handler.CollisionHandler != nil {
|
||||
if !handler.CollisionHandler(system.World, position, newpos, velocity, query.Entity()) {
|
||||
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check if current entity bumps into another entity
|
||||
for foreign_entity := range entityobserver.Entities {
|
||||
if foreign_entity == query.Entity() {
|
||||
// don't check entity against itself
|
||||
continue
|
||||
}
|
||||
|
||||
foreign_position := (*Position)(system.World.Get(foreign_entity, posID))
|
||||
|
||||
ok, newpos := foreign_position.Intersects(position, velocity)
|
||||
if ok {
|
||||
if handler.CollisionHandler != nil {
|
||||
if !handler.CollisionHandler(system.World, position, newpos, velocity, foreign_entity) {
|
||||
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for _, entity := range EntitiesToRemove {
|
||||
system.World.RemoveEntity(entity)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (system *CollisionSystem) Draw(screen *ebiten.Image) {}
|
||||
@@ -34,86 +34,16 @@ func NewObstacleSystem(world *ecs.World, gridcontainer *grid.GridContainer) Syst
|
||||
func (system *ObstacleSystem) Update() error {
|
||||
playerobserver := observers.GetPlayerObserver(system.World)
|
||||
gameobserver := observers.GetGameObserver(system.World)
|
||||
obstacleobserver := observers.GetObstacleObserver(system.World)
|
||||
|
||||
if gameobserver.Lost {
|
||||
return nil
|
||||
}
|
||||
|
||||
posID := ecs.ComponentID[components.Position](system.World)
|
||||
veloID := ecs.ComponentID[components.Velocity](system.World)
|
||||
var gameover bool
|
||||
|
||||
EntitiesToRemove := []ecs.Entity{}
|
||||
|
||||
query := system.Selector.Query(system.World)
|
||||
gameover := false
|
||||
|
||||
for query.Next() {
|
||||
obsposition, obsvelocity, _, _ := query.Get()
|
||||
|
||||
// check if one player has bumped into current obstacle
|
||||
for player := range playerobserver.Entities {
|
||||
playerposition := (*Position)(system.World.Get(player, posID))
|
||||
playervelocity := (*Velocity)(system.World.Get(player, veloID))
|
||||
|
||||
ok, newpos := obsposition.Intersects(playerposition, playervelocity)
|
||||
if ok {
|
||||
// slog.Debug("bumped into obstacle", "obstacle", obstacle)
|
||||
|
||||
if CheckObstacleSide(playervelocity, obsvelocity.Direction) {
|
||||
// player died
|
||||
EntitiesToRemove = append(EntitiesToRemove, player)
|
||||
gameover = true
|
||||
} else {
|
||||
// bumped into nonlethal obstacle side, stop the
|
||||
// player, set the obstacle in motion, if possible
|
||||
slog.Debug("bump not die", "originalpos", playerposition)
|
||||
obsvelocity.Set(playervelocity)
|
||||
playervelocity.Change(Stop)
|
||||
playerposition.Set(newpos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check if current obstacle bumped into another obstacle
|
||||
for foreign_obstacle := range obstacleobserver.Entities {
|
||||
if foreign_obstacle == query.Entity() {
|
||||
// don't check obstacle against itself
|
||||
continue
|
||||
}
|
||||
|
||||
foreign_obstacle_position := (*Position)(system.World.Get(foreign_obstacle, posID))
|
||||
|
||||
ok, newpos := foreign_obstacle_position.Intersects(obsposition, obsvelocity)
|
||||
if ok {
|
||||
//slog.Debug("bumped into foreign obstacle", "obstacle", foreign_obstacle)
|
||||
obsposition.Set(newpos)
|
||||
obsvelocity.ResetDirectionAndStop()
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: this is the same loop as in player_system, unite the
|
||||
// two, just iterate over all entities with pos,vel,render, dammit
|
||||
if obsvelocity.Moving() {
|
||||
ok, tilepos := system.GridContainer.Grid.GetSolidNeighborPosition(obsposition, obsvelocity, true)
|
||||
if ok {
|
||||
intersects, newpos := tilepos.Intersects(obsposition, obsvelocity)
|
||||
if intersects {
|
||||
// slog.Debug("collision with foreign obstacle detected", "tile",
|
||||
// tilepos, "obs", obsposition, "new", newpos)
|
||||
|
||||
obsposition.Set(newpos)
|
||||
obsvelocity.ResetDirectionAndStop()
|
||||
}
|
||||
}
|
||||
|
||||
obsposition.Move(obsvelocity)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for _, entity := range EntitiesToRemove {
|
||||
system.World.RemoveEntity(entity)
|
||||
if len(playerobserver.Entities) == 0 {
|
||||
// FIXME: check this in game or elsewhere
|
||||
gameover = true
|
||||
}
|
||||
|
||||
if gameover {
|
||||
|
||||
@@ -59,7 +59,7 @@ func (system PlayerSystem) Update() error {
|
||||
// check player movements etc
|
||||
query = system.Selector.Query(system.World)
|
||||
for query.Next() {
|
||||
playerposition, velocity, player, _ := query.Get()
|
||||
_, velocity, player, _ := query.Get()
|
||||
|
||||
if !player.IsPrimary {
|
||||
continue
|
||||
@@ -96,28 +96,6 @@ func (system PlayerSystem) Update() error {
|
||||
// other keys: <tab>: switch player, etc
|
||||
}
|
||||
}
|
||||
|
||||
if velocity.Moving() {
|
||||
ok, newpos := system.GridContainer.Grid.BumpEdge(playerposition, velocity)
|
||||
if ok {
|
||||
//slog.Debug("falling off the edge", "newpos", newpos)
|
||||
playerposition.Set(newpos)
|
||||
} else {
|
||||
ok, tilepos := system.GridContainer.Grid.GetSolidNeighborPosition(playerposition, velocity, true)
|
||||
if ok {
|
||||
slog.Debug("HaveSolidNeighbor", "ok", ok, "tilepos",
|
||||
tilepos.Point(), "playerpos", playerposition)
|
||||
intersects, newpos := tilepos.Intersects(playerposition, velocity)
|
||||
if intersects {
|
||||
// slog.Debug("collision detected", "tile",
|
||||
// tilepos, "player", playerposition, "new", newpos)
|
||||
|
||||
playerposition.Set(newpos)
|
||||
velocity.Change(Stop)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
query = system.Selector.Query(system.World)
|
||||
|
||||
Reference in New Issue
Block a user