diff --git a/engo/TrafficManager/assets/city.png b/engo/TrafficManager/assets/city.png new file mode 100644 index 0000000..577cf37 Binary files /dev/null and b/engo/TrafficManager/assets/city.png differ diff --git a/engo/TrafficManager/systems/citySystem.go b/engo/TrafficManager/systems/citySystem.go new file mode 100644 index 0000000..b1ddfaf --- /dev/null +++ b/engo/TrafficManager/systems/citySystem.go @@ -0,0 +1,86 @@ +package systems + +import ( + "engo.io/ecs" + "engo.io/engo" + "engo.io/engo/common" + "fmt" + "log" +) + +type System interface { + // Update is ran every frame, with `dt` being the time + // in seconds since the last frame + Update(dt float32) + + // Remove removes an Entity from the System + Remove(ecs.BasicEntity) +} + +type City struct { + ecs.BasicEntity + common.RenderComponent + common.SpaceComponent +} + +type MouseTracker struct { + ecs.BasicEntity + common.MouseComponent +} + +type CityBuildingSystem struct { + mouseTracker MouseTracker + + world *ecs.World +} + +// Remove is called whenever an Entity is removed from the World, in order to remove it from this sytem as well +func (*CityBuildingSystem) Remove(ecs.BasicEntity) {} + +// Update is ran every frame, with `dt` being the time +// in seconds since the last frame +func (cb *CityBuildingSystem) Update(dt float32) { + if engo.Input.Button("AddCity").JustPressed() { + fmt.Println("The gamer pressed F1") + + city := City{BasicEntity: ecs.NewBasic()} + + city.SpaceComponent = common.SpaceComponent{ + Position: engo.Point{cb.mouseTracker.MouseX, cb.mouseTracker.MouseY}, + Width: 30, + Height: 64, + } + + texture, err := common.LoadedSprite("textures/city.png") + if err != nil { + log.Println("Unable to load texture: " + err.Error()) + } + + city.RenderComponent = common.RenderComponent{ + Drawable: texture, + Scale: engo.Point{X: 0.1, Y: 0.1}, + } + + for _, system := range cb.world.Systems() { + switch sys := system.(type) { + case *common.RenderSystem: + sys.Add(&city.BasicEntity, &city.RenderComponent, &city.SpaceComponent) + } + } + } +} + +func (cb *CityBuildingSystem) New(w *ecs.World) { + cb.world = w + fmt.Println("CityBuildingSystem was added to the Scene") + + cb.mouseTracker.BasicEntity = ecs.NewBasic() + cb.mouseTracker.MouseComponent = common.MouseComponent{Track: true} + + for _, system := range w.Systems() { + switch sys := system.(type) { + case *common.MouseSystem: + sys.Add(&cb.mouseTracker.BasicEntity, &cb.mouseTracker.MouseComponent, nil, nil) + } + } +} \ No newline at end of file diff --git a/engo/TrafficManager/traffic.go b/engo/TrafficManager/traffic.go new file mode 100644 index 0000000..65aa2b7 --- /dev/null +++ b/engo/TrafficManager/traffic.go @@ -0,0 +1,47 @@ +package main + +import ( + "engo.io/engo" + "engo.io/engo/common" + "engo.io/ecs" + "image/color" + "./systems" +) + +type myScene struct {} + +type City struct { + ecs.BasicEntity + common.RenderComponent + common.SpaceComponent +} + + +// Type uniquely defines your game type +func (*myScene) Type() string { return "myGame" } + +// Preload is called before loading any assets from the disk, +// to allow you to register / queue them +func (*myScene) Preload() { + engo.Files.Load("city.png") +} + +// Setup is called before the main loop starts. It allows you +// to add entities and systems to your Scene. +func (*myScene) Setup(world *ecs.World) { + engo.Input.RegisterButton("AddCity", engo.F1) + common.SetBackground(color.White) + world.AddSystem(&common.RenderSystem{}) + world.AddSystem(&common.MouseSystem{}) + + world.AddSystem(&systems.CityBuildingSystem{}) +} + +func main() { + opts := engo.RunOptions{ + Title: "Hello World", + Width: 400, + Height: 400, + } + engo.Run(opts, &myScene{}) +} \ No newline at end of file