obstacle.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import json
  2. import pygame
  3. from client_local.file_loader.fileLoader import load_json, load_png
  4. OBSTACLE_FRAME_ROOT = "game/frame/obstacle/"
  5. OBSTACLE_IMG_ROOT = "res/img/mapElem/"
  6. obstacles = {}
  7. def get_obstacle(type, name):
  8. obstacles_json = {}
  9. if type not in obstacles.keys():
  10. obstacles[type] = dict()
  11. if name not in obstacles[type].keys():
  12. obstacles[type][name], obstacles_json = load_obstacle(type, name)
  13. if obstacles_json == {}:
  14. _, obstacles_json = load_obstacle(type, name)
  15. return obstacles[type][name], obstacles_json
  16. def load_obstacle(type, name):
  17. an_obstacle = {}
  18. # print(OBSTACLE_FRAME_ROOT + type + '/' + name + ".json")
  19. obstacle_json = load_json(f"{OBSTACLE_FRAME_ROOT}{type}/{name}.json")
  20. an_obstacle["WIDTH"] = obstacle_json["WIDTH"]
  21. an_obstacle["HEIGHT"] = obstacle_json["HEIGHT"]
  22. an_obstacle["BLOCK"] = an_obstacle["BLOCK_FLAME"] = obstacle_json["BLOCK"]
  23. an_obstacle["BREAKABLE"] = obstacle_json["BREAKABLE"]
  24. an_obstacle["CAN_HIDE"] = obstacle_json["CAN_HIDE"]
  25. if "BLOCK_FLAME" in obstacle_json.keys():
  26. an_obstacle["BLOCK_FLAME"] = obstacle_json["BLOCK_FLAME"]
  27. if "SLIDE" in obstacle_json.keys():
  28. an_obstacle["SLIDE"] = obstacle_json["SLIDE"]
  29. if "BLOCK_SIGHT" in obstacle_json.keys():
  30. an_obstacle["BLOCK_SIGHT"] = obstacle_json["BLOCK_SIGHT"]
  31. if "BACKGROUND" in obstacle_json.keys():
  32. an_obstacle["BACKGROUND"] = bool(obstacle_json["BACKGROUND"])
  33. if "CAN_PUSH" in obstacle_json.keys():
  34. an_obstacle["CAN_PUSH"] = bool(obstacle_json["CAN_PUSH"])
  35. if "PUSH_TIME" in obstacle_json.keys():
  36. an_obstacle["PUSH_TIME"] = obstacle_json["PUSH_TIME"]
  37. if "CONTACT" in obstacle_json.keys():
  38. an_obstacle["CONTACT"] = obstacle_json["CONTACT"]
  39. if "SLOW" in obstacle_json.keys():
  40. an_obstacle["SLOW"] = obstacle_json["SLOW"]
  41. if "EXPLODE" in obstacle_json.keys():
  42. an_obstacle["EXPLODE"] = obstacle_json["EXPLODE"]
  43. if "MULTI_TYPE" in obstacle_json.keys():
  44. an_obstacle["MULTI_TYPE"] = obstacle_json["MULTI_TYPE"]
  45. if "TYPE_LEN" in obstacle_json.keys():
  46. an_obstacle["TYPE_LEN"] = obstacle_json["TYPE_LEN"]
  47. an_obstacle["INTERVAL"] = obstacle_json["INTERVAL"]
  48. append_obstacle(obstacle_json, an_obstacle, "STAND")
  49. append_obstacle(obstacle_json, an_obstacle, "PUSH")
  50. append_obstacle(obstacle_json, an_obstacle, "DIE")
  51. append_obstacle(obstacle_json, an_obstacle, "TRIGGER")
  52. return an_obstacle, obstacle_json
  53. def append_obstacle(obstacle_json, an_obstacle, state: str):
  54. if state in obstacle_json:
  55. an_obstacle[state] = list()
  56. frames = obstacle_json[state]
  57. size = len(frames["IMG"])
  58. for i in range(size):
  59. # print(OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + '/' + frames["IMG"][i])
  60. img = load_png(OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + '/' + frames["IMG"][i])
  61. # img = pygame.image.load(OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + '/' + frames["IMG"][i]).convert_alpha()
  62. cx = frames["CX"][i]
  63. cy = frames["CY"][i]
  64. an_obstacle[state].append(Frame(img, cx, cy))
  65. def switch_type(obstacle_js, an_obstacle, current_type):
  66. an_obstacle["STAND"] = list()
  67. frames = obstacle_js["TYPE_LEN"]
  68. img = load_png(OBSTACLE_IMG_ROOT + obstacle_js["TYPE"] + '/' + frames["IMG"][current_type])
  69. # img = pygame.image.load(OBSTACLE_IMG_ROOT + obstacle_js["TYPE"] + '/' + frames["IMG"][current_type]).convert_alpha()
  70. cx = frames["CX"][current_type]
  71. cy = frames["CY"][current_type]
  72. an_obstacle["STAND"].append(Frame(img, cx, cy))
  73. class Frame:
  74. def __init__(self, image: pygame.Surface, cx, cy):
  75. self.image = image
  76. self.cx = cx
  77. self.cy = cy