Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

279 lines
8.9KB

  1. from . import gbe
  2. import pygame
  3. class NodeInterface(gbe.nodes.NodeSurface):
  4. def __init__(self, name="Interface", parent=None):
  5. try:
  6. gbe.nodes.NodeSurface.__init__(self, name, parent)
  7. except gbe.nodes.NodeError as e:
  8. raise e
  9. def on_render(self):
  10. size = self.resolution
  11. self.draw_rect((0, 0, size[0], 10), pygame.Color(255,0,0,128), 1)
  12. self.draw_circle((int(size[0]/2), int(size[1]/2)), 16, pygame.Color(255,0,0,255), 2, pygame.Color(0,255,0,255))
  13. class NodeGameMap(gbe.nodes.Node2D):
  14. def __init__(self, name="GameMap", parent=None):
  15. try:
  16. gbe.nodes.Node2D.__init__(self, name, parent)
  17. except gbe.nodes.NodeError as e:
  18. raise e
  19. self._renderMode = 0 # 0 = Top-down | 1 = Perspective
  20. self._layer = {}
  21. self._currentLayer = ""
  22. self._res = {
  23. "environment":"",
  24. "walls":""
  25. }
  26. self._topdown = {
  27. "size":8, # Pixels square
  28. "wall_color":pygame.Color(255, 255, 255),
  29. "blocked_color":pygame.Color(255, 0, 0)
  30. }
  31. self._orientation = "n"
  32. @property
  33. def environment_resource(self):
  34. return self._res["horizon"]
  35. @property
  36. def wall_resource(self):
  37. return self._res["walls"]
  38. @property
  39. def layer_count(self):
  40. return len(self._layer)
  41. @property
  42. def current_layer(self):
  43. return self._currentLayer
  44. @property
  45. def current_layer_width(self):
  46. if self._currentLayer != "":
  47. return self._layer[self._currentLayer]["w"]
  48. return 0
  49. @property
  50. def current_layer_height(self):
  51. if self._currentLayer != "":
  52. return self._layer[self._currentLayer]["h"]
  53. return 0
  54. @property
  55. def layer_names(self):
  56. names = []
  57. for key in self._layer:
  58. names.append(names)
  59. return names
  60. def set_resources(self, environment, walls):
  61. res = self.resource
  62. if environment != self._res["environment"]:
  63. if res.is_valid("graphic", environment):
  64. self._res["environment"] = environment
  65. if walls != self._res["walls"]:
  66. if res.is_valid("graphic", walls):
  67. self._res["walls"] = walls
  68. def add_layer(self, name, w, h):
  69. if name == "" or name in self._layers:
  70. return
  71. self._layer[name] = {
  72. "w":w,
  73. "h":h,
  74. "cells":[]
  75. }
  76. for c in range(0, w*h):
  77. self._layer[name]["cells"][c] = {
  78. "c":0, # Sky / Ceiling
  79. "g":0, # Ground
  80. "n":[-1,False], # North Wall | [<graphic index, -1 = None>, <blocking>]
  81. "s":[-1,False], # South Wall
  82. "e":[-1,False], # East Wall
  83. "w":[-1,False] # West Wall
  84. }
  85. if self._currentLayer == "":
  86. self._currentLayer = name
  87. def set_cell_env(self, x, y, ceiling=-1, ground=-1):
  88. if self._currentLayer == "":
  89. return
  90. layer = self._layer[self._currentLayer]
  91. if x >= 0 and x < layer["w"] and y >= 0 and y < layer["h"]:
  92. index = (y * layer["w"]) + x
  93. cell = layer["cells"]
  94. if ceiling >= 0:
  95. cell[index]["c"] = ceiling
  96. if ground >= 0:
  97. cell[index]["g"] = ground
  98. def fill_cell_env(self, x1, y1, x2, y2, ceiling=-1, ground=-1):
  99. if self._currentLayer == "":
  100. return
  101. for y in range(y1, y2+1):
  102. for x in range(x1, x2+1):
  103. self.set_cell_env(x, y, ceiling, ground)
  104. def set_cell_face(self, x, y, face, gi=-2, blocking=None):
  105. if self._currentLayer == "" or not (face == "n" or face == "s" or face == "w" or face == "e"):
  106. return
  107. layer = self._layer[self._currentLayer]
  108. if x >= 0 and x < layer["w"] and y >= 0 and y < layer["h"]:
  109. index = (y * layer["w"]) + x
  110. cell = layer["cells"]
  111. if gi >= -1:
  112. cell[index][face][0] = gi
  113. if blocking is not None:
  114. cell[index][face][1] = (blocking == True)
  115. else:
  116. if gi == -1: # If gi = -1, there is no wall, so, by default, there is no blocking.
  117. cell[index][face][1] = False
  118. else: # Otherwise, blocking is assumed :)
  119. cell[index][face][1] = True
  120. elif blocking is not None:
  121. blocking = (blocking == True) # Forcing a boolean
  122. cell[index][face][1] = blocking
  123. def turn_left(self):
  124. onum = self._d_s2n(self._orientation)
  125. onum -= 1
  126. if onum < 0:
  127. onum = 3
  128. self._orientation = self._d_n2s(onum)
  129. def turn_right(self):
  130. onum = self._d_s2n(self._orientation)
  131. onum += 1
  132. if onum > 3:
  133. onum = 0
  134. self._orientation = self._d_n2s(onum)
  135. def is_passible(self, x, y, d):
  136. """
  137. Returns true if it's possible to move forward from the x, y map position in the direction given.
  138. d - 0 = North, 1 = East, 2 = South, 3 = West
  139. """
  140. if self._currentLayer == "" or d < 0 or d >= 4:
  141. return False
  142. layer = self._layer[self._currentLayer]
  143. if x >= 0 and x < layer["w"] and y >= 0 and y < layer["h"]:
  144. index = (y * layer["w"]) + x
  145. d = self._d_n2s(d)
  146. return not layer["cells"][index][d][1]
  147. return False
  148. def _d_n2s(self, d): # _(d)irection_(n)umber_to_(s)tring
  149. if d == 0:
  150. return "n"
  151. elif d == 1:
  152. return "e"
  153. elif d == 2:
  154. return "s"
  155. elif d == 3:
  156. return "w"
  157. return ""
  158. def _d_s2n(self, d):
  159. if d == "n":
  160. return 0
  161. elif d == "e":
  162. return 1
  163. elif d == "s":
  164. return 2
  165. elif d == "w":
  166. return 3
  167. return -1
  168. def _indexFromPos(self, x, y):
  169. if x >= 0 and x < self.current_layer_width and y >= 0 and y < self.current_layer_height:
  170. return (y * self.current_layer_width) + x
  171. return -1
  172. def _getCell(self, x, y):
  173. index = self._indexFromPos(x, y)
  174. if index < 0:
  175. return None
  176. return self._layer[self._currentLayer]["cells"][index]
  177. def _RenderTopDown(self):
  178. cell_size = self._topdown["size"]
  179. size = self.resolution
  180. pos = self.position
  181. lsize = (self.current_layer_width, self.current_layer_height)
  182. hcells = int(size[0] / cell_size)
  183. vcells = int(size[1] / cell_size)
  184. cx = pos[0] - int(hcells * 0.5)
  185. cy = pos[1] - int(vcells * 0.5)
  186. ry = -4
  187. for j in range(0, vcells+1):
  188. y = cy + j
  189. if y >= 0 and y < lsize[1]:
  190. rx = -1
  191. for i in range(0, hcells+1):
  192. x = cx + i
  193. if x >= 0 and x < lsize[0]:
  194. cell = self._getCell(x, y)
  195. if cell["n"][0] >= 0:
  196. self.draw_rect((rx, ry, cell_size, 2), self._topdown["wall_color"], 0, self._topdown["wall_color"])
  197. if cell["e"][0] >= 0:
  198. self.draw_rect((rx+(cell_size-2), ry, 2, cell_size), self._topdown["wall_color"], 0, self._topdown["wall_color"])
  199. if cell["s"][0] >= 0:
  200. self.draw_rect((rx, ry+(cell_size-2), cell_size, 2), self._topdown["wall_color"], 0, self._topdown["wall_color"])
  201. if cell["w"][0] >= 0:
  202. self.draw_rect((rx, ry, 2, cell_size), self._topdown["wall_color"], 0, self._topdown["wall_color"])
  203. if cell["n"][1] == True:
  204. self.draw_lines([(rx+1, ry+1), (rx+(cell_size-1), ry+1)], self._topdown["blocked_color"], 1)
  205. if cell["e"][1] == True:
  206. self.draw_lines([(rx+(cell_size-2), ry+1), (rx+(cell_size-2), ry+(cell_size-2))], self._topdown["blocked_color"], 1)
  207. if cell["s"][1] == True:
  208. self.draw_lines([(rx+1, ry+(cell_size-2)), (rx+(cell_size-2), ry+(cell_size-2))], self._topdown["blocked_color"], 1)
  209. if cell["w"][1] == True:
  210. self.draw_lines([(rx+1, ry+1), (rx+1, ry+(cell_size-2))], self._topdown["blocked_color"], 1)
  211. rx += cell_size
  212. ry += cell_size
  213. def _RenderPerspective(self):
  214. pass
  215. def on_render(self):
  216. if self._renderMode == 0:
  217. self._RenderTopDown()
  218. else:
  219. self._RenderPerspective()
  220. class MapEditor(gbe.nodes.Node2D):
  221. def __init__(self, name="MapEditor", parent=None):
  222. try:
  223. gbe.nodes.Node2D.__init__(self, name, parent)
  224. except gbe.nodes.Node2D as e:
  225. raise e
  226. def on_start(self):
  227. pass
  228. def on_pause(self):
  229. pass