From c1a2d6b7419f26bb58f037c2530417b31da44f18 Mon Sep 17 00:00:00 2001 From: surdeus Date: Mon, 3 Jun 2024 00:27:11 +0500 Subject: [PATCH] fix: ... --- cmd/test/camera.go | 12 ++++++++++++ cmd/test/grid.go | 3 +-- cmd/test/main.go | 18 ++++++++++-------- cmd/test/text.go | 2 +- cmd/test/trianlge.go | 44 +++++++++++--------------------------------- engine.go | 9 ++++++--- ox/camera.go | 2 +- ox/transform.go | 5 +++-- 8 files changed, 45 insertions(+), 50 deletions(-) diff --git a/cmd/test/camera.go b/cmd/test/camera.go index a4c430e..348e191 100644 --- a/cmd/test/camera.go +++ b/cmd/test/camera.go @@ -41,6 +41,18 @@ func (cam *Camera) OnUpdate(c gg.Context) { cam.Move(mov.AbsDelta) } + for _, btn := range c.Events().Mouse.ButtonDowns { + switch btn.MouseButton { + case gg.MouseButtonLeft : + t := NewTri(ox.T().SetPosition( + c.Engine().GetAbsCursorPosition(), + )) + t.Spawned = true + c.Engine().Spawn(t) + } + } + + // Scale. if wheel != nil { k := 1.5 diff --git a/cmd/test/grid.go b/cmd/test/grid.go index f5ed743..654e5e8 100644 --- a/cmd/test/grid.go +++ b/cmd/test/grid.go @@ -5,7 +5,6 @@ import "surdeus.su/core/gg/ox" import "surdeus.su/core/gg/mx" type Grid struct { - ox.ObjectImpl ox.Sprite Cage, Width mx.Float } @@ -26,7 +25,7 @@ func (grid *Grid) OnUpdate(c Context) { func NewGrid() *Grid { grid := &Grid{} - grid.Transform = ox.T() + grid.Transform = *ox.T() grid.SetAround(mx.V2(0)) grid.Layer = LayerBackground grid.Images[0] = gg.NewImage(720, 480) diff --git a/cmd/test/main.go b/cmd/test/main.go index 5c3354d..f0d83f2 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -80,7 +80,6 @@ func main() { }) camera = NewCamera() e.SetCamera(camera) - e.Spawn(camera) var err error playerImg, err = gg.LoadImage(bytes.NewReader(images.Runner_png)) @@ -101,12 +100,8 @@ func main() { panic(err) } player = NewPlayer() - tri = NewTri() + tri = NewTri(ox.T()) - e.Spawn(&Debug{}) - e.Spawn(player) - e.Spawn(rect) - e.Spawn(tri) /*circle := &gg.Circle{} circle.Transform = gg.T() @@ -117,8 +112,6 @@ func main() { circle.Visible = true*/ //e.Spawn(circle) - e.Spawn(NewGrid()) - text, err := NewText() if err != nil { panic(err) @@ -127,6 +120,15 @@ func main() { //fmt.Println(rect.GetLayer(), player.GetLayer()) fmt.Println("Starting...") + objects := []gg.Object{ + camera, &Debug{}, + + player, rect, tri, + NewGrid(), text, + } + for _, object := range objects { + e.Spawn(object) + } err = e.Run() if err != nil { panic(err) diff --git a/cmd/test/text.go b/cmd/test/text.go index 4ea62ae..acd1422 100644 --- a/cmd/test/text.go +++ b/cmd/test/text.go @@ -15,7 +15,7 @@ type Text struct { func NewText() (*Text, error) { txt := &Text{} - txt.Transform = ox.T() + txt.Transform = *ox.T() //txt.Position = gg.V2(400) txt.Color = gg.RGBA(1, 1, 1, 1) txt.Layer = LayerHighest diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index bb22223..8a6fcd0 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -4,7 +4,7 @@ import "surdeus.su/core/gg" import "surdeus.su/core/gg/ox" import "surdeus.su/core/gg/mx" //import "fmt" -//import "log" +import "log" var ( counter int @@ -15,10 +15,9 @@ type Tri struct { Spawned bool } -func NewTri() *Tri { +func NewTri(t *ox.Transform) *Tri { ret := &Tri{} - ret.Transform = ox.T() - ret.SetPosition(mx.V2(-100)) + ret.Transform = *t ret.Triangles = mx.Triangles{ mx.Triangle{ mx.V(0, 10), @@ -28,38 +27,18 @@ func NewTri() *Tri { } ret.Color = gg.RGBA(1, 1, 0, 1) ret.Visible = true - //ret.Collidable = true - //ret.Type = gg.CollisionStaticPhysics ret.Layer = LayerTriangle - //ret.Connect(player) return ret } -func (t *Tri) OnUpdate(c Context) { - //log.Println("onup:", t.IsVisible()) - //redges := rect.Edges() - //tedges := t.Edges() - //crosses, ok := tedges.CrossWithEdges(redges) - //fmt.Println("shit:", crosses, ok) +func (t *Tri) OnStart(c Context) { + log.Println("added a tri") +} - if false { - //fmt.Println("edges:", tedges) - } - /*col, hasCol := gg.Collide(t, rect) - if hasCol && len(col.Crosses) > 0 { - fmt.Println("col:", col) - }*/ +func (t *Tri) OnUpdate(c Context) { e := c.Engine() dt := e.DT().Seconds() - /*if !t.ContainedPoints(gg.Points{c.AbsCursorPosition()}).Empty() { - t.Color = gg.Rgba(0, 1, 0, 1) - } else { - t.Color = gg.Rgba(1, 0, 1, 1) - }*/ - if t.Spawned { - return - } keys := e.GetKeyboardKeys() @@ -82,11 +61,6 @@ func (t *Tri) OnUpdate(c Context) { if t.Spawned { break } - /*t.Triangles = append(t.Triangles, gg.Triangle{ - gg.V(0, 10+ mx.Float(counter)), - gg.V(100+ mx.Float(counter), 0), - gg.V(0, -10-gg.Float(counter)), - })*/ case gg.Key3: if t.Spawned { break @@ -111,3 +85,7 @@ func (t *Tri) OnUpdate(c Context) { } } } + +func (tri *Tri) Draw(c Context) *gg.Drawing { + return tri.DrawablePolygon.Draw(c) +} diff --git a/engine.go b/engine.go index d575917..3962a70 100644 --- a/engine.go +++ b/engine.go @@ -139,8 +139,8 @@ func (e *Engine) Spawn(object Object) bool { ctx := Context{ engine: e, } - ok := e.objects.add(object) object.OnStart(ctx) + ok := e.objects.add(object) return ok } @@ -151,8 +151,8 @@ func (e *Engine) Delete(object Object) bool { ctx := Context{ engine: e, } - ok := e.objects.remove(object) object.OnDelete(ctx) + ok := e.objects.remove(object) return ok } @@ -328,6 +328,9 @@ func (e *engine) Update() error { events: events, } for _, object := range e.objects.store { + if object == nil { + continue + } object.OnUpdate(c) } @@ -358,7 +361,7 @@ func (e *engine) Draw(img *ebiten.Image) { m := map[Layer][]Drawer{} for _, object := range eng.objects.store { // Skipping the ones we do not need to draw. - if !object.IsVisible() { + if object == nil || !object.IsVisible() { continue } l := object.GetLayer() diff --git a/ox/camera.go b/ox/camera.go index 72f6e39..aeef96d 100644 --- a/ox/camera.go +++ b/ox/camera.go @@ -25,7 +25,7 @@ type Camera struct { // with default settings. func NewCamera() Camera { ret := Camera{} - ret.Transform = T() + ret.Transform = *T() return ret } diff --git a/ox/transform.go b/ox/transform.go index ed6bda9..6a42ae0 100644 --- a/ox/transform.go +++ b/ox/transform.go @@ -30,14 +30,15 @@ type Transform struct { } // Returns the default Transform structure. -func T() Transform { - ret := Transform{ +func T() *Transform { + ret := &Transform{ // Rotate around scale: mx.V2(1), // Rotate around the center. around: mx.V2(.5), } ret.dirty = true + ret.parentDirty = true return ret }