tiles.py
  1  # SPDX-FileCopyrightText: 2020 FoamyGuy for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  from tilegame_assets.states import (
  6      STATE_MAPWIN,
  7      STATE_LOST_SPARKY,
  8      STATE_MINERVA,
  9  )
 10  # pylint: disable=unused-argument
 11  
 12  
 13  # Minerva before_move. Set game state to STATE_MINERVA
 14  def minerva_walk(to_coords, from_coords, entity_obj, GAME_STATE):
 15      GAME_STATE["STATE"] = STATE_MINERVA
 16      return False
 17  
 18  
 19  # Sparky before_move. If user does not have a Mho in inventory they lose.
 20  # If user does have Mho subtract one from inventory and consume Sparky.
 21  def sparky_walk(to_coords, from_coords, entity_obj, GAME_STATE):
 22      if GAME_STATE["INVENTORY"].count("mho") > 0:
 23          GAME_STATE["INVENTORY"].remove("mho")
 24          GAME_STATE["ENTITY_SPRITES_DICT"][to_coords].remove(entity_obj)
 25          if len(GAME_STATE["ENTITY_SPRITES_DICT"][to_coords]) == 0:
 26              del GAME_STATE["ENTITY_SPRITES_DICT"][to_coords]
 27          if (-1, -1) in GAME_STATE["ENTITY_SPRITES_DICT"]:
 28              GAME_STATE["ENTITY_SPRITES_DICT"][-1, -1].append(entity_obj)
 29          else:
 30              GAME_STATE["ENTITY_SPRITES_DICT"][-1, -1] = [entity_obj]
 31          return True
 32      else:
 33          GAME_STATE["STATE"] = STATE_LOST_SPARKY
 34          return True
 35  
 36  
 37  # Robot before_move. If user has all Hearts they win the map.
 38  def robot_walk(to_coords, from_coords, entity_obj, GAME_STATE):
 39      if GAME_STATE["INVENTORY"].count("heart") == GAME_STATE["TOTAL_HEARTS"]:
 40          GAME_STATE["STATE"] = STATE_MAPWIN
 41          return True
 42  
 43      return False
 44  
 45  
 46  # Remove the item from this location and add it to player inventory.
 47  def take_item(to_coords, from_coords, entity_obj, GAME_STATE):
 48      print(entity_obj)
 49      GAME_STATE["INVENTORY"].append(entity_obj["map_tile_name"])
 50      GAME_STATE["ENTITY_SPRITES_DICT"][to_coords].remove(entity_obj)
 51      if len(GAME_STATE["ENTITY_SPRITES_DICT"][to_coords]) == 0:
 52          del GAME_STATE["ENTITY_SPRITES_DICT"][to_coords]
 53  
 54      if (-1, -1) in GAME_STATE["ENTITY_SPRITES_DICT"]:
 55          GAME_STATE["ENTITY_SPRITES_DICT"][-1, -1].append(entity_obj)
 56      else:
 57          GAME_STATE["ENTITY_SPRITES_DICT"][-1, -1] = [entity_obj]
 58  
 59      return True
 60  
 61  
 62  # main dictionary that maps tile type strings to objects.
 63  # each one stores the sprite_sheet index and any necessary
 64  # behavioral stats like can_walk or before_move
 65  TILES = {
 66      # empty strings default to floor and no walk.
 67      "": {"sprite_index": 10, "can_walk": False},
 68      "floor": {"sprite_index": 10, "can_walk": True},
 69      "top_wall": {"sprite_index": 7, "can_walk": False},
 70      "top_right_wall": {"sprite_index": 8, "can_walk": False},
 71      "top_left_wall": {"sprite_index": 6, "can_walk": False},
 72      "bottom_right_wall": {"sprite_index": 14, "can_walk": False},
 73      "bottom_left_wall": {"sprite_index": 12, "can_walk": False},
 74      "right_wall": {"sprite_index": 11, "can_walk": False},
 75      "left_wall": {"sprite_index": 9, "can_walk": False},
 76      "bottom_wall": {"sprite_index": 13, "can_walk": False},
 77      "robot": {
 78          "sprite_index": 1,
 79          "can_walk": True,
 80          "entity": True,
 81          "before_move": robot_walk,
 82      },
 83      "heart": {
 84          "sprite_index": 5,
 85          "can_walk": True,
 86          "entity": True,
 87          "before_move": take_item,
 88      },
 89      "mho": {
 90          "sprite_index": 2,
 91          "can_walk": True,
 92          "entity": True,
 93          "before_move": take_item,
 94      },
 95      "sparky": {
 96          "sprite_index": 4,
 97          "can_walk": True,
 98          "entity": True,
 99          "before_move": sparky_walk,
100      },
101      "minerva": {
102          "sprite_index": 3,
103          "can_walk": True,
104          "entity": True,
105          "before_move": minerva_walk,
106      },
107      "player": {"sprite_index": 0, "entity": True,},
108  }