forked from Tnze/go-mc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
193 lines (161 loc) · 4.15 KB
/
entity.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package core
import (
"github.com/Edouard127/go-mc/data/entity"
"github.com/Edouard127/go-mc/maths"
"github.com/google/uuid"
)
type UnaliveEntity struct {
Type entity.TypeEntity
ID int32
UUID uuid.UUID
Position maths.Vec3d
LastPosition maths.Vec3d
Rotation maths.Vec2d
LastRotation maths.Vec2d
Motion maths.Vec3d
BoundingBox maths.AxisAlignedBB
Width, Height float64
Vehicle Entity
Passengers []Entity
dataManager map[int32]interface{}
}
type Entity interface {
GetName() string
GetDisplayName() string
GetType() entity.TypeEntity
GetID() int32
GetUUID() uuid.UUID
GetPosition() maths.Vec3d
GetEyePosition() maths.Vec3d
GetRotation() maths.Vec2d
GetMotion() maths.Vec3d
GetBoundingBox() maths.AxisAlignedBB
GetWidth() float64
GetHeight() float64
GetDataManager() map[int32]interface{}
IsLivingEntity() bool
IsPlayer() bool
SetPosition(x, y, z float64)
SetRotation(yaw, pitch float64)
SetMotion(x, y, z float64)
SetSize(width, height float64)
Equals(other entity.TypeEntity) bool
IsPassenger() bool
IsVehicle() bool
GetPassengers() []Entity
}
func (e *UnaliveEntity) GetName() string {
return e.Type.Name
}
func (e *UnaliveEntity) GetDisplayName() string {
return e.Type.DisplayName
}
func (e *UnaliveEntity) GetType() entity.TypeEntity {
return e.Type
}
func (e *UnaliveEntity) GetID() int32 {
return e.ID
}
func (e *UnaliveEntity) GetUUID() uuid.UUID {
return e.UUID
}
func (e *UnaliveEntity) GetPosition() maths.Vec3d {
return e.Position
}
func (e *UnaliveEntity) GetEyePosition() maths.Vec3d {
c := e.Position
c.Y += e.Height * 0.85
return c
}
func (e *UnaliveEntity) GetRotation() maths.Vec2d {
return e.Rotation
}
func (e *UnaliveEntity) GetMotion() maths.Vec3d {
return e.Motion
}
func (e *UnaliveEntity) GetBoundingBox() maths.AxisAlignedBB {
return e.BoundingBox
}
func (e *UnaliveEntity) GetWidth() float64 {
return e.Width
}
func (e *UnaliveEntity) GetHeight() float64 {
return e.Height
}
func (e *UnaliveEntity) GetDataManager() map[int32]interface{} {
return e.dataManager
}
func (e *UnaliveEntity) IsLivingEntity() bool {
return false
}
func (e *UnaliveEntity) IsPlayer() bool {
return false
}
func (e *UnaliveEntity) SetPosition(x, y, z float64) {
e.Position.Set(maths.Vec3d{X: x, Y: y, Z: z})
e.BoundingBox.Move(x-e.LastPosition.X, y-e.LastPosition.Y, z-e.LastPosition.Z)
}
func (e *UnaliveEntity) SetRelativePosition(x, y, z float64) {
e.SetPosition(e.Position.X+x, e.Position.Y+y, e.Position.Z+z)
}
func (e *UnaliveEntity) SetRotation(yaw, pitch float64) {
e.LastRotation.Set(e.Rotation)
e.Rotation.Set(maths.Vec2d{X: yaw, Y: pitch})
}
func (e *UnaliveEntity) SetRelativeRotation(yaw, pitch float64) {
e.SetRotation(e.Rotation.X+yaw, e.Rotation.Y+pitch)
}
func (e *UnaliveEntity) SetMotion(x, y, z float64) {
e.Motion.Set(maths.Vec3d{X: x, Y: y, Z: z})
}
func (e *UnaliveEntity) SetSize(width, height float64) {
if width != e.Width || height != e.Height {
f := e.Width
e.Width = width
e.Height = height
if e.Width < f {
d0 := width / 2.0
e.BoundingBox = maths.AxisAlignedBB{
MinX: e.Position.X - d0,
MinY: e.Position.Y,
MinZ: e.Position.Z - d0,
MaxX: e.Position.X + d0,
MaxY: e.Position.Y + height,
MaxZ: e.Position.Z + d0,
}
}
aabb := e.BoundingBox
e.BoundingBox = maths.AxisAlignedBB{
MinX: aabb.MinX,
MinY: aabb.MinY,
MinZ: aabb.MinZ,
MaxX: aabb.MaxX + e.Width,
MaxY: aabb.MaxY + e.Height,
MaxZ: aabb.MaxZ + e.Width,
}
}
}
func (e *UnaliveEntity) Equals(other entity.TypeEntity) bool {
return e.Type == other
}
func (e *UnaliveEntity) IsPassenger() bool {
return e.Vehicle != nil
}
func (e *UnaliveEntity) IsVehicle() bool {
return len(e.Passengers) > 0
}
func (e *UnaliveEntity) GetPassengers() []Entity {
return e.Passengers
}
func NewEntity(id int32, uuid uuid.UUID, t int32, x, y, z float64, yaw, pitch float64) *UnaliveEntity {
entityType := entity.TypeEntityByID[t]
e := &UnaliveEntity{
Type: entityType,
ID: id,
UUID: uuid,
Position: maths.Vec3d{X: x, Y: y, Z: z},
Rotation: maths.Vec2d{X: yaw, Y: pitch},
}
e.SetSize(entityType.Width, entityType.Height)
return e
}