95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
pb "gitea.boner.be/bdnugget/goonserver/actions"
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
func GetTileAtMouse(camera *rl.Camera3D) (Tile, bool) {
|
|
if !rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
|
return Tile{}, false
|
|
}
|
|
mouse := rl.GetMousePosition()
|
|
ray := rl.GetMouseRay(mouse, *camera)
|
|
|
|
for x := 0; x < MapWidth; x++ {
|
|
for y := 0; y < MapHeight; y++ {
|
|
tile := mapGrid[x][y]
|
|
|
|
// Define the bounding box for each tile based on its position and height
|
|
tilePos := rl.NewVector3(float32(x*TileSize), tile.Height*TileHeight, float32(y*TileSize))
|
|
boxMin := rl.Vector3Subtract(tilePos, rl.NewVector3(TileSize/2, TileHeight/2, TileSize/2))
|
|
boxMax := rl.Vector3Add(tilePos, rl.NewVector3(TileSize/2, TileHeight/2, TileSize/2))
|
|
|
|
// Check if the ray intersects the bounding box
|
|
if RayIntersectsBox(ray, boxMin, boxMax) {
|
|
fmt.Println("Clicked:", tile.X, tile.Y)
|
|
return tile, true
|
|
}
|
|
}
|
|
}
|
|
return Tile{}, false
|
|
}
|
|
|
|
func HandleInput(player *Player, camera *rl.Camera) {
|
|
clickedTile, clicked := GetTileAtMouse(camera)
|
|
if clicked {
|
|
path := FindPath(mapGrid[player.PosTile.X][player.PosTile.Y], clickedTile)
|
|
if path != nil {
|
|
// Exclude the first tile (current position)
|
|
if len(path) > 1 {
|
|
player.TargetPath = path[1:]
|
|
player.ActionQueue = append(player.ActionQueue, &pb.Action{
|
|
Type: pb.Action_MOVE,
|
|
X: int32(clickedTile.X),
|
|
Y: int32(clickedTile.Y),
|
|
PlayerId: player.ID,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Helper function to test ray-box intersection (slab method)
|
|
func RayIntersectsBox(ray rl.Ray, boxMin, boxMax rl.Vector3) bool {
|
|
tmin := (boxMin.X - ray.Position.X) / ray.Direction.X
|
|
tmax := (boxMax.X - ray.Position.X) / ray.Direction.X
|
|
|
|
if tmin > tmax {
|
|
tmin, tmax = tmax, tmin
|
|
}
|
|
|
|
tymin := (boxMin.Z - ray.Position.Z) / ray.Direction.Z
|
|
tymax := (boxMax.Z - ray.Position.Z) / ray.Direction.Z
|
|
|
|
if tymin > tymax {
|
|
tymin, tymax = tymax, tymin
|
|
}
|
|
|
|
if (tmin > tymax) || (tymin > tmax) {
|
|
return false
|
|
}
|
|
|
|
if tymin > tmin {
|
|
tmin = tymin
|
|
}
|
|
if tymax < tmax {
|
|
tmax = tymax
|
|
}
|
|
|
|
tzmin := (boxMin.Y - ray.Position.Y) / ray.Direction.Y
|
|
tzmax := (boxMax.Y - ray.Position.Y) / ray.Direction.Y
|
|
|
|
if tzmin > tzmax {
|
|
tzmin, tzmax = tzmax, tzmin
|
|
}
|
|
|
|
if (tmin > tzmax) || (tzmin > tmax) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|