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.

This commit is contained in:
stevenhowes
2022-03-27 17:02:32 +01:00
parent bfa9ab0037
commit 428e2d175c
7 changed files with 88 additions and 24 deletions
+5 -4
View File
@@ -9,7 +9,6 @@ package GoRetro
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"time" "time"
@@ -65,12 +64,16 @@ func (an *animator) onUpdate() error {
} }
func (an *animator) onDraw() error { func (an *animator) onDraw() error {
Position := an.container.Position
if !an.container.PositionAbsolute {
Position = vectorAdd(an.container.Position, ViewPort.Position)
}
return drawTexture( return drawTexture(
an.tex, 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].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}, 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.Rotation,
an.container.Renderer) an.container.Renderer)
} }
@@ -131,8 +134,6 @@ func NewSequence(
} }
defer jsonFile.Close() defer jsonFile.Close()
fmt.Println(seq.frames)
seq.frame = 0 seq.frame = 0
seq.sampleRate = sampleRate seq.sampleRate = sampleRate
seq.loop = loop seq.loop = loop
+40
View File
@@ -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
}
+9 -2
View File
@@ -1,10 +1,13 @@
package GoRetro package GoRetro
import "fmt"
/* /*
* -------------------- * --------------------
* BounderScreen * BounderScreen
* -------------------- * --------------------
* A bounder which triggers if the element isn't on the screen * A bounder which triggers if the element isn't on the screen
* TODO: Use dimensions of the entity
*/ */
type bounderScreen struct { type bounderScreen struct {
@@ -13,6 +16,10 @@ type bounderScreen struct {
} }
func NewBounderScreen(container *Element, callback func(element *Element)) *bounderScreen { func NewBounderScreen(container *Element, callback func(element *Element)) *bounderScreen {
if !container.PositionAbsolute {
fmt.Println("Added screen bounder to non-absolute positioned element")
}
return &bounderScreen{ return &bounderScreen{
container: container, container: container,
callbackFunc: callback, callbackFunc: callback,
@@ -26,8 +33,8 @@ func (bounder *bounderScreen) onDraw() error {
func (bounder *bounderScreen) onUpdate() error { func (bounder *bounderScreen) onUpdate() error {
b := bounder.container b := bounder.container
if b.Position.X > float64(Config.ScreenWidth) || b.Position.X < 0 || if b.Position.X > float64(Config.WindowSize.X) || b.Position.X < 0 ||
b.Position.Y > float64(Config.ScreenHeight) || b.Position.Y < 0 { b.Position.Y > float64(Config.WindowSize.Y) || b.Position.Y < 0 {
bounder.callbackFunc(bounder.container) bounder.callbackFunc(bounder.container)
} }
+6 -1
View File
@@ -46,11 +46,16 @@ func (sr *spriteRenderer) onUpdate() error {
} }
func (sr *spriteRenderer) onDraw() error { func (sr *spriteRenderer) onDraw() error {
Position := sr.container.Position
if !sr.container.PositionAbsolute {
Position = vectorAdd(sr.container.Position, ViewPort.Position)
}
return drawTexture( return drawTexture(
sr.tex, sr.tex,
VectorInt32{-1, -1}, VectorInt32{-1, -1},
VectorInt32{0, 0}, VectorInt32{0, 0},
sr.container.Position, Position,
sr.container.Rotation, sr.container.Rotation,
sr.container.Renderer) sr.container.Renderer)
} }
+6 -1
View File
@@ -42,11 +42,16 @@ func (sr *spriteSheetRenderer) onUpdate() error {
} }
func (sr *spriteSheetRenderer) onDraw() error { func (sr *spriteSheetRenderer) onDraw() error {
Position := ViewPort.Position
if !sr.container.PositionAbsolute {
Position = vectorAdd(sr.container.Position, ViewPort.Position)
}
return drawTexture( return drawTexture(
sr.tex, sr.tex,
sr.size, sr.size,
sr.sheetposition, sr.sheetposition,
sr.container.Position, Position,
sr.container.Rotation, sr.container.Rotation,
sr.container.Renderer) sr.container.Renderer)
} }
+12 -10
View File
@@ -14,16 +14,18 @@ type component interface {
} }
type Element struct { type Element struct {
Renderer *sdl.Renderer Renderer *sdl.Renderer
Position Vector Position Vector
Rotation float64 SpawnPosition Vector
Active bool PositionAbsolute bool
Delete bool Rotation float64
Collisions []Circle Active bool
components []component Delete bool
parentElement *Element Collisions []Circle
ZIndex int components []component
CollisionLayer int parentElement *Element
ZIndex int
CollisionLayer int
} }
var Elements []*Element var Elements []*Element
+10 -6
View File
@@ -7,10 +7,13 @@ import (
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
) )
var Config struct { var ViewPort struct {
ScreenWidth int32 Size Vector
ScreenHeight int32 Position Vector
}
var Config struct {
WindowSize VectorInt32
TargetTicksPerSecond float64 TargetTicksPerSecond float64
DebugStatePrintSeconds float64 DebugStatePrintSeconds float64
@@ -27,12 +30,12 @@ func Init() (*sdl.Renderer, *sdl.Window) {
return nil, nil 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( window, err := sdl.CreateWindow(
"GoEscape", "GoEscape",
sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
Config.ScreenWidth, Config.ScreenHeight, Config.WindowSize.X, Config.WindowSize.Y,
sdl.WINDOW_OPENGL) sdl.WINDOW_OPENGL)
if err != nil { if err != nil {
fmt.Println("initializing window:", err) fmt.Println("initializing window:", err)
@@ -116,9 +119,10 @@ func Tick(renderer *sdl.Renderer) bool {
Elements = Elements[:len(Elements)-truncate] Elements = Elements[:len(Elements)-truncate]
if DebugTick { if DebugTick {
fmt.Printf("\n\n--\n")
fmt.Printf("TPS: %d\n", 1000000/(time.Since(frameStartTime).Microseconds()+1)) fmt.Printf("TPS: %d\n", 1000000/(time.Since(frameStartTime).Microseconds()+1))
fmt.Printf("Elements: %d\n", len(Elements)) 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 Delta = time.Since(frameStartTime).Seconds() * Config.TargetTicksPerSecond