get in and out of vehicles
authoradrianna <adrianna.pinska@gmail.com>
Sat, 5 Mar 2016 21:41:54 +0000 (23:41 +0200)
committeradrianna <adrianna.pinska@gmail.com>
Sat, 5 Mar 2016 21:42:05 +0000 (23:42 +0200)
koperkapel/gamelib/items.py
koperkapel/levels/level1.json
koperkapel/scenes/level.py
koperkapel/vehicles/base.py

index b61125016bb58b9707751ae655e9d031df5c6943..14bf61d05d37eb1a1012496a1c1595dd1e471a99 100644 (file)
@@ -2,6 +2,7 @@
 
 from ..serums import default_serums
 from ..weapons import default_weapons
+from ..vehicles.base import Vehicle
 
 
 def build_serum(item):
@@ -13,10 +14,15 @@ def build_weapon(item):
     actor = default_weapons.assemble(item["weapon"])
     return actor
 
+def build_vehicle(item):
+    actor = Vehicle.by_type(item["vehicle"]).get_avatar(dead=True)
+    return actor
+
 
 ITEM_BUILDERS = {
     "serum": build_serum,
     "weapon": build_weapon,
+    "vehicle": build_vehicle,
 }
 
 
@@ -39,3 +45,12 @@ def clone_old_item(item, **new_data):
     new_item.update(item.item_data)
     new_item.update(new_data)
     return get_item(new_item)
+
+
+def create_new_item(item_type, pos, **kwd):
+    new_item = {
+        "type": item_type,
+        "pos": pos,
+    }
+    new_item.update(kwd)
+    return get_item(new_item)
index 7ec08b52b4d24e6cebc67b0e72ba57f1d91a5350..f5e920af4e66575109a43159359697375cb91ad6 100644 (file)
         "serum": "fast",
         "pos": [2, 4]
       },
+      {
+          "type": "vehicle",
+          "vehicle": "roomba",
+          "pos": [2, 7]
+      },
       {
         "type": "serum",
         "serum": "smart",
index b17beb61231f1e2d817fe411d529e8e52eb535bc..c5a847b37de719da32810dd786ad36a7e78aa998 100644 (file)
@@ -7,7 +7,7 @@ from ..loaders.levelloader import levels
 from .base import (
     Scene, ChangeSceneEvent, MoveViewportEvent, WorldEvent, defer_to_update)
 from ..constants import TILE_SIZE, WIDTH, HEIGHT
-from ..gamelib.items import clone_old_item
+from ..gamelib.items import clone_old_item, create_new_item
 from ..roaches import build_roach
 from ..vehicles.base import Vehicle
 from ..weapons import weapon_by_name
@@ -231,8 +231,24 @@ class GameLevelScene(BaseLevelScene):
 
     @defer_to_update
     def _change_vehicle(self, world):
-        vehicle = Vehicle.random()
-        world.vehicles.current = vehicle
+        x, y = self._player_pos
+
+        old_vehicle = world.vehicles.current
+        
+        item = self._level.item_at(x, y)
+        
+        if item and item.item_type == "vehicle":
+            world.vehicles.current = item.item_data["vehicle"]
+            self._level.remove_item(item)
+            self._items.remove(item)
+        else:
+            world.vehicles.current = "walking"
+
+        if old_vehicle != "walking":
+            dropped_vehicle = create_new_item("vehicle", (x, y), vehicle=old_vehicle)
+            self._level.items.append(dropped_vehicle)
+            self._items.add(dropped_vehicle)
+
         self._vehicle_changed()
 
     def update(self, world, engine, dt):
@@ -316,9 +332,10 @@ class GameLevelScene(BaseLevelScene):
             self._add_roach()
         elif self._level.item_at(x, y):
             item = self._level.item_at(x, y)
-            self._level.remove_item(item)
-            self._items.remove(item)
-            self._gain_item(item)
+            if item.item_type != "vehicle":
+                self._level.remove_item(item)
+                self._items.remove(item)
+                self._gain_item(item)
         elif self._level.is_exit(x, y):
             next_level = self._level.get_exit_level()
             return [
index 2c1d9bf97d5b2e2109fc7f7cc4fbf129aacd2a5c..258ffe0fee53e93229f2dcf1270e77c76cf2e506 100644 (file)
@@ -96,13 +96,17 @@ class Vehicle:
         vehicle = images.load(safepath("vehicle%s/%s_%d") % (
             suffix, self.vehicle_type, i + 1))
         frame = vehicle.copy()
-        frame.blit(weapon.surf, (0, 0))
+        if weapon:
+            frame.blit(weapon.surf, (0, 0))
         return frame
 
-    def get_avatar(self, world):
-        weapon = default_weapons.assemble(
+    def get_avatar(self, world=None, dead=False):
+        if not dead:
+            weapon = default_weapons.assemble(
             world.weapons.current, tape=self.weapons_taped_on)
-        frames = [self._avatar_frame(i, weapon) for i in range(4)]
+            frames = [self._avatar_frame(i, weapon) for i in range(4)]
+        else:
+            frames = [self._avatar_frame(0, weapon=None)]
         return AnimatedSurfActor(frames, anchor=(0, 0))