ya2 · news · projects · code · about

first level
authorFlavio Calva <f.calva@gmail.com>
Wed, 26 Jan 2022 18:09:15 +0000 (19:09 +0100)
committerFlavio Calva <f.calva@gmail.com>
Wed, 26 Jan 2022 18:09:15 +0000 (19:09 +0100)
pmachines/items/basketball.py
pmachines/items/box.py
pmachines/items/domino.py
pmachines/items/item.py
pmachines/items/shelf.py
pmachines/items/teetertooter.py
pmachines/scene.py
prj.org

index c88b70cca9de23b4a8d43c6db3d4a58564020fd9..bb8625b8d2bb955e12ff1d1212e85ce4839ee696 100644 (file)
@@ -4,8 +4,8 @@ from pmachines.items.item import Item
 
 class Basketball(Item):
 
-    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos):
-        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/basketball/basketball.gltf', .4)
+    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos, mass=1, pos=(0, 0, 0), r=0, count=0):
+        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/basketball/basketball.gltf', .4, mass=mass, pos=pos, r=r, count=count)
 
     def _set_shape(self):
         self.node.add_shape(BulletSphereShape(1))
index ac15d16cfc7800cdf8775b41858da013fcc55404..7e9fb944e0f7f2beed2f980ac1d186fd87ab0d43 100644 (file)
@@ -4,8 +4,8 @@ from pmachines.items.item import Item
 
 class Box(Item):
 
-    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos):
-        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/box/box.gltf')
+    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos, mass=1, pos=(0, 0, 0), r=0, count=0):
+        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/box/box.gltf', mass=mass, pos=pos, r=r, count=count)
 
     def _set_shape(self):
         self.node.add_shape(BulletBoxShape((.5, .5, .5)))
index 5985a60992f114c233822d9f0baa0844484e0eba..c80273f04f4dc0e5f5e6e74ec33d510c0a4f4fed 100644 (file)
@@ -4,8 +4,8 @@ from pmachines.items.item import Item
 
 class Domino(Item):
 
-    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos):
-        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/domino/domino.gltf')
+    def __init__(self, world, plane_node, cb_inst, curr_bottom, repos, mass=1, pos=(0, 0, 0), r=0, count=0):
+        super().__init__(world, plane_node, cb_inst, curr_bottom, repos, 'assets/gltf/domino/domino.gltf', mass=mass, pos=pos, r=r, count=count)
 
 
     def _set_shape(self):
index 23d70d91aa7830abc435cb99515e5b6447d44686..81fdb912e002a9fe1351b8eaba526cbf4eb08b35 100644 (file)
@@ -14,7 +14,7 @@ class Command:
 
 class Item:
 
-    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, scene_repos, model_path, model_scale=1, exp_num_contacts=1):
+    def __init__(self, world, plane_node, cb_inst, curr_bottom, scene_repos, model_path, model_scale=1, exp_num_contacts=1, mass=1, pos=(0, 0, 0), r=0, count=0):
         self._world = world
         self._plane_node = plane_node
         self._count = count
@@ -23,6 +23,9 @@ class Item:
         self._overlapping = False
         self._first_command = True
         self.repos_done = False
+        self._mass = mass
+        self._pos = pos
+        self._r = r
         self._exp_num_contacts = exp_num_contacts
         self._curr_bottom = curr_bottom
         self._scene_repos = scene_repos
@@ -30,26 +33,38 @@ class Item:
         self._model_path = model_path
         self._commands = []
         self._command_idx = -1
-        self.node = BulletGhostNode(self.__class__.__name__)
+        if count:
+            self.node = BulletGhostNode(self.__class__.__name__)
+        else:
+            self.node = BulletRigidBodyNode(self.__class__.__name__)
         self._set_shape()
         self._np = render.attach_new_node(self.node)
-        world.attach_ghost(self.node)
+        if count:
+            world.attach_ghost(self.node)
+        else:
+            world.attach_rigid_body(self.node)
         self._model = loader.load_model(model_path)
         set_srgb(self._model)
         self._model.flatten_light()
         self._model.reparent_to(self._np)
-        self._set_outline_model()
         self._np.set_scale(model_scale)
-        set_srgb(self._outline_model)
-        self._model.hide(BitMask32(0x01))
-        self._outline_model.hide(BitMask32(0x01))
+        if count:
+            self._set_outline_model()
+            set_srgb(self._outline_model)
+            self._model.hide(BitMask32(0x01))
+            self._outline_model.hide(BitMask32(0x01))
         self._start_drag_pos = None
         self._prev_rot_info = None
         self._last_nonoverlapping_pos = None
         self._last_nonoverlapping_rot = None
-        self._instantiated = False
+        self._instantiated = not count
+        self.interactable = count
         self._box_tsk = taskMgr.add(self.on_frame, 'on_frame')
-        self._repos()
+        if count:
+            self._repos()
+        else:
+            self._np.set_pos(pos)
+            self._np.set_r(r)
 
     def _set_shape(self):
         pass
@@ -138,7 +153,7 @@ class Item:
         if not self._instantiated:
             return
         self._world.remove_rigid_body(self.node)
-        self.node.set_mass(1)
+        self.node.set_mass(self._mass)
         self._world.attach_rigid_body(self.node)
 
     def on_click_l(self, pos):
@@ -163,7 +178,7 @@ class Item:
             self._txt.destroy()
             self._count -= 1
             if self._count:
-                item = self.__class__(self._world, self._plane_node, self._count, self._cb_inst, self._curr_bottom, self._scene_repos)  # pylint: disable=no-value-for-parameter
+                item = self.__class__(self._world, self._plane_node, self._cb_inst, self._curr_bottom, self._scene_repos, count=self._count, mass=self._mass, pos=self._pos, r=self._r)  # pylint: disable=no-value-for-parameter
                 self._cb_inst(item)
             self._scene_repos()
 
