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 (
"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
+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
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)
}
+6 -1
View File
@@ -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)
}
+6 -1
View File
@@ -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)
}
+12 -10
View File
@@ -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
+10 -6
View File
@@ -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