-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballs.py
181 lines (135 loc) · 5.37 KB
/
balls.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
import math
import random
import numpy as np
from direct.interval.IntervalGlobal import Sequence, Parallel, Func
from panda3d.core import NodePath
from panda3d.core import Vec3, BitMask32, Point3
from panda3d.bullet import BulletRigidBodyNode, BulletSphereShape
from bubble import Bubbles
from tower import Colors
from create_geomnode import SphereGeom
PATH_TEXTURE_MULTI = 'textures/multi.jpg'
PATH_TEXTURE_TWOTONE = 'textures/two_tone.jpg'
class ColorBall:
def __init__(self, world):
self.bubbles = Bubbles()
self.ball = None
self.normal_ball = NormalBall()
world.attach(self.normal_ball.node())
self.multi_ball = MultiColorBall()
world.attach(self.multi_ball.node())
self.twotone_ball = TwoToneBall()
world.attach(self.twotone_ball.node())
def initialize(self, tower):
if self.ball is not None and self.ball.has_parent():
self.detach_ball()
self.tower = tower
self.twotone_used = False
def setup(self, pos, parent):
b = 7 if not self.twotone_used else 6
match n := random.randint(0, b):
case 6:
self.ball = self.multi_ball
case 7:
self.twotone_used = True
self.ball = self.twotone_ball
case _:
self.ball = self.normal_ball
color = Colors.get_rgba(n)
self.ball.set_color(color)
self.ball.set_pos(pos)
self.ball.set_hpr(Vec3(95, 0, 30))
self.ball.reparent_to(parent)
def detach_ball(self):
self.ball.detach_node()
def aim_at(self, clicked_pt, block):
self.target_pt = clicked_pt
self.target_block = block
start_pt = self.ball.get_pos()
end_pt = self.ball.get_parent().get_relative_point(base.render, clicked_pt)
mid = (start_pt + end_pt) / 2
mid.z += 10
self.control_pts = [start_pt, mid, end_pt]
self.total_dt = 0
def bernstein(self, n, k, t):
coef = math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
return coef * t ** k * (1 - t) ** (n - k)
def bezier_curve(self, dt):
n = len(self.control_pts) - 1
px = py = pz = 0
for i in range(len(self.control_pts)):
b = self.bernstein(n, i, self.total_dt)
px += np.dot(b, self.control_pts[i][0])
py += np.dot(b, self.control_pts[i][1])
pz += np.dot(b, self.control_pts[i][2])
return Point3(px, py, pz)
def move(self, dt):
self.total_dt += dt
if self.total_dt > 1:
self.total_dt = 1
pt = self.bezier_curve(dt)
self.ball.set_pos(pt)
self.ball.set_p(self.ball.get_p() + 360 * dt)
if self.total_dt == 1:
return False
return True
def hit(self):
self.detach_ball()
self.ball.hit(self.target_pt, self.target_block, self.bubbles, self.tower)
class Balls(NodePath):
def __init__(self, name):
super().__init__(BulletRigidBodyNode(name))
self.model = SphereGeom(radius=2.0)
self.model.reparent_to(self)
end, tip = self.model.get_tight_bounds()
size = tip - end
self.node().add_shape(BulletSphereShape(size.z / 2))
self.set_collide_mask(BitMask32.bit(3))
self.set_scale(0.2)
self.node().set_kinematic(True)
class NormalBall(Balls):
def __init__(self):
super().__init__('normal_ball')
def hit(self, clicked_pos, block, bubbles, tower):
blocks = []
if self.getColor() == block.get_color():
tower.get_neighbors(block, block.get_color(), blocks)
para = Parallel(bubbles.get_sequence(self.get_color(), clicked_pos))
for block in blocks:
pos = block.get_pos(base.render)
para.append(Sequence(
Func(tower.clean_up, block),
bubbles.get_sequence(self.get_color(), pos))
)
para.start()
class MultiColorBall(Balls):
def __init__(self):
super().__init__('multicolor_ball')
self.model.set_texture(base.loader.load_texture(PATH_TEXTURE_MULTI), 1)
def _hit(self, color, bubbles, tower):
for block in tower.judge_colors(lambda x: x.get_color() == color):
pos = block.get_pos(base.render)
yield Sequence(Func(tower.clean_up, block),
bubbles.get_sequence(color, pos))
def hit(self, clicked_pos, block, bubbles, tower):
color = block.get_color()
Parallel(
bubbles.get_sequence(color, clicked_pos),
*[seq for seq in self._hit(color, bubbles, tower)]
).start()
class TwoToneBall(Balls):
def __init__(self):
super().__init__('twotone_ball')
self.model.set_texture(base.loader.load_texture(PATH_TEXTURE_TWOTONE), 1)
def _hit(self, color, bubbles, tower):
for block in tower.judge_colors(lambda x: x.get_color() != color):
pos = block.get_pos(base.render)
color = block.get_color()
yield Sequence(Func(tower.clean_up, block),
bubbles.get_sequence(color, pos))
def hit(self, clicked_pos, block, bubbles, tower):
color = block.get_color()
Parallel(
bubbles.get_sequence(Colors.random_select(), clicked_pos),
*[seq for seq in self._hit(color, bubbles, tower)]
).start()