Skip to content

Commit

Permalink
Add nodal system
Browse files Browse the repository at this point in the history
  • Loading branch information
t-geindre committed Oct 13, 2024
1 parent b09d869 commit 5c3cac3
Show file tree
Hide file tree
Showing 24 changed files with 579 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/examples-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
examples: [camera, scenes, shmup, squares]
examples: [camera, nodes, scenes, shmup, squares]
runs-on: ubuntu-latest
env:
DISPLAY: ':99.0'
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/examples-update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
run: |
cd examples/camera
go get github.com/t-geindre/golem@master
cd ../nodes
go get github.com/t-geindre/golem@master
cd ../scenes
go get github.com/t-geindre/golem@master
cd ../shmup
Expand Down
12 changes: 12 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ Demonstrate a simple camera system with:
$ go run github.com/t-geindre/golem/examples/camera@latest
```

## [Nodes](nodes)

Demonstrate how a nodal system can be implemented with Golem.
Each entity is a node that can have children nodes.
Geometry and transformations are inherited from parent nodes.

### Run

```bash
$ go run github.com/t-geindre/golem/examples/node@latest
```

## [Scenes](scenes)

Demonstrate a scene management system with transitions and lifecycle.
Expand Down
12 changes: 12 additions & 0 deletions examples/nodes/component/boundaries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package component

import "image"

//go:generate golem component Boundaries
type Boundaries struct {
image.Rectangle
}

func NewBoundaries(x, y, w, h int) *Boundaries {
return &Boundaries{Rectangle: image.Rect(x, y, w+x, h+y)}
}
21 changes: 21 additions & 0 deletions examples/nodes/component/boundaries_golem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions examples/nodes/component/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package component

import (
"image/color"
)

//go:generate golem component Color
type Color struct {
Background color.Color
Borders color.Color
}

func NewColor(bg, bd color.Color) *Color {
return &Color{
Background: bg,
Borders: bd,
}
}
21 changes: 21 additions & 0 deletions examples/nodes/component/color_golem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/nodes/component/geometry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package component

import "github.com/hajimehoshi/ebiten/v2"

type Geometry struct {
*ebiten.GeoM
}

//go:generate golem component Geometry
func NewGeometry() *Geometry {
return &Geometry{GeoM: &ebiten.GeoM{}}
}
21 changes: 21 additions & 0 deletions examples/nodes/component/geometry_golem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/nodes/component/velocity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package component

import "image"

//go:generate golem component Velocity
type Velocity struct {
image.Point
}

func NewVelocity(x, y int) *Velocity {
return &Velocity{Point: image.Pt(x, y)}
}
21 changes: 21 additions & 0 deletions examples/nodes/component/velocity_golem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions examples/nodes/entity/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package entity

import (
"github.com/t-geindre/golem/examples/nodes/component"
"github.com/t-geindre/golem/examples/nodes/helper"
"github.com/t-geindre/golem/pkg/golem"
)

type Node struct {
golem.Entity
golem.World
*component.Boundaries
*component.Velocity
*component.Geometry
*component.Color
}

func NewNode(l golem.LayerID, vx, vy, x, y, w, h int) *Node {
n := &Node{
Entity: golem.NewEntity(l),
World: golem.NewWorld(),
Boundaries: component.NewBoundaries(x, y, w, h),
Velocity: component.NewVelocity(vx, vy),
Geometry: component.NewGeometry(),
Color: component.NewColor(helper.RandomColor(0xcc), helper.RandomColor(0xff)),
}

n.SetParentSharedSystems(true)

return n
}
20 changes: 20 additions & 0 deletions examples/nodes/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/t-geindre/golem/examples/nodes

go 1.23.1

require (
github.com/hajimehoshi/ebiten/v2 v2.7.8
github.com/t-geindre/golem v0.0.0-20241013093716-b09d869e6333
)

require (
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect
github.com/ebitengine/hideconsole v1.0.0 // indirect
github.com/ebitengine/purego v0.7.0 // indirect
github.com/jezek/xgb v1.1.1 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
)

replace github.com/t-geindre/golem => ..\..\
21 changes: 21 additions & 0 deletions examples/nodes/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 h1:48bCqKTuD7Z0UovDfvpCn7wZ0GUZ+yosIteNDthn3FU=
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895/go.mod h1:XZdLv05c5hOZm3fM2NlJ92FyEZjnslcMcNRrhxs8+8M=
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE=
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/purego v0.7.0 h1:HPZpl61edMGCEW6XK2nsR6+7AnJ3unUxpTZBkkIXnMc=
github.com/ebitengine/purego v0.7.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/hajimehoshi/bitmapfont/v3 v3.0.0 h1:r2+6gYK38nfztS/et50gHAswb9hXgxXECYgE8Nczmi4=
github.com/hajimehoshi/bitmapfont/v3 v3.0.0/go.mod h1:+CxxG+uMmgU4mI2poq944i3uZ6UYFfAkj9V6WqmuvZA=
github.com/hajimehoshi/ebiten/v2 v2.7.8 h1:QrlvF2byCzMuDsbxFReJkOCbM3O2z1H/NKQaGcA8PKk=
github.com/hajimehoshi/ebiten/v2 v2.7.8/go.mod h1:Ulbq5xDmdx47P24EJ+Mb31Zps7vQq+guieG9mghQUaA=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/t-geindre/golem v0.0.0-20241013093716-b09d869e6333/go.mod h1:z/zykS1uWdVlm3lX5jfNkAlFXs25A4Yy2ckb10wQ0II=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
15 changes: 15 additions & 0 deletions examples/nodes/helper/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package helper

import (
"image/color"
"math/rand"
)

func RandomColor(alpha uint8) color.Color {
return color.RGBA{
R: uint8(100 + rand.Intn(155)),
G: uint8(100 + rand.Intn(155)),
B: uint8(100 + rand.Intn(155)),
A: alpha,
}
}
51 changes: 51 additions & 0 deletions examples/nodes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/t-geindre/golem/examples/nodes/entity"
"github.com/t-geindre/golem/examples/nodes/system"
"github.com/t-geindre/golem/pkg/golemutils"
"math/rand"
"time"
)

func main() {
const (
LayerAll = iota
LayerDebug
)

ww, wh := 800, 800
ebiten.SetWindowSize(ww, wh)
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)

root := entity.NewNode(LayerAll, 1, 2, 30, 30, ww-60, wh-60)
for i := 0; i < 10; i++ {
cont := entity.NewNode(LayerAll,
1+rand.Intn(5), 1+rand.Intn(5),
rand.Intn(ww-60), rand.Intn(wh-60),
200, 200)
for j := 0; j < 10; j++ {
child := entity.NewNode(LayerAll,
rand.Intn(3), rand.Intn(3),
rand.Intn(190), rand.Intn(190),
10, 10)
cont.AddEntity(child)
}
root.AddEntity(cont)
}

game := golemutils.NewGame()
game.AddLayers(LayerAll, LayerDebug)
game.AddEntity(root)

game.AddSystems(
system.NewMove(),
system.NewBounce(),
system.NewGeometry(),
system.NewRenderer(),
golemutils.NewMetrics(LayerDebug, time.Millisecond*100),
)

ebiten.RunGame(game)
}
56 changes: 56 additions & 0 deletions examples/nodes/system/bounce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package system

import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/t-geindre/golem/examples/nodes/component"
"github.com/t-geindre/golem/pkg/golem"
"image"
)

type Bounce struct {
bds image.Rectangle
}

func NewBounce() *Bounce {
return &Bounce{}
}

func (b *Bounce) UpdateOnce(w golem.World, c golem.Clock) {
ww, wh := ebiten.WindowSize()
b.bds = image.Rect(0, 0, ww, wh)
}

func (b *Bounce) Update(e golem.Entity, w golem.World, _ golem.Clock) {
eBds := component.GetBoundaries(e)
eVel := component.GetVelocity(e)

if eBds == nil || eVel == nil {
return
}

bds := b.bds
rBds := eBds.Rectangle
if p := w.GetParentEntity(); p != nil {
if pb := component.GetBoundaries(p); pb != nil {
bds = pb.Rectangle
rBds = eBds.Add(pb.Rectangle.Min)
}
}

if rBds.Min.X < bds.Min.X {
eBds.Rectangle = eBds.Rectangle.Add(image.Pt(bds.Min.X-rBds.Min.X, 0))
eVel.X = -eVel.X
}
if rBds.Min.Y < bds.Min.Y {
eBds.Rectangle = eBds.Rectangle.Add(image.Pt(0, bds.Min.Y-rBds.Min.Y))
eVel.Y = -eVel.Y
}
if rBds.Max.X > bds.Max.X {
eBds.Rectangle = eBds.Rectangle.Add(image.Pt(bds.Max.X-rBds.Max.X, 0))
eVel.X = -eVel.X
}
if rBds.Max.Y > bds.Max.Y {
eBds.Rectangle = eBds.Rectangle.Add(image.Pt(0, bds.Max.Y-rBds.Max.Y))
eVel.Y = -eVel.Y
}
}
Loading

0 comments on commit 5c3cac3

Please sign in to comment.