-
Notifications
You must be signed in to change notification settings - Fork 0
/
monsterman.py
375 lines (235 loc) · 9.41 KB
/
monsterman.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from panda3d.core import (
Geom,
GeomNode,
GeomVertexFormat,
GeomVertexData,
GeomVertexWriter,
NodePath,
)
from panda3d.core import (
GeomTriangles,
Material,
Texture,
Vec3,
LVector3f,
LVecBase3f,
LPoint3f,
)
import math
import random
class YinYangMonster(NodePath):
def __init__(self, parent_node, size=1):
super().__init__("YinYangMonster")
self.parent_node = parent_node if isinstance(parent_node, NodePath) else render
print(f"MonsterManager initialized with parent: {self.parent_node}")
# Parameters for the Yin and Yang symbol
self.size = size
self.radius = self.size / 2
self.eye_radius = (
self.size / 8
) # Small eye radius for the two "eyes" inside Yin and Yang
# Create the YinYang shape
self.yin_yang_np = self.create_yin_yang(self.radius, self.eye_radius)
self.yin_yang_np.reparent_to(self) # Attach to this monster's node (self)
# Now, access the individual parts (Yin and Yang)
self.yin_np = self.yin_yang_np.find("**/YinGeom") # Reference to the Yin part
self.yang_np = self.yin_yang_np.find(
"**/YangGeom"
) # Reference to the Yang part
# Initial velocity and rotation speed
self.position = LPoint3f(0, 0, 0) # Initial position set to origin
self.velocity = Vec3(0, 0, 0)
self.kick_power = 10
self.rotation_speed = random.uniform(10.0, 30.0) # Speed of rotation
self.gravity = Vec3(0, 0, -9.81) # Simple gravity force (downward)
self.kick_off()
self.reparent_to(self.parent_node) # Reparent to the parent node
base.taskMgr.add(self.update, "update_monster_task")
def create_yin_yang(self, radius, eye_radius):
"""Create the Yin Yang symbol procedurally with separate collision nodes for Yin and Yang."""
format = GeomVertexFormat.get_v3n3c4()
# Create separate GeomVertexData for Yin and Yang
vertex_data_yin = GeomVertexData("yin_data", format, Geom.UHStatic)
vertex_data_yang = GeomVertexData("yang_data", format, Geom.UHStatic)
# Writers for Yin and Yang
yin_vertex_writer = GeomVertexWriter(vertex_data_yin, "vertex")
yin_normal_writer = GeomVertexWriter(vertex_data_yin, "normal")
yin_color_writer = GeomVertexWriter(vertex_data_yin, "color")
yang_vertex_writer = GeomVertexWriter(vertex_data_yang, "vertex")
yang_normal_writer = GeomVertexWriter(vertex_data_yang, "normal")
yang_color_writer = GeomVertexWriter(vertex_data_yang, "color")
# Function to add vertices
def add_vertex(writer_v, writer_n, writer_c, x, y, z, nx, ny, nz, r, g, b, a):
writer_v.add_data3f(x, y, z)
writer_n.add_data3f(nx, ny, nz)
writer_c.add_data4f(r, g, b, a)
segments = 32 # Number of segments for the circle
angle_step = 2 * math.pi / segments
# Add vertices for Yin and Yang halves
for i in range(segments):
angle = i * angle_step
x = radius * math.cos(angle)
y = radius * math.sin(angle)
if i < segments // 2: # Yin (black)
add_vertex(
yin_vertex_writer,
yin_normal_writer,
yin_color_writer,
x,
y,
0,
0,
0,
1,
0,
0,
0,
1,
)
else: # Yang (white)
add_vertex(
yang_vertex_writer,
yang_normal_writer,
yang_color_writer,
x,
y,
0,
0,
0,
1,
1,
1,
1,
1,
)
# Add eye geometry
self.add_eye(
yang_vertex_writer,
yang_normal_writer,
yang_color_writer,
radius * 0.4,
eye_radius,
1,
1,
1,
1,
)
self.add_eye(
yin_vertex_writer,
yin_normal_writer,
yin_color_writer,
radius * -0.4,
eye_radius,
0,
0,
0,
1,
)
# Create triangles for Yin and Yang
triangles_yin = GeomTriangles(Geom.UHStatic)
triangles_yang = GeomTriangles(Geom.UHStatic)
# Triangles for Yin (first half)
for i in range(segments // 2 - 1):
triangles_yin.add_vertices(i, i + 1, segments // 2 - 1) # Adjust as needed
# Triangles for Yang (second half)
for i in range(segments // 2, segments - 1):
triangles_yang.add_vertices(
i - segments // 2, i - segments // 2 + 1, segments // 2 - 1
) # Adjust as needed
# Create Geoms
geom_yin = Geom(vertex_data_yin)
geom_yin.add_primitive(triangles_yin)
geom_yang = Geom(vertex_data_yang)
geom_yang.add_primitive(triangles_yang)
# Create GeomNodes and NodePaths
geom_node_yin = GeomNode("YinGeom")
geom_node_yin.add_geom(geom_yin)
geom_node_yang = GeomNode("YangGeom")
geom_node_yang.add_geom(geom_yang)
yin_np = NodePath(geom_node_yin)
yang_np = NodePath(geom_node_yang)
# Create a parent node to combine both parts
yin_yang_np = NodePath("YinYangNode")
# Reparent the individual parts to the parent node
yin_np.reparent_to(yin_yang_np)
yang_np.reparent_to(yin_yang_np)
# Return the combined NodePath
return yin_yang_np
def add_eye(self, vertex_writer, normal_writer, color_writer, x, y, r, g, b, a):
"""Add the small circular eye to the Yin Yang symbol."""
segments = 8
angle_step = 2 * math.pi / segments
for i in range(segments):
angle = i * angle_step
eye_x = x + self.eye_radius * math.cos(angle)
eye_y = y + self.eye_radius * math.sin(angle)
vertex_writer.add_data3f(eye_x, eye_y, 0)
normal_writer.add_data3f(0, 0, 1)
color_writer.add_data4f(r, g, b, a)
def update(self, task):
# print("Updating monster")
delta_time = globalClock.get_dt()
self.update_velocity(delta_time)
self.update_position(delta_time)
self.update_hpr()
return task.cont # Continue the task
def kick_off(self):
"""Kick the monster in a random direction."""
direction = Vec3(random.uniform(-1, 1), random.uniform(-1, 1), 0).normalized()
self.velocity = (
direction * self.kick_power
) # Apply the kick power to the velocity
# print(f"Initial velocity after kick-off: {self.velocity}")
def update_velocity(self, delta_time):
self.velocity += LVector3f(0, 0, -9.8) * delta_time # Apply gravity
# print(f"Velocity: {self.velocity}")
def update_position(self, delta_time):
self.position += self.velocity * delta_time
self.set_pos(self.position) # Update the NodePath's position in the scene graph
# print(f"Position: {self.position}")
def update_hpr(self):
if self.velocity.length() > 0:
direction = self.velocity.normalized()
self.hpr = LVecBase3f(
direction.getX(), direction.getY(), 0
) # Update heading
# print(f"Updated hpr: {self.hpr}")
class MonsterManager:
def create_yin_yang(self):
return YinYangMonster(parent_node=render)
def __init__(self, parent=None):
self.parent_node = parent
self.monsters = []
def create_monster(self, index):
"""Create a monster object and assign properties (health, type, etc.)."""
# Pass render explicitly if self.parent is None
monster = YinYangMonster(self.parent_node, size=random.uniform(1.0, 2.0))
monster.name = f"Monster_{index}"
monster.health = 100
monster.type = "Flower"
monster.set_scale(1)
print(
f"Created {monster.name} with health {monster.health} and type {monster.type}."
)
return monster
def place_monsters(self, monsters=[], num_monsters=10):
"""Place monsters randomly in the world."""
for i in range(num_monsters):
monster = self.create_monster(i)
if self.parent_node is not None:
monster.reparent_to(
self.parent_node
) # Attach the monster to the parent node
else:
raise ValueError(
"Parent NodePath is None. Cannot reparent the monster."
)
monsters.append(monster)
return monsters
def update_monsters(self, dt):
"""Update all monsters (e.g., move them, apply velocities)."""
for monster in self.monsters:
monster.update(dt) # Update position based on velocity
# Every few seconds, apply a "kick" to a random monster
if random.random() < 0.1: # Chance to kick off
monster.kick_off()