Skip to content

Commit

Permalink
Started working on fog of war.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfloer committed Nov 10, 2018
1 parent 332f1b0 commit 5e90f69
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions cocos2d.py
Expand Up @@ -220,16 +220,17 @@ def __init__(self, terrain_type=0, sprite_id=None, building=None):
self.sprite_id = sprite_id self.sprite_id = sprite_id
self.building = building self.building = building
self.safe = 0 self.safe = 0
self.visible = 0


def __str__(self): def __str__(self):
return f"Terrain: {self.terrain_type}, id: {self.sprite_id}, building: {self.building}, safe: {self.safe}" return f"Terrain: {self.terrain_type}, id: {self.sprite_id}, building: {self.building}, safe: {self.safe}, visible: {self.visible}"




class Building: class Building:
""" """
A class to store the different buildings in. A class to store the different buildings in.
""" """
_sprite_to_building = {0: "core claimed", 1: "RB", 2: "HR", 3: "protection tower", 4: "network energy test"} _sprite_to_building = {0: "core claimed", 1: "RB", 2: "HR", 3: "protection tower", 4: "sensor tower"}


def __init__(self, building_id): def __init__(self, building_id):
self.building_id = building_id self.building_id = building_id
Expand Down Expand Up @@ -330,6 +331,8 @@ def on_mouse_press(self, x, y, button, dy):
b = Building(3) b = Building(3)
elif self.key is ord('e'): elif self.key is ord('e'):
network_layer.plop_network(h, "energy") network_layer.plop_network(h, "energy")
elif self.key is ord('s'):
b = Building(4)
if b is not None: if b is not None:
building_layer.plop_building(h, b) building_layer.plop_building(h, b)


Expand Down Expand Up @@ -426,6 +429,9 @@ def plop_building(self, cell, building):
if building.building_id == 3 and network_map.network[cell]["powered"]: if building.building_id == 3 and network_map.network[cell]["powered"]:
terrain_map.add_safe_area(cell, 2, 3) terrain_map.add_safe_area(cell, 2, 3)
overlay_layer.draw_safe() overlay_layer.draw_safe()
elif building.building_id == 4 and network_map.network[cell]["powered"]:
fog_layer.add_visible_area(cell, 2, 5)
fog_layer.draw_fog()
self.draw_buildings() self.draw_buildings()
else: else:
print("Building already exists, skipping.") print("Building already exists, skipping.")
Expand All @@ -449,6 +455,56 @@ def remove_building(self, cell):
if building_id == 3 and network_map.network[cell]["powered"]: if building_id == 3 and network_map.network[cell]["powered"]:
terrain_map.add_safe_area(cell, -2, 3) terrain_map.add_safe_area(cell, -2, 3)
overlay_layer.draw_safe() overlay_layer.draw_safe()
elif building_id == 4 and network_map.network[cell]["powered"]:
fog_layer.add_visible_area(cell, -2, 5)
fog_layer.draw_fog()


class FogLayer(ScrollableLayer):
"""
Class to hold the fog of war.
"""
def __init__(self):
super().__init__()
self.fog_batch = BatchNode()
self.fog_batch.position = layout.origin.x, layout.origin.y

def add_visible_area(self, center, visible_type=0, radius=7):
"""
Adds a visible area to terrain hexes. Can also be used to remove visibility, by setting safe_type=0.
Args:
center (Hexagon): center of the area to be made safe.
visible_type (int): 0 for unsafe, 1 for city-core visibility, 2 for other visibility, -2 to remove other visibility.
radius (int): radius of the visible area.
"""
visible_hexes = hex_math.get_hex_chunk(center, radius)
for h in visible_hexes:
if terrain_map.hexagon_map[h].visible == 1:
continue
terrain_map.hexagon_map[h].visible += visible_type

def draw_fog(self):
print("draw fog")
viewport_hexes = scroller.visible_hexes
for k in viewport_hexes:
h = terrain_map.hexagon_map[k]
if h.visible == 0:
pass
position = hex_math.hex_to_pixel(layout, k, False)
anchor = sprite_width / 2, sprite_height / 2
sprite_id = "fog"
sprite = Sprite(sprite_images[sprite_id], position=position, anchor=anchor)
try:
self.fog_batch.add(sprite, z=-k.r, name=f"{k.q}_{k.r}_{k.s}")
except Exception:
pass
else:
try:
self.fog_batch.remove(f"{k.q}_{k.r}_{k.s}")
except Exception:
pass
self.add(self.fog_batch)
print("draw fog done.")




class OverlayLayer(ScrollableLayer): class OverlayLayer(ScrollableLayer):
Expand Down Expand Up @@ -547,6 +603,19 @@ def update_powered(self):
overlay_layer.draw_safe() overlay_layer.draw_safe()
except KeyError: except KeyError:
pass pass
try:
if terrain_map.buildings[n].building_id == 4:
p = 0
# This tile is making the transition from unpowered to powered
if self.network[n]["powered"] and not previous_powered:
p = 2
# This tile was previously powered but isn't anymore.
elif not self.network[n]["powered"] and previous_powered:
p = -2
fog_layer.add_visible_area(n, p, 5)
fog_layer.draw_fog()
except KeyError:
pass




def find_connected(self, cell_source, cell_destination): def find_connected(self, cell_source, cell_destination):
Expand Down Expand Up @@ -744,6 +813,7 @@ def on_key_press(self, key, modifiers):
building_layer.draw_buildings() building_layer.draw_buildings()
overlay_layer.draw_safe() overlay_layer.draw_safe()
network_layer.draw_network() network_layer.draw_network()
fog_layer.draw_fog()
# Generate more terrain chunks. # Generate more terrain chunks.
terrain_map.fill_viewport_chunks() terrain_map.fill_viewport_chunks()


Expand Down Expand Up @@ -810,11 +880,15 @@ def load_images(path):
network_map = Network() network_map = Network()
network_layer = NetworkLayer() network_layer = NetworkLayer()
text_layer = TextOverlay() text_layer = TextOverlay()
fog_layer = FogLayer()
fog_layer.add_visible_area(Hexagon(0, 0, 0), 1, 9)
fog_layer.draw_fog()


scroller.add(terrain_layer, z=0) scroller.add(terrain_layer, z=0)
scroller.add(network_layer, z=1) scroller.add(network_layer, z=1)
scroller.add(building_layer, z=2) scroller.add(building_layer, z=2)
scroller.add(overlay_layer, z=3) scroller.add(fog_layer, z=3)
scroller.add(overlay_layer, z=4)
scroller.add(input_layer, z=5) scroller.add(input_layer, z=5)
building_layer.draw_buildings() building_layer.draw_buildings()
director.window.push_handlers(keyboard) director.window.push_handlers(keyboard)
Expand Down
Binary file added sprites/fog.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/sensor tower off.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/sensor tower on.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5e90f69

Please sign in to comment.