mirror of
https://github.com/stevenhowes/GoRetro.git
synced 2026-05-26 15:53:31 +01:00
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:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ type component interface {
|
|||||||
type Element struct {
|
type Element struct {
|
||||||
Renderer *sdl.Renderer
|
Renderer *sdl.Renderer
|
||||||
Position Vector
|
Position Vector
|
||||||
|
SpawnPosition Vector
|
||||||
|
PositionAbsolute bool
|
||||||
Rotation float64
|
Rotation float64
|
||||||
Active bool
|
Active bool
|
||||||
Delete bool
|
Delete bool
|
||||||
|
|||||||
+10
-6
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user