This commit is contained in:
Andrey Parhomenko 2024-06-03 00:27:11 +05:00
parent 0abd485f55
commit c1a2d6b741
8 changed files with 45 additions and 50 deletions

View file

@ -41,6 +41,18 @@ func (cam *Camera) OnUpdate(c gg.Context) {
cam.Move(mov.AbsDelta) 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. // Scale.
if wheel != nil { if wheel != nil {
k := 1.5 k := 1.5

View file

@ -5,7 +5,6 @@ import "surdeus.su/core/gg/ox"
import "surdeus.su/core/gg/mx" import "surdeus.su/core/gg/mx"
type Grid struct { type Grid struct {
ox.ObjectImpl
ox.Sprite ox.Sprite
Cage, Width mx.Float Cage, Width mx.Float
} }
@ -26,7 +25,7 @@ func (grid *Grid) OnUpdate(c Context) {
func NewGrid() *Grid { func NewGrid() *Grid {
grid := &Grid{} grid := &Grid{}
grid.Transform = ox.T() grid.Transform = *ox.T()
grid.SetAround(mx.V2(0)) grid.SetAround(mx.V2(0))
grid.Layer = LayerBackground grid.Layer = LayerBackground
grid.Images[0] = gg.NewImage(720, 480) grid.Images[0] = gg.NewImage(720, 480)

View file

@ -80,7 +80,6 @@ func main() {
}) })
camera = NewCamera() camera = NewCamera()
e.SetCamera(camera) e.SetCamera(camera)
e.Spawn(camera)
var err error var err error
playerImg, err = gg.LoadImage(bytes.NewReader(images.Runner_png)) playerImg, err = gg.LoadImage(bytes.NewReader(images.Runner_png))
@ -101,12 +100,8 @@ func main() {
panic(err) panic(err)
} }
player = NewPlayer() player = NewPlayer()
tri = NewTri() tri = NewTri(ox.T())
e.Spawn(&Debug{})
e.Spawn(player)
e.Spawn(rect)
e.Spawn(tri)
/*circle := &gg.Circle{} /*circle := &gg.Circle{}
circle.Transform = gg.T() circle.Transform = gg.T()
@ -117,8 +112,6 @@ func main() {
circle.Visible = true*/ circle.Visible = true*/
//e.Spawn(circle) //e.Spawn(circle)
e.Spawn(NewGrid())
text, err := NewText() text, err := NewText()
if err != nil { if err != nil {
panic(err) panic(err)
@ -127,6 +120,15 @@ func main() {
//fmt.Println(rect.GetLayer(), player.GetLayer()) //fmt.Println(rect.GetLayer(), player.GetLayer())
fmt.Println("Starting...") fmt.Println("Starting...")
objects := []gg.Object{
camera, &Debug{},
player, rect, tri,
NewGrid(), text,
}
for _, object := range objects {
e.Spawn(object)
}
err = e.Run() err = e.Run()
if err != nil { if err != nil {
panic(err) panic(err)

View file

@ -15,7 +15,7 @@ type Text struct {
func NewText() (*Text, error) { func NewText() (*Text, error) {
txt := &Text{} txt := &Text{}
txt.Transform = ox.T() txt.Transform = *ox.T()
//txt.Position = gg.V2(400) //txt.Position = gg.V2(400)
txt.Color = gg.RGBA(1, 1, 1, 1) txt.Color = gg.RGBA(1, 1, 1, 1)
txt.Layer = LayerHighest txt.Layer = LayerHighest

View file

@ -4,7 +4,7 @@ import "surdeus.su/core/gg"
import "surdeus.su/core/gg/ox" import "surdeus.su/core/gg/ox"
import "surdeus.su/core/gg/mx" import "surdeus.su/core/gg/mx"
//import "fmt" //import "fmt"
//import "log" import "log"
var ( var (
counter int counter int
@ -15,10 +15,9 @@ type Tri struct {
Spawned bool Spawned bool
} }
func NewTri() *Tri { func NewTri(t *ox.Transform) *Tri {
ret := &Tri{} ret := &Tri{}
ret.Transform = ox.T() ret.Transform = *t
ret.SetPosition(mx.V2(-100))
ret.Triangles = mx.Triangles{ ret.Triangles = mx.Triangles{
mx.Triangle{ mx.Triangle{
mx.V(0, 10), mx.V(0, 10),
@ -28,38 +27,18 @@ func NewTri() *Tri {
} }
ret.Color = gg.RGBA(1, 1, 0, 1) ret.Color = gg.RGBA(1, 1, 0, 1)
ret.Visible = true ret.Visible = true
//ret.Collidable = true
//ret.Type = gg.CollisionStaticPhysics
ret.Layer = LayerTriangle ret.Layer = LayerTriangle
//ret.Connect(player)
return ret return ret
} }
func (t *Tri) OnUpdate(c Context) { func (t *Tri) OnStart(c Context) {
//log.Println("onup:", t.IsVisible()) log.Println("added a tri")
//redges := rect.Edges() }
//tedges := t.Edges()
//crosses, ok := tedges.CrossWithEdges(redges)
//fmt.Println("shit:", crosses, ok)
if false { func (t *Tri) OnUpdate(c Context) {
//fmt.Println("edges:", tedges)
}
/*col, hasCol := gg.Collide(t, rect)
if hasCol && len(col.Crosses) > 0 {
fmt.Println("col:", col)
}*/
e := c.Engine() e := c.Engine()
dt := e.DT().Seconds() 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() keys := e.GetKeyboardKeys()
@ -82,11 +61,6 @@ func (t *Tri) OnUpdate(c Context) {
if t.Spawned { if t.Spawned {
break 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: case gg.Key3:
if t.Spawned { if t.Spawned {
break break
@ -111,3 +85,7 @@ func (t *Tri) OnUpdate(c Context) {
} }
} }
} }
func (tri *Tri) Draw(c Context) *gg.Drawing {
return tri.DrawablePolygon.Draw(c)
}

View file

@ -139,8 +139,8 @@ func (e *Engine) Spawn(object Object) bool {
ctx := Context{ ctx := Context{
engine: e, engine: e,
} }
ok := e.objects.add(object)
object.OnStart(ctx) object.OnStart(ctx)
ok := e.objects.add(object)
return ok return ok
} }
@ -151,8 +151,8 @@ func (e *Engine) Delete(object Object) bool {
ctx := Context{ ctx := Context{
engine: e, engine: e,
} }
ok := e.objects.remove(object)
object.OnDelete(ctx) object.OnDelete(ctx)
ok := e.objects.remove(object)
return ok return ok
} }
@ -328,6 +328,9 @@ func (e *engine) Update() error {
events: events, events: events,
} }
for _, object := range e.objects.store { for _, object := range e.objects.store {
if object == nil {
continue
}
object.OnUpdate(c) object.OnUpdate(c)
} }
@ -358,7 +361,7 @@ func (e *engine) Draw(img *ebiten.Image) {
m := map[Layer][]Drawer{} m := map[Layer][]Drawer{}
for _, object := range eng.objects.store { for _, object := range eng.objects.store {
// Skipping the ones we do not need to draw. // Skipping the ones we do not need to draw.
if !object.IsVisible() { if object == nil || !object.IsVisible() {
continue continue
} }
l := object.GetLayer() l := object.GetLayer()

View file

@ -25,7 +25,7 @@ type Camera struct {
// with default settings. // with default settings.
func NewCamera() Camera { func NewCamera() Camera {
ret := Camera{} ret := Camera{}
ret.Transform = T() ret.Transform = *T()
return ret return ret
} }

View file

@ -30,14 +30,15 @@ type Transform struct {
} }
// Returns the default Transform structure. // Returns the default Transform structure.
func T() Transform { func T() *Transform {
ret := Transform{ ret := &Transform{
// Rotate around // Rotate around
scale: mx.V2(1), scale: mx.V2(1),
// Rotate around the center. // Rotate around the center.
around: mx.V2(.5), around: mx.V2(.5),
} }
ret.dirty = true ret.dirty = true
ret.parentDirty = true
return ret return ret
} }