fog.gd 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. extends TileMap
  2. # member variables here, example:
  3. # var a=2
  4. # var b="textvar"
  5. # boundarys for the fog rectangle
  6. var x_min = -20 # left start tile
  7. var x_max = 20 # right end tile
  8. var y_min = -20 # top start tile
  9. var y_max = 20 # bottom end tile
  10. var position # players position
  11. # iteration variables
  12. var x
  13. var y
  14. # variable to check if player moved
  15. var x_old
  16. var y_old
  17. # array to build up the visible area like a square
  18. # first value determines the width/height of the tip
  19. # here it would be 2*2 + 1 = 5 tiles wide/high
  20. # second value determines the total squares size
  21. # here it would be 5*2 + 1 = 10 tiles wide/high
  22. var l = range(2,5)
  23. # process that runs in realtime
  24. func _fixed_process(delta):
  25. position = get_node("../troll").get_pos()
  26. # calculate the corresponding tile
  27. # from the players position
  28. x = int(position.x/get_cell_size().x)
  29. # switching from positive to negative tile positions
  30. # causes problems because of rounding problems
  31. if position.x < 0:
  32. x -= 1 # correct negative values
  33. y = int(position.y/get_cell_size().y)
  34. if position.y < 0:
  35. y -= 1
  36. # check if the player moved one tile further
  37. if (x_old != x) or (y_old != y):
  38. # create the transparent part (visited area)
  39. var end = l.size()-1
  40. var start = 0
  41. for steps in range(l.size()):
  42. for m in range(x-l[end]-1,x+l[end]+2):
  43. for n in range(y-l[start]-1,y+l[start]+2):
  44. if get_cell(m,n) != 0:
  45. set_cell(m,n,1,0,0)
  46. end -= 1
  47. start += 1
  48. # create the actual and active visible part
  49. var end = l.size()-1
  50. var start = 0
  51. for steps in range(l.size()):
  52. for m in range(x-l[end],x+l[end]+1):
  53. for n in range(y-l[start],y+l[start]+1):
  54. set_cell(m,n,-1)
  55. end -= 1
  56. start += 1
  57. x_old = x
  58. y_old = y
  59. pass
  60. func _ready():
  61. # Initalization here
  62. # create a square filled with the 100% opaque fog
  63. for x in range(x_min,x_max):
  64. for y in range(y_min,y_max):
  65. set_cell(x,y,0,0,0)
  66. set_fixed_process(true)
  67. pass