Skip to content

Commit

Permalink
Merge pull request #49 from rafaelcastrocouto/main
Browse files Browse the repository at this point in the history
fog - pre shader
  • Loading branch information
rafaelcastrocouto authored Jun 30, 2022
2 parents 9b1e66d + 78d0586 commit da04ad9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 59 deletions.
128 changes: 73 additions & 55 deletions prototype/map/fog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var game:Node
var tile_map_size:int
var trees:TileMap

var clear_skip:int = 10
var clear_skip:int = 16
var clear_frame:int = 0


Expand All @@ -21,40 +21,29 @@ func _ready():

func skip_start():
clear_frame = (clear_frame + 1) % clear_skip
if clear_frame%(clear_skip*2) == 0: cover_map()
if clear_frame % clear_skip == 0 : cover_map()


var quarter = 0

func cover_map():
if quarter == 0:
for y in floor(tile_map_size/2):
for x in floor(tile_map_size/2):
game.map.fog.set_cell(x, y, 0)

if quarter == 1:
for y in range(floor(tile_map_size/2), tile_map_size):
for x in floor(tile_map_size/2):
game.map.fog.set_cell(x, y, 0)

if quarter == 2:
for y in tile_map_size:
for x in range(floor(tile_map_size/2), tile_map_size):
game.map.fog.set_cell(x, y, 0)

if quarter == 3:
for y in tile_map_size:
for x in range(floor(tile_map_size/2), tile_map_size):
game.map.fog.set_cell(x, y, 0)

quarter = (quarter + 1) % 4


for y in floor(tile_map_size):
for x in floor(tile_map_size):
game.map.fog.set_cell(x, y, 0)


# computes and caches 2d arrays with circle as booleans eg:
# 00100
# 01010
# 10001
# 01010
# 00100
var border_sight_mem:Dictionary = {}
var sight_mem:Dictionary = {}

func compute_sight(unit):
func compute_sight(unit, border):
var id = game.unit.modifiers.get_value(unit, "vision")
if id in sight_mem: return sight_mem[id]
if border:
if id in border_sight_mem: return border_sight_mem[id]
else:
if id in sight_mem: return sight_mem[id]
var a = []
if id > 0:
var rad = round(id/cell_size.x)
Expand All @@ -63,41 +52,70 @@ func compute_sight(unit):
for x in range(0, 2*rad):
var r = Vector2(x-rad, y-rad)
var d = r.length()
a[a.size()-1].append(d < rad)
sight_mem[id] = a
if border:
a[a.size()-1].append(d <= rad && d > rad - 1.5)
else:
a[a.size()-1].append(d <= rad)
if border: border_sight_mem[id] = a
else: sight_mem[id] = a
return a


func clear_sigh_skip(unit):
if clear_frame%clear_skip == 0:
clear_sight(unit)
if clear_frame % clear_skip == 0 : clear_sight(unit)


func clear_sight(unit):
var id = game.unit.modifiers.get_value(unit, "vision")
if id > 0:
if unit.team == game.player_team:
var id = game.unit.modifiers.get_value(unit, "vision")
var rad = round(id/cell_size.x)
var pos = world_to_map(unit.global_position)
var a = compute_sight(unit)

for y in a.size():
for x in a[y].size():
if (a[y][x]):
var p = pos - Vector2(rad,rad) + Vector2(x,y)
if (unit.type == "building"):
if id > 0:
#if unit.type != "leader":
var a = compute_sight(unit, false)
for y in a.size():
for x in a[y].size():
if (a[y][x]):
var p = pos - Vector2(rad,rad) + Vector2(x,y)
game.map.fog.set_cellv(p, -1)
else:
if game.map.fog.get_cellv(p) >= 0:
var line = game.unit.follow.path_finder.expandPath([[pos.x, pos.y], [p.x, p.y]])
var blocked = false
for point in line:
var tree = trees.get_cell(point[0]/3, point[1]/3)
if tree > 0:
blocked = true

if not blocked: game.map.fog.set_cellv(p, -1)


# adds tree shadows

# else:
# var a = compute_sight(unit, true)
# for y in a.size():
# for x in a[y].size():
# if (a[y][x]):
# var p = pos - Vector2(rad,rad) + Vector2(x,y)
# var line = game.unit.follow.path_finder.expandPath([[pos.x, pos.y], [p.x, p.y]])
# var blocked = false
# for point in line:
# var point_pos = Vector2(point[0], point[1])
# var tree = trees.get_cell(point[0]/3, point[1]/3)
# if tree > 0: blocked = true
# if not blocked: game.map.fog.set_cellv(point_pos, -1)
#
# adds sight angle limit
#
# if unit has sight angle:
# var la = PI/6
# var a = abs(game.utils.limit_angle(r.angle() - unit.angle))
# if d > rad and (unit.type == "building" or a<la):
# if d > rad and a<la:
# game.map.fog.set_cellv(pos + r, -1)


func hide_unit_skip(unit):
if clear_frame % clear_skip == 0 : hide_unit(unit)


func hide_unit(unit):
if behind_fog(unit) and not unit.type == "building":
unit.modulate = Color.transparent
else:
unit.modulate = Color.white


func behind_fog(unit):
var pos = world_to_map(unit.global_position)
var fog = game.map.fog.get_cellv(pos)
return fog == 0
11 changes: 8 additions & 3 deletions prototype/unit/collision.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ func process(delta):
game.map.blocks.quad.clear()
if game.test.fog: game.map.fog.skip_start()

# loop 1: add to quad
# loop 1
for unit1 in game.all_units:

# add units to quad
if unit1.collide and not unit1.dead:
game.map.blocks.quad.add_body(unit1)

if game.test.fog:
if unit1.team == game.player_team: game.map.fog.clear_sigh_skip(unit1)
# clear fog if unit has sight
if game.test.fog: game.map.fog.clear_sigh_skip(unit1)


# loop 2: checks for collisions

for unit1 in game.all_units:

# hide units behind fog
if game.test.fog: game.map.fog.hide_unit_skip(unit1)

# projectiles collision

if unit1.projectiles.size():
Expand Down
2 changes: 2 additions & 0 deletions prototype/unit/unit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var channeling_timer:Timer

# NODES
var hud:Node
var sprites:Node
var spawn:Node
var move:Node
var attack:Node
Expand All @@ -109,6 +110,7 @@ func _ready():
if has_node("behavior/skills"): skills = get_node("behavior/skills")
if has_node("behavior/modifiers"): modifiers = get_node("behavior/modifiers")

if has_node("sprites"): sprites = get_node("sprites")
if has_node("sprites/weapon"): weapon = get_node("sprites/weapon")
if has_node("sprites/weapon/projectile"): projectile = get_node("sprites/weapon/projectile")

Expand Down
2 changes: 1 addition & 1 deletion prototype/utils/test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var game:Node

var unit = 0
var stress = 0
var fog = 0
var fog = 1


func _ready():
Expand Down

0 comments on commit da04ad9

Please sign in to comment.