Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
blueloveTH committed Aug 13, 2023
1 parent 27ef100 commit d1568bb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pocketpy
25 changes: 12 additions & 13 deletions workdir/ct/_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def __init__(self, name: str, parent: 'Node'=None) -> None:
self.scale = vec2(1, 1)
# hierarchy
self.children = {}
# box2d
self.b2_body = None # box2d body attached to this node

if parent is None:
parent = Node._root
Expand All @@ -51,8 +49,15 @@ def get_node(self, path: str) -> 'Node':
node = node.children[name]
return node

def create_body(self) -> box2d.Body:
"""create a box2d body attached to this node with RAII enabled"""
return self.RAII(box2d.Body(Node._b2_world, node=self))

def RAII(self, obj):
"""automatically destroy the object when this node is destroyed"""
"""automatically destroy the object when this node is destroyed
+ `obj`: an object which contains a `destroy` method
"""
self._raii_objects.append(obj)
return obj

Expand Down Expand Up @@ -131,24 +136,18 @@ def stop_all_coroutines(self):
for i in range(len(self._coroutines)):
self._coroutines[i] = None

# box2d step callbacks
def on_box2d_pre_step(self):
if self.b2_body is None:
return
self.b2_body.position = self.position
self.b2_body.rotation = self.rotation
pass

def on_box2d_post_step(self):
if self.b2_body is None:
return
self.position = self.b2_body.position
self.rotation = self.b2_body.rotation
pass



def get_node(path: str) -> Node:
return Node._root.get_node(path)



def build_scene_tree(tree: dict, g, root: Node = None):
root: Node = root or Node._root
for key, child in tree.items():
Expand Down
2 changes: 1 addition & 1 deletion workdir/ct/_tilemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def bake_box2d_bodies(self, node: Node) -> list[box2d.Body]:
transform = node.transform()
cell_size = vec2(self.cell_size.x, self.cell_size.y)
for path in res:
body = box2d.Body(Node._b2_world, node)
body = node.create_body()
body.type = 0 # static
vertices = []
for v in path:
Expand Down
15 changes: 13 additions & 2 deletions workdir/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Player(Node):
def on_ready(self):
self.tex = load_texture('assets/kenney_simple-space/ship_G.png')
self.speed = 1
self.b2_body = Node._b2_world.create_body(self)
self.b2_body = self.create_body()
# self.b2_body.set_circle_shape(0.5 * self.scale.x)
self.b2_body.set_box_shape(0.5 * self.scale.x, 0.5 * self.scale.y)
# self.b2_body.velocity = vec2(-1, 0)
Expand Down Expand Up @@ -56,13 +56,24 @@ class Ball(Node):
def on_ready(self):
self.tex = load_texture('assets/circle_64x64.png')
self.scale = vec2(1, 1) * 0.4
self.b2_body = self.RAII(box2d.Body(Node._b2_world, node=self))
self.b2_body = self.create_body()
self.b2_body.set_circle_shape(0.32 * self.scale.x)
self.b2_body.velocity = vec2(1.5, 0)

def on_render(self):
draw_sprite(self.transform(), self.tex)

# box2d step callbacks
def on_box2d_pre_step(self):
self.b2_body.position = self.position
self.b2_body.rotation = self.rotation

def on_box2d_post_step(self):
self.position = self.b2_body.position
self.rotation = self.b2_body.rotation



class Cell:
def __init__(self, tex, tint_col: vec4):
if tex is None:
Expand Down

0 comments on commit d1568bb

Please sign in to comment.