last_light/engine/engine_util.go

213 lines
3.2 KiB
Go
Raw Normal View History

2024-05-06 21:19:08 +03:00
package engine
2024-05-12 23:22:39 +03:00
import (
"math"
"math/rand"
)
2024-05-03 13:46:32 +03:00
2024-05-06 18:59:14 +03:00
type Positioned struct {
pos Position
}
func WithPosition(pos Position) Positioned {
return Positioned{
pos: pos,
}
}
func (wp *Positioned) Position() Position {
return wp.pos
}
2024-05-12 23:22:39 +03:00
func (wp *Positioned) SetPosition(pos Position) {
wp.pos = pos
}
type Position struct {
x int
y int
}
func PositionAt(x int, y int) Position {
return Position{int(x), int(y)}
}
func (p Position) X() int {
return p.x
}
func (p Position) Y() int {
return p.y
}
2024-04-24 17:11:33 +03:00
func (p Position) XY() (int, int) {
return p.x, p.y
}
2024-05-13 01:01:39 +03:00
func (p Position) DistanceSquared(pos Position) int {
return (pos.x-p.x)*(pos.x-p.x) + (pos.y-p.y)*(pos.y-p.y)
2024-05-12 23:22:39 +03:00
}
2024-05-13 01:01:39 +03:00
func (p Position) Distance(pos Position) int {
return int(math.Floor(math.Sqrt(float64(p.DistanceSquared(pos)))))
2024-05-12 23:22:39 +03:00
}
func (p Position) Equals(other Position) bool {
return p.x == other.x && p.y == other.y
}
func (p Position) WithOffset(xOffset int, yOffset int) Position {
p.x = p.x + xOffset
p.y = p.y + yOffset
return p
2024-04-24 17:11:33 +03:00
}
2024-06-11 11:04:22 +03:00
func (p Position) Diff(other Position) Position {
p.x = p.x - other.x
p.y = p.y - other.y
return p
}
func (p Position) Sign() Position {
p.x = IntSign(p.x)
p.y = IntSign(p.y)
return p
}
func IntSign(i int) int {
if i < 0 {
return -1
}
if i > 0 {
return 1
}
return 0
}
2024-05-06 18:59:14 +03:00
type Sized struct {
size Size
}
func WithSize(size Size) Sized {
return Sized{
size: size,
}
}
// Checks if the provided coordinates fit within the sized struct, [0, N)
func (ws *Sized) FitsWithin(x, y int) bool {
return 0 <= x && x < ws.size.width && 0 <= y && y < ws.size.height
}
func (ws *Sized) Size() Size {
return ws.size
}
type Size struct {
width int
height int
}
func SizeOf(width int, height int) Size {
return Size{int(width), int(height)}
}
func (s Size) Width() int {
return s.width
}
func (s Size) Height() int {
return s.height
}
2024-04-24 17:11:33 +03:00
func (s Size) WH() (int, int) {
return s.width, s.height
2024-04-24 17:11:33 +03:00
}
2024-05-03 13:46:32 +03:00
func (s Size) Area() int {
return s.width * s.height
}
2024-05-06 18:59:14 +03:00
func (s Size) AsArrayIndex(x, y int) int {
return y*s.width + x
}
2024-05-12 23:22:39 +03:00
func (s Size) Contains(x, y int) bool {
return 0 <= x && x < s.width && 0 <= y && y < s.height
}
2024-06-06 23:17:22 +03:00
func LimitAdd(original, amount, limit int) int {
if original+amount > limit {
return limit
}
return original + amount
}
2024-04-24 17:11:33 +03:00
func LimitIncrement(i int, limit int) int {
if (i + 1) > limit {
return i
}
return i + 1
}
2024-06-06 23:17:22 +03:00
func LimitSubtract(original, amount, limit int) int {
if original-amount < limit {
return limit
}
return original - amount
}
2024-04-24 17:11:33 +03:00
func LimitDecrement(i int, limit int) int {
if (i - 1) < limit {
return i
}
return i - 1
}
2024-05-03 13:46:32 +03:00
func RandInt(min, max int) int {
2024-05-31 23:37:06 +03:00
if min == max {
return min
}
2024-05-03 13:46:32 +03:00
return min + rand.Intn(max-min)
}
2024-05-12 23:22:39 +03:00
func MapSlice[S ~[]E, E any, R any](slice S, mappingFunc func(e E) R) []R {
newSlice := make([]R, 0, len(slice))
for _, el := range slice {
newSlice = append(newSlice, mappingFunc(el))
}
return newSlice
}
2024-05-30 23:39:54 +03:00
func AbsInt(val int) int {
switch {
case val < 0:
return -val
case val == 0:
return 0
}
return val
}
2024-06-10 23:20:38 +03:00
func Lerp(start, end float64, t float64) float64 {
return start*(1.0-t) + end*t
}
func LerpPositions(p0, p1 Position, t float64) Position {
return PositionAt(
int(math.Round(Lerp(float64(p0.x), float64(p1.x), t))),
int(math.Round(Lerp(float64(p0.y), float64(p1.y), t))),
)
}