Repository navigation
Expand file tree
/
Copy pathsystem_player.go
More file actions
139 lines (110 loc) · 3.46 KB
/
Copy pathsystem_player.go
File metadata and controls
139 lines (110 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"log/slog"
"github.com/g3n/engine/experimental/collision"
"github.com/g3n/engine/graphic"
"github.com/g3n/engine/window"
)
// TODO: cleanup deps
type Raycast struct {
rc *collision.Raycaster
}
// TODO: cleanup deps
func registerRaycast(g3nApp *G3NApp, wallMesh *graphic.Mesh) {
a := g3nApp
a.rc.rc.LinePrecision = 0.05
a.rc.rc.PointPrecision = 0.05
// Subscribe to mouse button down events
a.app.SubscribeID(window.OnMouseDown, a, func(evname string, ev interface{}) {
a.rc.onMouse(a, wallMesh, ev)
})
}
// TODO: cleanup deps
func (rc *Raycast) onMouse(a *G3NApp, wallMesh *graphic.Mesh, ev interface{}) {
// Convert mouse coordinates to normalized device coordinates
mev := ev.(*window.MouseEvent)
width, height := a.GetSize()
x := 2*(mev.Xpos/float32(width)) - 1
y := -2*(mev.Ypos/float32(height)) + 1
// Set the raycaster from the current camera and mouse coordinates
rc.rc.SetFromCamera(a.cam, x, y)
//fmt.Printf("rc:%+v\n", t.rc.Ray)
// Checks intersection with all objects in the scene
intersects := rc.rc.IntersectObjects(a.scene.Children(), true)
//fmt.Printf("intersects:%+v\n", intersects)
if len(intersects) == 0 {
return
}
// Get first intersection
obj := intersects[0].Object
// Convert INode to IGraphic
ig, ok := obj.(graphic.IGraphic)
if !ok {
slog.Debug("Not graphic", "obj", fmt.Sprintf("%T", obj))
return
}
// Get graphic object
gr := ig.GetGraphic()
// imat := gr.GetMaterial(0)
// type matI interface {
// EmissiveColor() math32.Color
// SetEmissiveColor(*math32.Color)
// }
// if v, ok := imat.(matI); ok {
// if em := v.EmissiveColor(); em.R == 1 && em.G == 1 && em.B == 1 {
// v.SetEmissiveColor(&math32.Color{0, 0, 0})
// } else {
// v.SetEmissiveColor(&math32.Color{1, 1, 1})
// }
// }
pos := gr.Position()
mesh := CloneAndPosition(wallMesh, int(pos.X), int(pos.Z))
mesh.SetVisible(true)
i := a.game.Map.CurrentLevel.GetIndexFromXZ(int(pos.X), int(pos.Z))
a.game.Map.CurrentLevel.Tiles[i].Blocked = true
a.scene.Remove(a.game.Map.CurrentLevel.Tiles[i].Mesh)
a.game.Map.CurrentLevel.Tiles[i].Mesh = mesh
a.game.Map.CurrentLevel.Tiles[i].Mesh.SetChanged(true)
a.scene.Add(mesh)
//slog.Info("click", "pos", pos, "mesh", mesh, "mesh.pos", mesh.Position())
}
// TODO: make it a game method, not a G3NApp)
func (g3nApp *G3NApp) TakePlayerAction() {
game := g3nApp.game
players := game.WorldTags["players"]
turnTaken := false
game.muCurrentOffset.RLock()
x := game.currentOffsetX
z := game.currentOffsetZ
game.muCurrentOffset.RUnlock()
level := game.Map.CurrentLevel
for _, result := range game.World.Query(players) {
pos := result.Components[positions].(*Position)
index := level.GetIndexFromXZ(pos.X+x, pos.Z+z)
tile := level.Tiles[index]
if !tile.Blocked {
level.Tiles[level.GetIndexFromXZ(pos.X, pos.Z)].Blocked = false
pos.X += x
pos.Z += z
level.Tiles[index].Blocked = true
level.mu.Lock()
level.PlayerVisible.Compute(level, pos.X, pos.Z, 8)
level.mu.Unlock()
} else if x != 0 || z != 0 {
if level.Tiles[index].TileType != TileTypeWall {
//Its a tile with a monster -- Fight it
monsterPosition := Position{X: pos.X + x, Z: pos.Z + z}
CombatSystem(game, pos, &monsterPosition)
}
}
g3nApp.updateMapVisibility(level)
}
if x != 0 || z != 0 || turnTaken {
game.muCurrentOffset.Lock()
game.currentOffsetX, game.currentOffsetZ = 0, 0
game.muCurrentOffset.Unlock()
game.Turn = GetNextState(game.Turn)
game.TurnCounter = 0
}
}