# Commits

committed f70d89b Draft

Fix: make balloons work

• Participants
• Parent commits 3a727b4

# korovic/components/balloons.py

class Balloon(Component):
LIFT = v(0, 20000)
-    MASS = 5
+    MASS = 3
+    ALT_ATTENUATION = 0.00001  # how fast lift drops off with altitude
-    yfix = -1    # This is a bodge to fix insertion points - see base class

def __init__(self, squid, attachment_point):
super(Balloon, self).__init__(squid, attachment_point)
+        self.reset()
+
+    def reset(self):
self.tether = None
self.create_body()
-        self.tether_to(squid.body)
+        self.tether_to(self.squid.body)

def bodies_and_shapes(self):
bs = super(Balloon, self).bodies_and_shapes()
bs.extend(self.tether.bodies_and_shapes())
return bs

+    def home_position(self):
+        return Component.get_position(self) + v(0, 100)
+
def tether_to(self, body):
-        self.body.position = body.position - self.insertion_point
+        self.body.position = self.home_position()
self.tether = Tether(
-            a=self.body.local_to_world(self.insertion_point),
+            a=self.body.local_to_world(-self.insertion_point),
b=v(body.position + self.attachment_point),
c1=self.body,
c2=body,
-            density=5,
-            segments=10
+            density=0.1,
+            segments=2
)

def update(self, dt):
self.body.reset_forces()
-        self.body.apply_force(self.LIFT)
+        alt = self.body.position.y
+        if alt < 0:
+            frac = 1.0
+        else:
+            # lift drops off with altitude
+            frac = 1.0 / (1 + alt * 0.0001)
+        self.body.apply_force(frac * self.LIFT)

-    @property
-    def position(self):
+    def get_position(self):
return v(self.body.position)

+    def set_position(self, pos):
+        self.body.position = v(pos)
+        a = self.body.local_to_world(-self.insertion_point)
+        b = Component.get_position(self)
+        self.tether.reorient(a, b)
+        self.body.angle = 0
+
+    position = property(get_position, set_position)
+
@property
def rotation(self):
return self.body.angle

# korovic/components/base.py

self.squid = squid
self.attachment_point = attachment_point
self.sprite = pyglet.sprite.Sprite(self.image, 0, 0)
+        self.body = None

def bodies_and_shapes(self):
+        if self.body is None:
+            return []
bs = [self.body] + self.shapes
try:
for c in self.components:
icon.scale = float(size) / s
return icon

-    @property
-    def position(self):
+    def get_position(self):
"""World position of the component."""
p = self.attachment_point + self.insertion_point.rotated(math.degrees(self.angle))  # position of the insertion point in body space
return v(self.squid.body.local_to_world(p))
+    position = property(get_position)
+
+    home_position = get_position

@property
def rotation(self):

# korovic/components/squid.py

bs = [self.body] + self.shapes
for s in self.spikes:
bs.extend(s.bodies_and_shapes())
+        for c in self.slots.components:
+            bs.extend(c.bodies_and_shapes())
return bs

def create_body(self):
#            TentacleSpike(self, v(-90, 0)),
]

-    def reset(self):
+    def reset(self, position=(250, 80)):
self.create_body()
self.body.angular_velocity_limit = 1.5  # Ensure Susie can't spin too fast
self.body.mass = self.total_weight()
self.fuel = self.fuel_capacity()
-        self.position = (250, 80)
+        self.position = position
for a in self.slots.components:
a.reset()

return self.fuel > 0

def set_position(self, pos):
-        trans = pos - self.body.position
self.body.position = pos
self.sprite.position = pos
+        for c in self.slots.components:
+            try:
+                c.position = Component.get_position(c)
+            except AttributeError:
+                # position isn't settable, probably because it's derived
+                pass
#        self.spikes[0].position += trans

def get_position(self):

# korovic/data/components/balloon.json

-{"points": [{"radius": 35, "name": "2", "offset": [39, 37]}, {"radius": 30, "name": "3", "offset": [40, 65]}], "radius": 6, "name": "sprites/balloon.png", "offset": [-38, -194]}
+{"points": [{"radius": 35, "name": "2", "offset": [39, 37]}, {"radius": 30, "name": "3", "offset": [40, 65]}], "radius": 6, "name": "sprites/balloon.png", "offset": [-40, -112]}

# korovic/editor_hud.py

Item('Propeller', 100, components.Propeller, 'Contact!'),
Item('Small Fuel Tank', 40, components.SmallFuelTank, 'A little fuel goes a long way!'),
Item('Large Fuel Tank', 75, components.LargeFuelTank, 'Is zis too much fuel for you?'),
-#    Item('Balloon', 10, components.Balloon, 'Ninety-nine Luftballoons...'),
+    Item('Balloon', 10, components.Balloon, 'Ninety-nine Luftballoons...'),
Item('Pulsejet', 150, components.PulseJet, 'Vould you like to cruise like a V-1, Susie?'),
Item('Rotor', 500, components.Rotor, 'You are Susie, not Huey, yes?'),
Item('Hang Glider', 100, components.HangGlider, 'And it vill keep ze sun off you!'),

# korovic/scene.py

self.editor = None

def start(self):
-        self.squid.position = (255, 145)
-        self.squid.rotation = 0
-        self.squid.body.velocity = (0, 0)
+        self.squid.reset(position=(255, 145))
self.hud = EditorHud(self.squid, max_money=self.world.money)

def stop(self):