@@ -187,12 +202,13 @@ class Item:
             self._outline_model.set_color_scale(.4, .4, .4, 1)
 
     def on_mouse_on(self):
-        if not self._paused:
+        if not self._paused and self.interactable:
             self._outline_model.show()
 
     def on_mouse_off(self):
         if self._start_drag_pos or self._prev_rot_info: return
-        self._outline_model.hide()
+        if self.interactable:
+            self._outline_model.hide()
 
     def on_mouse_move(self, pos):
         if self._start_drag_pos:
@@ -235,19 +251,20 @@ class Item:
         self._paused = True
         self._model.set_transparency(True)
         self._model.set_alpha_scale(.3)
-        if not self._txt.is_empty():
+        if hasattr(self, '_txt') and not self._txt.is_empty():
             self._txt.set_alpha_scale(.3)
 
     def restore_state(self):
         self._paused = False
         self._model.set_alpha_scale(1)
-        if not self._txt.is_empty():
+        if hasattr(self, '_txt') and not self._txt.is_empty():
             self._txt.set_alpha_scale(1)
 
     def destroy(self):
         self._np.remove_node()
         taskMgr.remove(self._box_tsk)
-        self._txt.destroy()
+        if hasattr(self, '_txt'):
+            self._txt.destroy()
         if not self._instantiated:
             self._world.remove_ghost(self.node)
         else:
index 178dd27b806b12fddb8101ee52ade34dc628ecb1..558ad25688a8bbc083ca59a88a98ec665c7fff1b 100644 (file)
@@ -4,8 +4,8 @@ from pmachines.items.item import Item
 
 class Shelf(Item):
 
-    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos):
-        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/shelf/shelf.gltf')
+    def __init__(self, world, plane_node, cb_inst, curr_bottom, repos, mass=1, pos=(0, 0, 0), r=0, count=0):
+        super().__init__(world, plane_node, cb_inst, curr_bottom, repos, 'assets/gltf/shelf/shelf.gltf', mass=mass, pos=pos, r=r, count=count)
 
     def _set_shape(self):
         self.node.add_shape(BulletBoxShape((1, .5, .05)))
index 92cb23dda9c523d30ce113e7b6219c08cdadd01e..9daf3e26632cb16da0ff177101c8e77430c6e827 100644 (file)
@@ -5,8 +5,8 @@ from pmachines.items.item import Item
 
 class TeeterTooter(Item):
 
-    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos):
-        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/teeter_tooter/teeter_tooter.gltf', exp_num_contacts=2)
+    def __init__(self, world, plane_node, count, cb_inst, curr_bottom, repos, mass=1, pos=(0, 0, 0), r=0, count=0):
+        super().__init__(world, plane_node, count, cb_inst, curr_bottom, repos, 'assets/gltf/teeter_tooter/teeter_tooter.gltf', exp_num_contacts=2, mass=1, pos=(0, 0, 0), r=0, count=0)
 
     def _set_shape(self):
         self.node.add_shape(
index 9ac3178ae5d97afb73428ce6c34fb197da15771e..2e95a8617376e2cadbd48e39ba5b661f603969d7 100644 (file)
@@ -55,11 +55,14 @@ class Scene(DirectObject):
 
     def reset(self):
         [itm.destroy() for itm in self.items]
-        self.items = [Box(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)]
-        self.items += [Shelf(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)]
-        self.items += [Domino(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)]
-        self.items += [Basketball(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)]
-        self.items += [TeeterTooter(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)]
+        self.items = []
+        #self.items += [Box(self._world, self._mouse_plane_node, self.cb_inst, self.current_bottom, self.repos, count=3)]
+        #self.items += [Shelf(self._world, self._mouse_plane_node, self.cb_inst, self.current_bottom, self.repos, count=3)]
+        self.items += [Shelf(self._world, self._mouse_plane_node, self.cb_inst, self.current_bottom, self.repos, mass=0, pos=(-1.2, 0, 1), r=10)]
+        self.items += [Shelf(self._world, self._mouse_plane_node, self.cb_inst, self.current_bottom, self.repos, mass=0, pos=(1.2, 0, 1), r=-10)]
+        self.items += [Domino(self._world, self._mouse_plane_node, self.cb_inst, self.current_bottom, self.repos, count=3)]
+        #self.items += [Basketball(self._world, self._mouse_plane_node, self.cb_inst, self.current_bottom, self.repos, count=3)]
+        #self.items += [TeeterTooter(self._world, self._mouse_plane_node, self.cb_inst, self.current_bottom, self.repos, count=3)]
         self._commands = []
         self._command_idx = 0
 
@@ -212,7 +215,7 @@ class Scene(DirectObject):
             if hit.get_node() == self._mouse_plane_node:
                 pos = hit.get_hit_pos()
         for hit in self._get_hits():
-            for item in [i for i in self.items if hit.get_node() == i.node]:
+            for item in [i for i in self.items if hit.get_node() == i.node and i.interactable]:
                 if not self._item_active:
                     self._item_active = item
                 getattr(item, method)(pos)
diff --git a/prj.org b/prj.org
index c14fa1b4a28123dc710e896c868244ffd4fd330d..58c2c3e593bcb934aea5b8c8a2fcf80f96d7ecf8 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -1,6 +1,6 @@
 * issues
 * todo
-** first level (domino + shelf)
+** ending condition
 ** script for creating a screenshot of the level
 ** second level (box + shelf)
 ** level menu