From: Flavio Calva Date: Sat, 8 Jan 2022 17:12:19 +0000 (+0100) Subject: instantiation X-Git-Url: http://git.ya2.it/?p=pmachines.git;a=commitdiff_plain;h=7850ccc485a9bf387a6c9f12ce86b1e5c2b2d059 instantiation --- diff --git a/assets/fonts/Hanken-Book.ttf b/assets/fonts/Hanken-Book.ttf new file mode 100644 index 0000000..c690722 Binary files /dev/null and b/assets/fonts/Hanken-Book.ttf differ diff --git a/pmachines/items/box.py b/pmachines/items/box.py index c0419bb..493ae2a 100644 --- a/pmachines/items/box.py +++ b/pmachines/items/box.py @@ -1,15 +1,19 @@ -from panda3d.core import CullFaceAttrib +from panda3d.core import CullFaceAttrib, Point3, NodePath, Point2, Texture from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode +from direct.gui.OnscreenText import OnscreenText + class Box: - def __init__(self, world): + def __init__(self, world, plane_node, count, cb_inst): self._world = world + self._plane_node = plane_node + self._count = count + self._cb_inst = cb_inst shape = BulletBoxShape((.5, .5, .5)) self.node = BulletRigidBodyNode('box') self.node.add_shape(shape) self._np = render.attach_new_node(self.node) - self._np.set_pos(0, 0, 1) world.attach_rigid_body(self.node) model = loader.load_model('assets/gltf/box/box.gltf') model.flatten_light() @@ -17,7 +21,40 @@ class Box: self._set_outline_model() self._start_drag_pos = None self._prev_rot_info = None + self._instantiated = False taskMgr.add(self.on_frame, 'on_frame') + #self._set_side() + taskMgr.doMethodLater(.01, lambda task: self._set_side(), 'a') + # i get weird values in the first frame; i could restore this approach + # when the hook with the events of the window is up + + def _set_side(self): + p_from, p_to = Point3(), Point3() # in camera coordinates + base.camLens.extrude((-1, 1), p_from, p_to) + p_from = render.get_relative_point(base.cam, p_from) # global coords + p_to = render.get_relative_point(base.cam, p_to) # global coords + for hit in self._world.ray_test_all(p_from, p_to).get_hits(): + if hit.get_node() == self._plane_node: + pos = hit.get_hit_pos() + bounds = self._np.get_tight_bounds() + dpos = bounds[1][0], 0, -bounds[1][2] + self._np.set_pos(pos + dpos) + new_node = NodePath('temp') + new_node.set_pos(pos + dpos + (bounds[1][0], bounds[1][1], -bounds[1][2])) + coord3d = new_node.get_pos(base.cam) + coord2d = Point2() + base.camLens.project(coord3d, coord2d) + coord_r2d = Point3(coord2d[0], 0, coord2d[1]) + coord_a2d = base.aspect2d.get_relative_point(render2d, coord_r2d) + font = base.loader.load_font('assets/fonts/Hanken-Book.ttf') + font.clear() + font.set_pixels_per_unit(60) + font.set_minfilter(Texture.FTLinearMipmapLinear) + font.set_outline((0, 0, 0, 1), .8, .2) + self._txt = OnscreenText( + str(self._count), pos=(coord_a2d[0], coord_a2d[2]), + font=font, scale=0.06, fg=(.9, .9, .9, 1)) + new_node.remove_node() def _set_outline_model(self): self._outline_model = loader.load_model('assets/gltf/box/box.gltf') @@ -42,6 +79,13 @@ class Box: def on_click_l(self, pos): self._start_drag_pos = pos, self._np.get_pos() loader.load_sfx('assets/audio/sfx/grab.ogg').play() + if not self._instantiated: + self._instantiated = True + self._txt.destroy() + self._count -= 1 + if self._count: + box = Box(self._world, self._plane_node, self._count, self._cb_inst) + self._cb_inst(box) def on_click_r(self, pos): self._prev_rot_info = pos, self._np.get_pos(), self._np.get_r() diff --git a/pmachines/scene.py b/pmachines/scene.py index f67c45b..5d046e9 100644 --- a/pmachines/scene.py +++ b/pmachines/scene.py @@ -23,7 +23,7 @@ class Scene(DirectObject): self._set_input() self._set_mouse_plane() Background() - self.items = [Box(world)] + self.items = [Box(world, self._mouse_plane_node, 3, self.cb_inst)] taskMgr.add(self.on_frame, 'on_frame') def _set_camera(self): @@ -111,8 +111,7 @@ class Scene(DirectObject): def _get_hits(self): if not base.mouseWatcherNode.has_mouse(): return [] - p_from = Point3() # in camera coordinates - p_to = Point3() # in camera coordinates + p_from, p_to = Point3(), Point3() # in camera coordinates base.camLens.extrude(base.mouseWatcherNode.get_mouse(), p_from, p_to) p_from = render.get_relative_point(base.cam, p_from) # global coords p_to = render.get_relative_point(base.cam, p_to) # global coords @@ -153,6 +152,9 @@ class Scene(DirectObject): [itm.on_mouse_move(pos) for itm in self.items] return task.cont + def cb_inst(self, item): + self.items += [item] + def on_play(self): [itm.play() for itm in self.items] diff --git a/prj.org b/prj.org index 311ee94..f4c05e2 100644 --- a/prj.org +++ b/prj.org @@ -1,8 +1,7 @@ * issues * todo -** instantiation of the objects -*** model in the left side with a counter -*** when you click a new object is created iff counter > 0 +** update the to-be-instantiated's pos if the windows is updated +** manage the physics of the to-be-instantiated with a ghost ** instructions ** main menu ** implement the operations of the buttons