GOingTunneling/gameobject.go

121 lines
3.3 KiB
Go
Raw Normal View History

2024-08-29 18:48:27 +02:00
package main
import (
"github.com/veandco/go-sdl2/sdl"
"image/color"
)
type GameObject struct {
2024-08-31 15:35:19 +02:00
baseRect *sdl.Rect
prevBaseRect *sdl.Rect
borderRect *sdl.Rect
collisionRect *sdl.Rect
orientation uint8
visualRects [][]*ColoredRect
colors []color.Color
inView bool
2024-08-29 18:48:27 +02:00
}
type ColoredRect struct {
color color.Color
rect *sdl.Rect
}
func adjustRectToCamera(object *sdl.Rect, camera *sdl.Rect) *sdl.Rect {
return &sdl.Rect{
X: object.X - camera.X,
Y: object.Y - camera.Y,
W: object.W,
H: object.H,
}
}
2024-08-30 19:07:56 +02:00
func (gameObject *GameObject) adjustRectWorld(offset *sdl.Rect) *sdl.Rect {
return &sdl.Rect{
X: gameObject.baseRect.X + offset.X,
Y: gameObject.baseRect.Y + offset.Y,
W: offset.W,
H: offset.H,
}
}
2024-08-31 15:35:19 +02:00
func (gameObject *GameObject) adjustColoredRectWorld(offset *ColoredRect) *ColoredRect {
return &ColoredRect{
color: offset.color,
rect: gameObject.adjustRectWorld(offset.rect),
}
}
2024-08-29 18:48:27 +02:00
func (gameObject *GameObject) adjustRectToCamera(offset *sdl.Rect, camera *sdl.Rect) *sdl.Rect {
return &sdl.Rect{
X: gameObject.baseRect.X + offset.X - camera.X,
Y: gameObject.baseRect.Y + offset.Y - camera.Y,
W: offset.W,
H: offset.H,
}
}
2024-08-31 15:35:19 +02:00
func (gameObject *GameObject) adjustBaseRect() {
first := true
oldX, oldY := gameObject.baseRect.X, gameObject.baseRect.Y
for _, rect := range gameObject.getCurrentRects() {
if first {
gameObject.baseRect = gameObject.adjustRectWorld(rect.rect)
first = false
} else {
}
newRect := gameObject.baseRect.Union(gameObject.adjustRectWorld(rect.rect))
gameObject.baseRect = &newRect
gameObject.baseRect.X, gameObject.baseRect.Y = oldX, oldY
}
}
2024-08-29 18:48:27 +02:00
func (gameObject *GameObject) render(camera *sdl.Rect, surface *sdl.Surface) {
2024-08-31 15:35:19 +02:00
if camera.HasIntersection(gameObject.baseRect) {
2024-08-30 19:07:56 +02:00
gameObject.inView = true
if config.Debug {
2024-08-31 15:35:19 +02:00
baseRectFinal := adjustRectToCamera(gameObject.baseRect, camera)
2024-08-29 18:48:27 +02:00
surface.FillRect(baseRectFinal, sdl.MapRGBA(surface.Format, 255, 20, 10, 64))
2024-08-31 15:35:19 +02:00
if !gameObject.collisionRect.Empty() {
surface.FillRect(adjustRectToCamera(gameObject.collisionRect, camera), sdl.MapRGBA(surface.Format, 40, 192, 255, 64))
}
2024-08-29 18:48:27 +02:00
}
2024-08-30 19:07:56 +02:00
if config.RenderGameObjects {
2024-08-29 18:48:27 +02:00
for _, coloredRect := range gameObject.visualRects[gameObject.orientation] {
finalRect := gameObject.adjustRectToCamera(coloredRect.rect, camera)
r, g, b, a := coloredRect.color.RGBA()
surface.FillRect(finalRect, sdl.MapRGBA(surface.Format, uint8(r), uint8(g), uint8(b), uint8(a)))
}
}
2024-08-30 19:07:56 +02:00
} else {
gameObject.inView = false
2024-08-29 18:48:27 +02:00
}
}
func (gameObject *GameObject) addColoredRect(x, y, w, h int32, color uint8) {
if gameObject.visualRects == nil {
gameObject.visualRects = make([][]*ColoredRect, 0)
}
if uint8(len(gameObject.visualRects)) <= gameObject.orientation {
gameObject.visualRects = append(gameObject.visualRects, make([]*ColoredRect, 0))
}
if uint8(len(gameObject.colors)) > color {
gameObject.visualRects[gameObject.orientation] = append(gameObject.visualRects[gameObject.orientation],
&ColoredRect{
color: gameObject.colors[color],
rect: &sdl.Rect{
X: x, Y: y, W: w, H: h,
},
})
}
}
func (gameObject *GameObject) getCurrentRects() []*ColoredRect {
return gameObject.visualRects[gameObject.orientation]
}
func (gameObject *GameObject) addColor(color color.Color) {
gameObject.colors = append(gameObject.colors, color)
}