Skip to content

Commit

Permalink
#39 - adds docs to camera stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
co0p committed Apr 23, 2023
1 parent 5dbd22c commit 5a351ad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
16 changes: 11 additions & 5 deletions app/camera/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@ import (
)

type CameraDemo struct {
game.GameSceneWithCamera
game *game.Game
game.GameSceneWithCamera // type embedding

cameraComponent *components.Camera
game *game.Game
cameraComponent *components.Camera // to control the camera
}

func (s *CameraDemo) Init() error {

w, h := s.game.WindowSize()
s.Camera = *camera.NewCamera(w, h)
s.cameraComponent = &components.Camera{Zoom: 1, CameraMode: camera.CameraModeCenter}
s.cameraComponent = &components.Camera{
Zoom: 1.0,
CameraMode: camera.CameraModeDefault,
}

s.Systems = append(s.Systems,
&systems.SpriteRenderer{EntityManager: &s.EntityManager},
&systems.Controller{EntityManager: &s.EntityManager},
&systems.MovementSystem{EntityManager: &s.EntityManager},
&systems.TextRenderer{EntityManager: &s.EntityManager},
&systems.PerformanceMonitor{EntityManager: &s.EntityManager},

systems.NewCameraSystem(&s.EntityManager, &s.Camera),
)

// the objects int the world
fps := s.EntityManager.NewEntity()
game.FPSCounter(fps, 1024)

Expand Down Expand Up @@ -77,7 +82,8 @@ func main() {
game.AddScene("Demo", &demo)
game.SetScene("Demo")

ebiten.SetFullscreen(true)
ebiten.SetFullscreen(false)
ebiten.SetWindowSize(800, 600)

if err := ebiten.RunGame(game); err != nil {
log.Fatalf("failed to start game: %s", err)
Expand Down
4 changes: 3 additions & 1 deletion game/ecs/systems/cameraSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)

// CameraSystem controls the camera by reading cameraComponent and setting values on the actual camera
type CameraSystem struct {
EntityManager *ecs.EntityManager
camera *camera.Camera
Expand All @@ -26,7 +27,8 @@ func (s *CameraSystem) Draw(screen *ebiten.Image) {}

func (s *CameraSystem) Update() error {

entities := s.EntityManager.FindByComponents(components.TransformType, components.CameraType)
// find the entity with the camera attached and positioned in the world
entities := s.EntityManager.FindByComponents(components.CameraType, components.TransformType)

if len(entities) != 1 {
return errors.New("expected exactly 1 entity with camera attached")
Expand Down
1 change: 1 addition & 0 deletions game/sceneWithCamera.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (s *GameSceneWithCamera) Init() error {
func (s *GameSceneWithCamera) Draw(screen *ebiten.Image) {

s.Camera.Surface.Clear()

for _, v := range s.Systems {
v.Draw(s.Camera.Surface)
}
Expand Down

0 comments on commit 5a351ad

Please sign in to comment.