Compare commits
4 Commits
a9c6298da8
...
feature/mu
Author | SHA1 | Date | |
---|---|---|---|
be569be8a6 | |||
de75b8ef52 | |||
6c8993981c | |||
92b0b13add |
12
go.mod
12
go.mod
@ -2,10 +2,14 @@ module goonscape
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require github.com/gen2brain/raylib-go/raylib v0.0.0-20240916050633-6bc3d79c96ad
|
||||
require (
|
||||
gitea.boner.be/bdnugget/goonserver v0.0.0-20241011122434-4bd5303cfd46
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240930075631-c66f9e2942fe
|
||||
google.golang.org/protobuf v1.35.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ebitengine/purego v0.7.1 // indirect
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
github.com/ebitengine/purego v0.8.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
|
||||
golang.org/x/sys v0.26.0 // indirect
|
||||
)
|
||||
|
22
go.sum
22
go.sum
@ -1,8 +1,14 @@
|
||||
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
|
||||
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240916050633-6bc3d79c96ad h1:kmIjqc2wOOn+MXw/pyuoYhhzfRFsZ+ESfkMWVt+WE7Y=
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240916050633-6bc3d79c96ad/go.mod h1:BaY76bZk7nw1/kVOSQObPY1v1iwVE1KHAGMfvI6oK1Q=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gitea.boner.be/bdnugget/goonserver v0.0.0-20241011122434-4bd5303cfd46 h1:T2D4QcmvBqzGoHO0VJGNUd1k2lLmUcyg6Rc/vN4/Im8=
|
||||
gitea.boner.be/bdnugget/goonserver v0.0.0-20241011122434-4bd5303cfd46/go.mod h1:inR1bKrr/vcTba+G1KzmmY6vssMq9oGNOk836VwPa4c=
|
||||
github.com/ebitengine/purego v0.8.0 h1:JbqvnEzRvPpxhCJzJJ2y0RbiZ8nyjccVUrSM3q+GvvE=
|
||||
github.com/ebitengine/purego v0.8.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240930075631-c66f9e2942fe h1:mInjrbJkUglTM7tBmXG+epnPCE744aj15J7vjJwM4gs=
|
||||
github.com/gen2brain/raylib-go/raylib v0.0.0-20240930075631-c66f9e2942fe/go.mod h1:BaY76bZk7nw1/kVOSQObPY1v1iwVE1KHAGMfvI6oK1Q=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
|
||||
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
|
||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
|
205
main.go
205
main.go
@ -2,9 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
pb "gitea.boner.be/bdnugget/goonserver/actions"
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -12,6 +17,8 @@ const (
|
||||
MapHeight = 50
|
||||
TileSize = 32
|
||||
TileHeight = 2.0
|
||||
TickRate = 2600 * time.Millisecond // Server tick rate (600ms)
|
||||
serverAddr = "localhost:6969"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -26,11 +33,23 @@ type Tile struct {
|
||||
Walkable bool
|
||||
}
|
||||
|
||||
type ActionType int
|
||||
|
||||
const (
|
||||
MoveAction ActionType = iota
|
||||
)
|
||||
|
||||
type Action struct {
|
||||
Type ActionType
|
||||
X, Y int // Target position for movement
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
PosActual rl.Vector3
|
||||
PosTile Tile
|
||||
TargetPath []Tile
|
||||
Speed float32
|
||||
PosActual rl.Vector3
|
||||
PosTile Tile
|
||||
TargetPath []Tile
|
||||
Speed float32
|
||||
ActionQueue []Action // Queue for player actions
|
||||
}
|
||||
|
||||
// Initialize the map with some height data
|
||||
@ -108,35 +127,69 @@ func DrawPlayer(player Player, model *rl.Model, mapGrid [][]Tile) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func GetTileAtMouse(mapGrid [][]Tile, camera *rl.Camera3D) (Tile, bool) {
|
||||
if !rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
||||
return Tile{}, false
|
||||
}
|
||||
mouse := rl.GetMousePosition()
|
||||
|
||||
// Unproject mouse coordinates to 3D ray
|
||||
ray := rl.GetMouseRay(mouse, *camera)
|
||||
|
||||
// Calculate the distance from the camera to the ray's intersection with the ground plane (Y=0)
|
||||
if ray.Direction.Y == 0 {
|
||||
return Tile{}, false
|
||||
}
|
||||
dist := -ray.Position.Y / ray.Direction.Y
|
||||
for x := 0; x < MapWidth; x++ {
|
||||
for y := 0; y < MapHeight; y++ {
|
||||
tile := mapGrid[x][y]
|
||||
|
||||
// Calculate the intersection point
|
||||
intersection := rl.NewVector3(
|
||||
ray.Position.X+ray.Direction.X*dist,
|
||||
ray.Position.Y+ray.Direction.Y*dist,
|
||||
ray.Position.Z+ray.Direction.Z*dist,
|
||||
)
|
||||
// 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))
|
||||
|
||||
// Convert the intersection point to tile coordinates
|
||||
tileX := int(intersection.X / float32(TileSize))
|
||||
tileY := int(intersection.Z / float32(TileSize))
|
||||
|
||||
if tileX >= 0 && tileX < MapWidth && tileY >= 0 && tileY < MapHeight {
|
||||
fmt.Println("Clicked:", tileX, tileY)
|
||||
return mapGrid[tileX][tileY], true
|
||||
// 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
|
||||
}
|
||||
@ -175,6 +228,7 @@ func HandleInput(player *Player, mapGrid [][]Tile, camera *rl.Camera) {
|
||||
// Exclude the first tile (current position)
|
||||
if len(path) > 1 {
|
||||
player.TargetPath = path[1:]
|
||||
player.ActionQueue = append(player.ActionQueue, Action{Type: MoveAction, X: clickedTile.X, Y: clickedTile.Y})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -228,23 +282,11 @@ func UpdateCamera(camera *rl.Camera3D, player rl.Vector3, deltaTime float32) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(800, 600, "GoonScape")
|
||||
rl.InitWindow(1024, 768, "GoonScape")
|
||||
defer rl.CloseWindow()
|
||||
rl.InitAudioDevice()
|
||||
defer rl.CloseAudioDevice()
|
||||
|
||||
playerModel := rl.LoadModel("resources/models/goonion.obj")
|
||||
defer rl.UnloadModel(playerModel)
|
||||
playerTexture := rl.LoadTexture("resources/models/goonion.png")
|
||||
defer rl.UnloadTexture(playerTexture)
|
||||
rl.SetMaterialTexture(playerModel.Materials, rl.MapDiffuse, playerTexture)
|
||||
|
||||
coomerModel := rl.LoadModel("resources/models/coomer.obj")
|
||||
defer rl.UnloadModel(coomerModel)
|
||||
coomerTexture := rl.LoadTexture("resources/models/coomer.png")
|
||||
defer rl.UnloadTexture(coomerTexture)
|
||||
rl.SetMaterialTexture(coomerModel.Materials, rl.MapDiffuse, coomerTexture)
|
||||
|
||||
mapGrid := InitMap()
|
||||
|
||||
player := Player{
|
||||
@ -262,6 +304,27 @@ func main() {
|
||||
Projection: rl.CameraPerspective,
|
||||
}
|
||||
|
||||
conn, playerID, err := ConnectToServer()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to server: %v", err)
|
||||
}
|
||||
log.Printf("Player ID: %d", playerID)
|
||||
defer conn.Close()
|
||||
|
||||
go HandleServerCommunication(conn, playerID, &player)
|
||||
|
||||
playerModel := rl.LoadModel("resources/models/goonion.obj")
|
||||
defer rl.UnloadModel(playerModel)
|
||||
playerTexture := rl.LoadTexture("resources/models/goonion.png")
|
||||
defer rl.UnloadTexture(playerTexture)
|
||||
rl.SetMaterialTexture(playerModel.Materials, rl.MapDiffuse, playerTexture)
|
||||
|
||||
coomerModel := rl.LoadModel("resources/models/coomer.obj")
|
||||
defer rl.UnloadModel(coomerModel)
|
||||
coomerTexture := rl.LoadTexture("resources/models/coomer.png")
|
||||
defer rl.UnloadTexture(coomerTexture)
|
||||
rl.SetMaterialTexture(coomerModel.Materials, rl.MapDiffuse, coomerTexture)
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
// Music
|
||||
@ -271,6 +334,7 @@ func main() {
|
||||
defer rl.UnloadMusicStream(music)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
|
||||
rl.UpdateMusicStream(music)
|
||||
|
||||
// Time management
|
||||
@ -303,6 +367,73 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func ConnectToServer() (net.Conn, int32, error) {
|
||||
// Attempt to connect to the server
|
||||
conn, err := net.Dial("tcp", serverAddr)
|
||||
if err != nil {
|
||||
log.Printf("Failed to dial server: %v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
log.Println("Connected to server. Waiting for player ID...")
|
||||
// Buffer for incoming server message
|
||||
buf := make([]byte, 1024)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
log.Printf("Error reading player ID from server: %v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
log.Printf("Received data: %x", buf[:n])
|
||||
|
||||
// Unmarshal server message to extract the player ID
|
||||
var response pb.ServerMessage
|
||||
if err := proto.Unmarshal(buf[:n], &response); err != nil {
|
||||
log.Printf("Failed to unmarshal server response: %v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
playerID := response.GetPlayerId()
|
||||
log.Printf("Successfully connected with player ID: %d", playerID)
|
||||
return conn, playerID, nil
|
||||
}
|
||||
|
||||
func HandleServerCommunication(conn net.Conn, playerID int32, player *Player) {
|
||||
for {
|
||||
// Check if there are actions in the player's queue
|
||||
if len(player.ActionQueue) > 0 {
|
||||
// Process the first action in the queue
|
||||
actionData := player.ActionQueue[0]
|
||||
action := &pb.Action{
|
||||
PlayerId: playerID,
|
||||
Type: pb.Action_MOVE,
|
||||
X: int32(actionData.X),
|
||||
Y: int32(actionData.Y),
|
||||
}
|
||||
|
||||
// Serialize the action
|
||||
data, err := proto.Marshal(action)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal action: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Send action to server
|
||||
_, err = conn.Write(data)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send action to server: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Remove the action from the queue once it's sent
|
||||
player.ActionQueue = player.ActionQueue[1:]
|
||||
}
|
||||
|
||||
// Add a delay based on the server's tick rate
|
||||
time.Sleep(TickRate)
|
||||
}
|
||||
}
|
||||
|
||||
// pathfinding
|
||||
type Node struct {
|
||||
Tile Tile
|
||||
|
@ -1,7 +1,7 @@
|
||||
# Blender 3.6.0
|
||||
# www.blender.org
|
||||
mtllib Coomer_one_exaggerat_1003075737.mtl
|
||||
o Coomer_one_exaggerat_1003075737
|
||||
mtllib coomer.mtl
|
||||
o coomer
|
||||
v 0.319145 -0.929950 0.071755
|
||||
v 0.322944 -0.934938 0.079493
|
||||
v 0.329399 -0.938953 0.075593
|
||||
|
@ -1,7 +1,7 @@
|
||||
# Blender 3.6.0
|
||||
# www.blender.org
|
||||
mtllib Minion_full_body_A__1002112559.mtl
|
||||
o Minion_full_body_A__1002112559
|
||||
mtllib goonion.mtl
|
||||
o goonion
|
||||
v -0.441385 -0.290769 -0.044101
|
||||
v -0.448696 -0.291602 -0.052330
|
||||
v -0.444993 -0.298477 -0.046398
|
||||
|
Reference in New Issue
Block a user