goonscape/player.go

64 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-10-31 01:06:39 +01:00
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func DrawPlayer(player Player, model rl.Model) {
// Draw the player based on its actual position (PosActual) and current tile height
playerPos := rl.Vector3{
X: player.PosActual.X,
Y: mapGrid[player.PosTile.X][player.PosTile.Y].Height*TileHeight + 16.0,
Z: player.PosActual.Z,
}
rl.DrawModel(model, playerPos, 16, rl.White)
// Draw highlight around target tile
if len(player.TargetPath) > 0 {
targetTile := player.TargetPath[len(player.TargetPath)-1] // last tile in the slice
targetPos := rl.Vector3{
X: float32(targetTile.X * TileSize),
Y: mapGrid[targetTile.X][targetTile.Y].Height * TileHeight,
Z: float32(targetTile.Y * TileSize),
}
rl.DrawCubeWires(targetPos, TileSize, TileHeight, TileSize, rl.Green)
nextTile := player.TargetPath[0] // first tile in the slice
nextPos := rl.Vector3{
X: float32(nextTile.X * TileSize),
Y: mapGrid[nextTile.X][nextTile.Y].Height * TileHeight,
Z: float32(nextTile.Y * TileSize),
}
rl.DrawCubeWires(nextPos, TileSize, TileHeight, TileSize, rl.Yellow)
}
}
func (player *Player) MoveTowards(target Tile, deltaTime float32) {
// Calculate the direction vector to the target tile
targetPos := rl.Vector3{
X: float32(target.X * TileSize),
Y: mapGrid[target.X][target.Y].Height * TileHeight,
Z: float32(target.Y * TileSize),
}
// Calculate direction and normalize it for smooth movement
direction := rl.Vector3Subtract(targetPos, player.PosActual)
distance := rl.Vector3Length(direction)
if distance > 0 {
direction = rl.Vector3Scale(direction, player.Speed*deltaTime/distance)
}
// Move the player towards the target tile
if distance > 1.0 {
player.PosActual = rl.Vector3Add(player.PosActual, direction)
} else {
// Snap to the target tile when close enough
player.PosActual = targetPos
player.PosTile = target // Update player's tile
if len(player.TargetPath) > 1 {
player.TargetPath = player.TargetPath[1:] // Move to next tile in path if any
}
}
}