From 428e2d175cf7f4def4099be8f893de9275d811c3 Mon Sep 17 00:00:00 2001 From: stevenhowes <38082088+stevenhowes@users.noreply.github.com> Date: Sun, 27 Mar 2022 17:02:32 +0100 Subject: [PATCH] Moveable viewport. Absolute positioning (for UI etc) is possible. Added distance-based bounder for projectiles etc as 'screen' is no longer a suitable bounder. Removed debug msg. --- component_animator.go | 9 +++---- component_bounder_distance.go | 40 +++++++++++++++++++++++++++++++ component_bounder_screen.go | 11 +++++++-- component_sprite_renderer.go | 7 +++++- component_spritesheet_renderer.go | 7 +++++- element.go | 22 +++++++++-------- goretro.go | 16 ++++++++----- 7 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 component_bounder_distance.go diff --git a/component_animator.go b/component_animator.go index f79ca08..133bf79 100644 --- a/component_animator.go +++ b/component_animator.go @@ -9,7 +9,6 @@ package GoRetro import ( "encoding/json" - "fmt" "os" "time" @@ -65,12 +64,16 @@ func (an *animator) onUpdate() error { } func (an *animator) onDraw() error { + Position := an.container.Position + if !an.container.PositionAbsolute { + Position = vectorAdd(an.container.Position, ViewPort.Position) + } return drawTexture( an.tex, VectorInt32{an.sequences[an.current].frames[an.sequences[an.current].frame].W, an.sequences[an.current].frames[an.sequences[an.current].frame].H}, VectorInt32{an.sequences[an.current].frames[an.sequences[an.current].frame].X, an.sequences[an.current].frames[an.sequences[an.current].frame].Y}, - an.container.Position, + Position, an.container.Rotation, an.container.Renderer) } @@ -131,8 +134,6 @@ func NewSequence( } defer jsonFile.Close() - fmt.Println(seq.frames) - seq.frame = 0 seq.sampleRate = sampleRate seq.loop = loop diff --git a/component_bounder_distance.go b/component_bounder_distance.go new file mode 100644 index 0000000..a635b44 --- /dev/null +++ b/component_bounder_distance.go @@ -0,0 +1,40 @@ +package GoRetro + +/* + * -------------------- + * BounderDistance + * -------------------- + * A bounder which triggers if the distance from spawn to current exceeds range + */ + +type bounderDistance struct { + container *Element + callbackFunc func(element *Element) + maxrange float64 +} + +func NewBounderDistance(container *Element, callback func(element *Element), maxrange float64) *bounderDistance { + return &bounderDistance{ + container: container, + callbackFunc: callback, + maxrange: maxrange, + } +} + +func (bounder *bounderDistance) onDraw() error { + return nil +} + +func (bounder *bounderDistance) onUpdate() error { + b := bounder.container + + if vectorDistance(b.Position, b.SpawnPosition) > bounder.maxrange { + bounder.callbackFunc(bounder.container) + } + + return nil +} + +func (bounder *bounderDistance) onCollision(other *Element) error { + return nil +} diff --git a/component_bounder_screen.go b/component_bounder_screen.go index 30ed7d3..4a71434 100644 --- a/component_bounder_screen.go +++ b/component_bounder_screen.go @@ -1,10 +1,13 @@ package GoRetro +import "fmt" + /* * -------------------- * BounderScreen * -------------------- * A bounder which triggers if the element isn't on the screen + * TODO: Use dimensions of the entity */ type bounderScreen struct { @@ -13,6 +16,10 @@ type bounderScreen struct { } func NewBounderScreen(container *Element, callback func(element *Element)) *bounderScreen { + if !container.PositionAbsolute { + fmt.Println("Added screen bounder to non-absolute positioned element") + } + return &bounderScreen{ container: container, callbackFunc: callback, @@ -26,8 +33,8 @@ func (bounder *bounderScreen) onDraw() error { func (bounder *bounderScreen) onUpdate() error { b := bounder.container - if b.Position.X > float64(Config.ScreenWidth) || b.Position.X < 0 || - b.Position.Y > float64(Config.ScreenHeight) || b.Position.Y < 0 { + if b.Position.X > float64(Config.WindowSize.X) || b.Position.X < 0 || + b.Position.Y > float64(Config.WindowSize.Y) || b.Position.Y < 0 { bounder.callbackFunc(bounder.container) } diff --git a/component_sprite_renderer.go b/component_sprite_renderer.go index 4bd5f98..6ae20fa 100644 --- a/component_sprite_renderer.go +++ b/component_sprite_renderer.go @@ -46,11 +46,16 @@ func (sr *spriteRenderer) onUpdate() error { } func (sr *spriteRenderer) onDraw() error { + Position := sr.container.Position + if !sr.container.PositionAbsolute { + Position = vectorAdd(sr.container.Position, ViewPort.Position) + } + return drawTexture( sr.tex, VectorInt32{-1, -1}, VectorInt32{0, 0}, - sr.container.Position, + Position, sr.container.Rotation, sr.container.Renderer) } diff --git a/component_spritesheet_renderer.go b/component_spritesheet_renderer.go index 8a5fc68..ddc0998 100644 --- a/component_spritesheet_renderer.go +++ b/component_spritesheet_renderer.go @@ -42,11 +42,16 @@ func (sr *spriteSheetRenderer) onUpdate() error { } func (sr *spriteSheetRenderer) onDraw() error { + Position := ViewPort.Position + if !sr.container.PositionAbsolute { + Position = vectorAdd(sr.container.Position, ViewPort.Position) + } + return drawTexture( sr.tex, sr.size, sr.sheetposition, - sr.container.Position, + Position, sr.container.Rotation, sr.container.Renderer) } diff --git a/element.go b/element.go index 55ae529..88f4370 100644 --- a/element.go +++ b/element.go @@ -14,16 +14,18 @@ type component interface { } type Element struct { - Renderer *sdl.Renderer - Position Vector - Rotation float64 - Active bool - Delete bool - Collisions []Circle - components []component - parentElement *Element - ZIndex int - CollisionLayer int + Renderer *sdl.Renderer + Position Vector + SpawnPosition Vector + PositionAbsolute bool + Rotation float64 + Active bool + Delete bool + Collisions []Circle + components []component + parentElement *Element + ZIndex int + CollisionLayer int } var Elements []*Element diff --git a/goretro.go b/goretro.go index 6b0ea4d..32190e7 100644 --- a/goretro.go +++ b/goretro.go @@ -7,10 +7,13 @@ import ( "github.com/veandco/go-sdl2/sdl" ) -var Config struct { - ScreenWidth int32 - ScreenHeight int32 +var ViewPort struct { + Size Vector + Position Vector +} +var Config struct { + WindowSize VectorInt32 TargetTicksPerSecond float64 DebugStatePrintSeconds float64 @@ -27,12 +30,12 @@ func Init() (*sdl.Renderer, *sdl.Window) { return nil, nil } - fmt.Printf("%d x %d", Config.ScreenWidth, Config.ScreenHeight) + fmt.Printf("%d x %d", Config.WindowSize.X, Config.WindowSize.Y) window, err := sdl.CreateWindow( "GoEscape", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, - Config.ScreenWidth, Config.ScreenHeight, + Config.WindowSize.X, Config.WindowSize.Y, sdl.WINDOW_OPENGL) if err != nil { fmt.Println("initializing window:", err) @@ -116,9 +119,10 @@ func Tick(renderer *sdl.Renderer) bool { Elements = Elements[:len(Elements)-truncate] if DebugTick { + fmt.Printf("\n\n--\n") fmt.Printf("TPS: %d\n", 1000000/(time.Since(frameStartTime).Microseconds()+1)) fmt.Printf("Elements: %d\n", len(Elements)) - + fmt.Printf("Viewport: %f %f %f %f\n", ViewPort.Position.X, ViewPort.Position.Y, ViewPort.Size.X, ViewPort.Size.Y) } Delta = time.Since(frameStartTime).Seconds() * Config.TargetTicksPerSecond