Skip to content

Commit

Permalink
Refactored Responder to CustomGame
Browse files Browse the repository at this point in the history
Fixes #35.

This means all mouse movement is now saved in the Mouse variable.
(not yet tested for "drag" movement)

Updated all examples to reflect changes.

Systems now have a map of entities - note that &System{} has now been
replaced by NewSystem() - to initialize the map. Fixes #56.

engi now has a SetTitle function exposed, which sets the window title.

RenderSystem now also stops rendering stuff that isn't part of the
RenderSystem any more. Fixes #57.
  • Loading branch information
EtienneBruines committed Nov 7, 2015
1 parent 024fdfc commit 8eb339a
Show file tree
Hide file tree
Showing 28 changed files with 293 additions and 275 deletions.
2 changes: 1 addition & 1 deletion animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type AnimationSystem struct {
}

func (a *AnimationSystem) New() {
a.System = &System{}
a.System = NewSystem()
}

func (AnimationSystem) Type() string {
Expand Down
2 changes: 1 addition & 1 deletion assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func NewTexture(img Image) *Texture {
}

func (t *Texture) Render(b *Batch, render *RenderComponent, space *SpaceComponent) {
Wo.Batch(render.Priority).Draw(t,
world.batch(render.Priority).Draw(t,
space.Position.X, space.Position.Y,
0, 0,
render.Scale.X, render.Scale.Y,
Expand Down
2 changes: 1 addition & 1 deletion audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (AudioSystem) Type() string {
}

func (as *AudioSystem) New() {
as.System = &System{}
as.System = NewSystem()

if as.HeightModifier == 0 {
as.HeightModifier = defaultHeightModifier
Expand Down
10 changes: 5 additions & 5 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// BenchmarkEmpty creates the game, and measures the runtime of a single frame, w/o anything set up
func BenchmarkEmpty(b *testing.B) {
preload := func(w *World) {}
preload := func() {}
setup := func(w *World) {}
Bench(b, preload, setup)
}
Expand All @@ -15,7 +15,7 @@ func BenchmarkEmpty(b *testing.B) {
func BenchmarkSystem10(b *testing.B) {
const count = 10

preload := func(w *World) {}
preload := func() {}
setup := func(w *World) {
for i := 0; i < count; i++ {
w.AddSystem(&NilSystem{})
Expand All @@ -28,7 +28,7 @@ func BenchmarkSystem10(b *testing.B) {
func BenchmarkSystem1000(b *testing.B) {
const count = 1000

preload := func(w *World) {}
preload := func() {}
setup := func(w *World) {
for i := 0; i < count; i++ {
w.AddSystem(&NilSystem{})
Expand All @@ -41,7 +41,7 @@ func BenchmarkSystem1000(b *testing.B) {
func BenchmarkEntity10(b *testing.B) {
const count = 10

preload := func(w *World) {}
preload := func() {}
setup := func(w *World) {
w.AddSystem(&NilSystem{})
for i := 0; i < count; i++ {
Expand All @@ -55,7 +55,7 @@ func BenchmarkEntity10(b *testing.B) {
func BenchmarkEntity1000(b *testing.B) {
const count = 1000

preload := func(w *World) {}
preload := func() {}
setup := func(w *World) {
w.AddSystem(&NilSystem{})
for i := 0; i < count; i++ {
Expand Down
2 changes: 1 addition & 1 deletion camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (cameraSystem) Type() string {
}

func (cam *cameraSystem) New() {
cam.System = &System{}
cam.System = NewSystem()

cam.x = WorldBounds.Max.X / 2
cam.y = WorldBounds.Max.Y / 2
Expand Down
38 changes: 36 additions & 2 deletions camera_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (*KeyboardScroller) Type() string {

func (c *KeyboardScroller) New() {
if !c.isSetup {
c.System = &System{}
c.System = NewSystem()
c.isSetup = true
}
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func (*EdgeScroller) Type() string {

func (c *EdgeScroller) New() {
if !c.isSetup {
c.System = &System{}
c.System = NewSystem()
c.isSetup = true
}
}
Expand Down Expand Up @@ -127,3 +127,37 @@ func NewEdgeScroller(scrollSpeed float32, margin float64) *EdgeScroller {
es.AddEntity(NewEntity([]string{es.Type()}))
return es
}

// MouseZoomer is a Systemer that allows for zooming when the scroll wheel is used
type MouseZoomer struct {
*System
zoomSpeed float32

isSetup bool
}

func (*MouseZoomer) Type() string {
return "MouseZoomer"
}

func (c *MouseZoomer) New() {
if !c.isSetup {
c.System = NewSystem()
c.isSetup = true
}
}

func (c *MouseZoomer) Update(entity *Entity, dt float32) {
if Mouse.ScrollY != 0 {
Mailbox.Dispatch(CameraMessage{ZAxis, Mouse.ScrollY * c.zoomSpeed, true})
}
}

func NewMouseZoomer(zoomSpeed float32) *MouseZoomer {
es := &MouseZoomer{
zoomSpeed: zoomSpeed,
}
es.New()
es.AddEntity(NewEntity([]string{es.Type()}))
return es
}
23 changes: 9 additions & 14 deletions demos/animation/anim.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var (
)

type GameWorld struct {
engi.World
RUN_ACTION *engi.AnimationAction
WALK_ACTION *engi.AnimationAction
STOP_ACTION *engi.AnimationAction
Expand All @@ -20,7 +19,6 @@ type GameWorld struct {
}

func (game *GameWorld) Preload() {
game.New()
engi.Files.Add("assets/hero.png")
game.STOP_ACTION = &engi.AnimationAction{Name: "stop", Frames: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
game.RUN_ACTION = &engi.AnimationAction{Name: "run", Frames: []int{16, 17, 18, 19, 20, 21}}
Expand All @@ -30,19 +28,20 @@ func (game *GameWorld) Preload() {
game.actions = []*engi.AnimationAction{game.DIE_ACTION, game.STOP_ACTION, game.WALK_ACTION, game.RUN_ACTION, game.SKILL_ACTION}
}

func (game *GameWorld) Setup() {
func (game *GameWorld) Setup(w *engi.World) {
engi.SetBg(0xFFFFFF)

game.AddSystem(&engi.RenderSystem{})
game.AddSystem(&engi.AnimationSystem{})
w.AddSystem(&engi.RenderSystem{})
w.AddSystem(&engi.AnimationSystem{})
w.AddSystem(engi.NewMouseZoomer(zoomSpeed))

spriteSheet := engi.NewSpritesheetFromFile("hero.png", 150, 150)

game.AddEntity(game.CreateEntity(&engi.Point{0, 0}, spriteSheet, game.RUN_ACTION))
game.AddEntity(game.CreateEntity(&engi.Point{300, 0}, spriteSheet, game.WALK_ACTION))
game.AddEntity(game.CreateEntity(&engi.Point{600, 0}, spriteSheet, game.STOP_ACTION))
game.AddEntity(game.CreateEntity(&engi.Point{900, 0}, spriteSheet, game.SKILL_ACTION))
game.AddEntity(game.CreateEntity(&engi.Point{1200, 0}, spriteSheet, game.DIE_ACTION))
w.AddEntity(game.CreateEntity(&engi.Point{0, 0}, spriteSheet, game.RUN_ACTION))
w.AddEntity(game.CreateEntity(&engi.Point{300, 0}, spriteSheet, game.WALK_ACTION))
w.AddEntity(game.CreateEntity(&engi.Point{600, 0}, spriteSheet, game.STOP_ACTION))
w.AddEntity(game.CreateEntity(&engi.Point{900, 0}, spriteSheet, game.SKILL_ACTION))
w.AddEntity(game.CreateEntity(&engi.Point{1200, 0}, spriteSheet, game.DIE_ACTION))
}

func (game *GameWorld) CreateEntity(point *engi.Point, spriteSheet *engi.Spritesheet, action *engi.AnimationAction) *engi.Entity {
Expand All @@ -60,10 +59,6 @@ func (game *GameWorld) CreateEntity(point *engi.Point, spriteSheet *engi.Sprites
return entity
}

func (game *GameWorld) Scroll(amount float32) {
engi.Mailbox.Dispatch(engi.CameraMessage{Axis: engi.ZAxis, Value: amount * zoomSpeed, Incremental: true})
}

func main() {
World = &GameWorld{}
engi.Open("Animation Demo", 1024, 640, false, World)
Expand Down
42 changes: 18 additions & 24 deletions demos/audio/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (moveSystem) Type() string {
}

func (ms *moveSystem) New() {
ms.System = &engi.System{}
ms.System = engi.NewSystem()
}

func (ms *moveSystem) Update(entity *engi.Entity, dt float32) {
Expand All @@ -37,11 +37,10 @@ func (ms *moveSystem) Update(entity *engi.Entity, dt float32) {

var (
zoomSpeed float32 = -0.125
World *GameWorld
World *Game
)

type GameWorld struct {
engi.World
type Game struct {
RUN_ACTION *engi.AnimationAction
WALK_ACTION *engi.AnimationAction
STOP_ACTION *engi.AnimationAction
Expand All @@ -50,8 +49,7 @@ type GameWorld struct {
actions []*engi.AnimationAction
}

func (game *GameWorld) Preload() {
game.New()
func (game *Game) Preload() {
engi.Files.Add("assets/hero.png")
engi.Files.Add("assets/326488.wav")
engi.Files.Add("assets/326064.wav")
Expand All @@ -63,45 +61,41 @@ func (game *GameWorld) Preload() {
game.actions = []*engi.AnimationAction{game.DIE_ACTION, game.STOP_ACTION, game.WALK_ACTION, game.RUN_ACTION, game.SKILL_ACTION}
}

func (game *GameWorld) Setup() {
func (game *Game) Setup(w *engi.World) {
engi.SetBg(0xFFFFFF)

game.AddSystem(&engi.RenderSystem{})
game.AddSystem(&engi.AnimationSystem{})
game.AddSystem(&engi.AudioSystem{})
game.AddSystem(&moveSystem{})
game.AddSystem(engi.NewEdgeScroller(800, 20))
w.AddSystem(&engi.RenderSystem{})
w.AddSystem(&engi.AnimationSystem{})
w.AddSystem(&engi.AudioSystem{})
w.AddSystem(&moveSystem{})
w.AddSystem(engi.NewEdgeScroller(800, 20))
w.AddSystem(engi.NewMouseZoomer(zoomSpeed))

spriteSheet := engi.NewSpritesheetFromFile("hero.png", 150, 150)

game.AddEntity(game.CreateEntity(&engi.Point{600, 0}, spriteSheet, game.STOP_ACTION))
w.AddEntity(game.CreateEntity(&engi.Point{600, 0}, spriteSheet, game.STOP_ACTION))

backgroundMusic := engi.NewEntity([]string{"AudioSystem"})
backgroundMusic.AddComponent(&engi.AudioComponent{File: "326488.wav", Repeat: true, Background: true})
game.AddEntity(backgroundMusic)
w.AddEntity(backgroundMusic)
}

func (game *GameWorld) CreateEntity(point *engi.Point, spriteSheet *engi.Spritesheet, action *engi.AnimationAction) *engi.Entity {
func (game *Game) CreateEntity(point *engi.Point, spriteSheet *engi.Spritesheet, action *engi.AnimationAction) *engi.Entity {
entity := engi.NewEntity([]string{"AudioSystem", "AnimationSystem", "RenderSystem", "moveSystem"})

space := engi.SpaceComponent{*point, 0, 0}
space := &engi.SpaceComponent{*point, 0, 0}
render := engi.NewRenderComponent(spriteSheet.Renderable(action.Frames[0]), engi.Point{3, 3}, "hero")
animation := engi.NewAnimationComponent(spriteSheet.Renderables(), 0.1)
animation.AddAnimationActions(game.actions)
animation.SelectAnimationByAction(action)
entity.AddComponent(&render)
entity.AddComponent(&space)
entity.AddComponent(render)
entity.AddComponent(space)
entity.AddComponent(animation)

return entity
}

func (game *GameWorld) Scroll(amount float32) {
engi.Mailbox.Dispatch(engi.CameraMessage{Axis: engi.ZAxis, Value: amount * zoomSpeed, Incremental: true})

}

func main() {
World = &GameWorld{}
World = &Game{}
engi.Open("Audio Demo", 1024, 640, false, World)
}
15 changes: 7 additions & 8 deletions demos/edgescroller/escroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/paked/engi"
)

type Game struct {
engi.World
}
type Game struct{}

var (
scrollSpeed float32 = 700
Expand Down Expand Up @@ -51,19 +49,20 @@ func generateBackground() *engi.Entity {
return field
}

func (game *Game) Preload() {}

// Setup is called before the main loop is started
func (game *Game) Setup() {
func (game *Game) Setup(w *engi.World) {
engi.SetBg(0x222222)
game.AddSystem(&engi.RenderSystem{})
w.AddSystem(&engi.RenderSystem{})

// The most important line in this whole demo:
game.AddSystem(engi.NewEdgeScroller(scrollSpeed, edgeMargin))
w.AddSystem(engi.NewEdgeScroller(scrollSpeed, edgeMargin))

// Create the background; this way we'll see when we actually scroll
game.AddEntity(generateBackground())
w.AddEntity(generateBackground())
}

func main() {
engi.SetFPSLimit(120)
engi.Open("EdgeScroller Demo", 400, 400, false, &Game{})
}
12 changes: 4 additions & 8 deletions demos/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ import (

var World *GameWorld

type GameWorld struct {
engi.World
}
type GameWorld struct{}

func (game *GameWorld) Preload() {
game.New()

// Load all files from the data directory. Do not do it recursively.
engi.Files.AddFromDir("data", false)

log.Println("Preloaded")
}

func (game *GameWorld) Setup() {
func (game *GameWorld) Setup(w *engi.World) {
engi.SetBg(0x2d3739)

game.AddSystem(&engi.RenderSystem{})
w.AddSystem(&engi.RenderSystem{})

// Create an entity part of the Render and Scale systems
guy := engi.NewEntity([]string{"RenderSystem", "ScaleSystem"})
Expand All @@ -42,7 +38,7 @@ func (game *GameWorld) Setup() {
guy.AddComponent(render)
guy.AddComponent(space)

game.AddEntity(guy)
w.AddEntity(guy)
}

func main() {
Expand Down
Loading

0 comments on commit 8eb339a

Please sign in to comment.