ya2 · news · projects · code · about

fixed logging
[pmachines.git] / pmachines / items / box.py
CommitLineData
37ba71d8 1from panda3d.core import CullFaceAttrib
1be87278
FC
2from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
3
4class Box:
5
6 def __init__(self, world):
c8d8653f 7 self._world = world
1be87278
FC
8 shape = BulletBoxShape((.5, .5, .5))
9 self.node = BulletRigidBodyNode('box')
1be87278 10 self.node.add_shape(shape)
c8d8653f
FC
11 self._np = render.attach_new_node(self.node)
12 self._np.set_pos(0, 0, 1)
1be87278
FC
13 world.attach_rigid_body(self.node)
14 model = loader.load_model('assets/gltf/box/box.gltf')
15 model.flatten_light()
c8d8653f
FC
16 model.reparent_to(self._np)
17 self._set_outline_model()
18 self._start_drag_pos = None
49c79300
FC
19 self._start_rot_info = None
20 taskMgr.add(self.on_frame, 'on_frame')
37ba71d8 21
c8d8653f 22 def _set_outline_model(self):
37ba71d8
FC
23 self._outline_model = loader.load_model('assets/gltf/box/box.gltf')
24 clockw = CullFaceAttrib.MCullClockwise
25 self._outline_model.set_attrib(CullFaceAttrib.make(clockw))
c8d8653f 26 self._outline_model.reparent_to(self._np)
37ba71d8
FC
27 self._outline_model.set_scale(-1.08, -1.08, -1.08)
28 self._outline_model.set_light_off()
29 self._outline_model.set_color(.4, .4, .4, 1)
30 self._outline_model.set_color_scale(.4, .4, .4, 1)
31 self._outline_model.hide()
1be87278 32
49c79300
FC
33 def on_frame(self, task):
34 self._np.set_y(0)
35 return task.cont
36
c8d8653f
FC
37 def play(self):
38 self._world.remove_rigid_body(self.node)
39 self.node.set_mass(1)
40 self._world.attach_rigid_body(self.node)
41
49c79300 42 def on_click_l(self, pos):
c8d8653f 43 self._start_drag_pos = pos, self._np.get_pos()
54a1397e 44 loader.load_sfx('assets/audio/sfx/grab.ogg').play()
c8d8653f 45
49c79300
FC
46 def on_click_r(self, pos):
47 self._start_rot_info = pos, self._np.get_pos(), self._np.get_r()
54a1397e 48 loader.load_sfx('assets/audio/sfx/grab.ogg').play()
49c79300 49
c8d8653f 50 def on_release(self):
54a1397e
FC
51 if self._start_drag_pos or self._start_rot_info:
52 loader.load_sfx('assets/audio/sfx/release.ogg').play()
49c79300 53 self._start_drag_pos = self._start_rot_info = None
37ba71d8
FC
54
55 def on_mouse_on(self):
56 self._outline_model.show()
57
58 def on_mouse_off(self):
49c79300 59 if self._start_drag_pos or self._start_rot_info: return
37ba71d8 60 self._outline_model.hide()
c8d8653f
FC
61
62 def on_mouse_move(self, pos):
49c79300
FC
63 if self._start_drag_pos:
64 d_pos = pos - self._start_drag_pos[0]
65 self._np.set_pos(self._start_drag_pos[1] + d_pos)
66 if self._start_rot_info:
67 start_vec = self._start_rot_info[0] - self._start_rot_info[1]
68 curr_vec = pos - self._start_rot_info[1]
69 d_angle = curr_vec.signed_angle_deg(start_vec, (0, -1, 0))
70 self._np.set_r(self._start_rot_info[2] + d_angle)