diff --git a/cmd/test/main.go b/cmd/test/main.go index 2fb2c83..938928f 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -4,7 +4,7 @@ import ( "vultras.su/core/gg" "github.com/hajimehoshi/ebiten/v2/examples/resources/images" "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts" - _ "github.com/silbinarywolf/preferdiscretegpu" + //_ "github.com/silbinarywolf/preferdiscretegpu" "bytes" "log" "fmt" @@ -95,7 +95,6 @@ func main() { } e.Spawn(txt) - //fmt.Println(rect.GetLayer(), player.GetLayer()) fmt.Println("Starting...") err = e.Run() diff --git a/cmd/test/player.go b/cmd/test/player.go index 5368126..207e3d9 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -47,6 +47,7 @@ func (p *Player) Draw(c *Context) []gg.EVertex { } func (p *Player) Update(c *Context) { + p.Position.Y += 2 if p.Spawned { return } @@ -122,6 +123,7 @@ func (p *Player) Update(c *Context) { pp := *p counter++ p.Spawned = true + p.Collidable = false c.Spawn(&pp) } } @@ -153,25 +155,29 @@ func (p *Player) Update(c *Context) { )) }} - p.Position.Y += 2 +} + +func (p *Player) GetCollisionInterest() []gg.CollisionType { + return []gg.CollisionType{ + gg.CollisionStaticPhysics, + } } func (p *Player) Resolve(c *Context) { - col := c.Collisions[0] - fmt.Printf("frame[%d]: the col[0] len(%d): %T, %T\n", c.Frame(), len(c.Collisions), col.What, col.With) - switch col.Type{ - case gg.PhysCollision : + if !p.Spawned { + fmt.Printf("frame[%d]: the col[0] len(%d): %T, %T\n", c.Frame(), len(c.Collisions), col.What, col.With) + } + for _, col := range c.Collisions {switch col.Type{ + case gg.CollisionStaticPhysics : LOOP: for { - p.Position.Y -= 0.2 - shit, collides := gg.Collide(p, col.With) - fmt.Println("shit:", shit) - fmt.Println("do:", collides) + p.Position.Y -= 1 + _, collides := gg.Collide(p, col.With) if !collides { break LOOP } } - } + }} } diff --git a/cmd/test/rect.go b/cmd/test/rect.go index 9f37d55..b4e1542 100644 --- a/cmd/test/rect.go +++ b/cmd/test/rect.go @@ -24,6 +24,10 @@ func NewRect() *Rect { return ret } +func (r *Rect) CollisionType() gg.CollisionType { + return gg.CollisionStaticPhysics +} + func (r *Rect) Update(c *Context) { //r.R += 0.3 * e.DT() //r.Position = c.AbsCursorPosition() diff --git a/cmd/test/text.go b/cmd/test/text.go index bab9183..3030a07 100644 --- a/cmd/test/text.go +++ b/cmd/test/text.go @@ -23,5 +23,5 @@ type Text struct{ } func (txt *Text) Update(c *Context) { - txt.Data += string(c.Runes()) + //txt.Data += string(c.Runes()) } diff --git a/collider.go b/collider.go index d1326c2..526cd20 100644 --- a/collider.go +++ b/collider.go @@ -10,10 +10,14 @@ package gg //ColliderSimplify() Triangle //} +type CollisionMap map[CollisionType] []Collision + type CollisionType int const ( - PhysCollision CollisionType = iota - TriggerCollision + CollisionNo CollisionType = iota + CollisionStaticPhysics + CollisionTrigger + CollisionLast ) // The structure contains collision information. @@ -62,14 +66,19 @@ type Edger interface { type Resolver interface { IsResolvable() bool Resolve(c *Context) + GetCollisionInterest() []CollisionType Collider } type Resolvability struct { Resolvable bool + CollisionInterest []CollisionType } func (r Resolvability) IsResolvable() bool { return r.Resolvable } +func (r Resolvability) GetCollisionInterest() []CollisionType { + return r.CollisionInterest +} func Collide(c1, c2 Collider) (Collision, bool) { vertices := c1.Vertices() diff --git a/engine.go b/engine.go index 4c211fa..6cd1bd7 100644 --- a/engine.go +++ b/engine.go @@ -335,14 +335,18 @@ func (e *engine) Resolve() { Resolver Resolver input chan *Context } - colliders := []Collider{} + colliders := map[CollisionType] []Collider{} resolvers := []resHold{} for _, object := range e.Objects.store { - if object.IsCollidable() { - colliders = append(colliders, object) - } if object.IsResolvable() { + interests := object.GetCollisionInterest() + for _, interest := range interests { + _, ok := colliders[interest] + if !ok { + colliders[interest] = []Collider{} + } + } resolvers = append(resolvers, resHold{ object, object.Input(), @@ -350,8 +354,23 @@ func (e *engine) Resolve() { } } + for _, object := range e.Objects.store { + if object.IsCollidable() { + typ := object.CollisionType() + _, ok := colliders[typ] + if !ok { + continue + } + colliders[typ] = append(colliders[typ], object) + } + } + for _, resolver := range resolvers { - cols := GetCollisions(resolver.Resolver, colliders) + interests := resolver.Resolver.GetCollisionInterest() + cols := []Collision{} + for _, interest := range interests { + cols = append(cols, GetCollisions(resolver.Resolver, colliders[interest])...) + } if len(cols) > 0 { c := &Context{ typ: resolveContext, @@ -458,10 +477,14 @@ func (e *Engine) Dframe() uint { } // Return the current fixed delta time. -func (e *Engine) Dt() Duration { +func (e *Engine) RealDt() Duration { return e.dt } +func (e *Engine) Dt() Duration { + return time.Second/60 +} + func (e *Engine) Frame() uint { return e.frame } diff --git a/go.mod b/go.mod index 5bb1743..5178d06 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ toolchain go1.21.3 require ( github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b - github.com/silbinarywolf/preferdiscretegpu v1.0.0 golang.org/x/image v0.7.0 vultras.su/core/gods v0.0.0-20240106154104-c280608f8de4 ) diff --git a/go.sum b/go.sum index dd09f23..c5ed443 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,6 @@ github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b h1 github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b/go.mod h1:+fFI6Ag5YvbX1ivNQD2TxNhpWFDPuxEoew421TTQAxI= github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= -github.com/silbinarywolf/preferdiscretegpu v1.0.0 h1:tuvXLRCnoFMFyk74/8PFvO0B5rjDmXm0JgNTaOYAHT0= -github.com/silbinarywolf/preferdiscretegpu v1.0.0/go.mod h1:h3s2GkfAP2sWqoS7v/PxAlFOQ1azMRsZxUJNw47QhLc= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= diff --git a/media/player.png b/media/player.png new file mode 100644 index 0000000..9f1a87d Binary files /dev/null and b/media/player.png differ