From f837fb6f1d40087a3686d2e22c70e05be02e58f3 Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Sat, 30 Aug 2008 21:45:22 +0000 Subject: create a tool for adding resolute joints --- diff --git a/bridge.py b/bridge.py index 76daffc..6dac4c7 100644 --- a/bridge.py +++ b/bridge.py @@ -1,14 +1,23 @@ import pygame -def create_world(game): - rect = pygame.Rect((0,800), (350, -250)) - rect.normalize() - pygame.draw.rect(game.screen, (100,180,255), rect, 3) - game.world.add.rect(rect.center, rect.width / 2, rect.height / 2, - dynamic=False) - rect = pygame.Rect((1200,800), (-350, -250)) - rect.normalize() - pygame.draw.rect(game.screen, (100,180,255), rect, 3) - game.world.add.rect(rect.center, rect.width / 2, rect.height / 2, - dynamic=False) +class Bridge: + def __init__(self, game): + self.game = game + self.screen = game.screen + self.world = game.world + + def create_world(self): + rect = pygame.Rect((0,800), (350, -250)) + rect.normalize() + pygame.draw.rect(self.screen, (100,180,255), rect, 3) + self.world.add.rect(rect.center, rect.width / 2, rect.height / 2, + dynamic=False) + rect = pygame.Rect((1200,800), (-350, -250)) + rect.normalize() + pygame.draw.rect(self.screen, (100,180,255), rect, 3) + self.world.add.rect(rect.center, rect.width / 2, rect.height / 2, + dynamic=False) + + def joint_added(self, joint): + print "joint added!" diff --git a/physics.py b/physics.py index 6d9fb78..b76db14 100644 --- a/physics.py +++ b/physics.py @@ -23,7 +23,7 @@ import olpcgames import elements from elements import Elements import tools -import bridge +from bridge import Bridge from helpers import * class PhysicsGame: @@ -35,6 +35,7 @@ class PhysicsGame: self.canvas = olpcgames.ACTIVITY.canvas self.joystickobject = None self.debug = True + # create the name --> instance map for components self.toolList = {} for c in tools.allTools: @@ -48,7 +49,8 @@ class PhysicsGame: # set up static environment self.world.add.ground() - bridge.create_world(self) + self.bridge = Bridge(self) + self.bridge.create_world() def run(self): self.running = True diff --git a/tools.py b/tools.py index 0ce23d9..1302d8c 100644 --- a/tools.py +++ b/tools.py @@ -4,6 +4,7 @@ # By Alex Levenson #================================================================== import pygame +from elements import box2d from pygame.locals import * from helpers import * from inspect import getmro @@ -92,6 +93,9 @@ class Tool(object): # default cancel doesn't do anything pass + def to_b2vec(self, pt): + return self.game.world.add.to_b2vec(pt) + # The circle creation tool class CircleTool(Tool): name = "circle" @@ -430,6 +434,41 @@ class JoystickTool(Tool): self.vertices = None +# The joint tool +class BridgeJointTool(Tool): + name = "bridgejoint" + icon = "joint" + toolTip = "Bridge Joint" + + def __init__(self,gameInstance): + self.game = gameInstance + self.name = "Bridge Joint" + self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None + def handleEvents(self,event): + #look for default events, and if none are handled then try the custom events + if super(BridgeJointTool,self).handleEvents(event): + return + if event.type != MOUSEBUTTONUP or event.button != 1: + return + + print "mouse button up" + bodies = self.game.world.get_bodies_at_pos(event.pos) + if not bodies or len(bodies) != 2: + return + + print bodies[0] + print bodies[1] + jointDef = box2d.b2RevoluteJointDef() + jointDef.Initialize(bodies[0], bodies[1], self.to_b2vec(event.pos)) + joint = self.game.world.world.CreateJoint(jointDef) + self.game.bridge.joint_added(joint) + + def draw(self): + return + + def cancel(self): + self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None + def getAllTools(): this_mod = __import__(__name__) all = [val for val in this_mod.__dict__.values() if isinstance(val, type)] -- cgit v0.9.1