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)
}
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

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)
}

View file

@ -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()

View file

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

View file

@ -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
}