From 2db55ef6fc9ce73ecec35d2241d2faa4082f0441 Mon Sep 17 00:00:00 2001 From: fry- Date: Mon, 2 Feb 2015 23:08:57 +0100 Subject: added a demo for 2D fog of war --- demos/2d/fog_of_war/.fscache | 11 +++++ demos/2d/fog_of_war/engine.cfg | 12 ++++++ demos/2d/fog_of_war/floor.png | Bin 0 -> 572 bytes demos/2d/fog_of_war/fog.gd | 86 +++++++++++++++++++++++++++++++++++++ demos/2d/fog_of_war/fog.png | Bin 0 -> 31448 bytes demos/2d/fog_of_war/fog.scn | Bin 0 -> 3714 bytes demos/2d/fog_of_war/fog.xml | 29 +++++++++++++ demos/2d/fog_of_war/icon.png | Bin 0 -> 3639 bytes demos/2d/fog_of_war/icon.png.flags | 1 + demos/2d/fog_of_war/tile_edit.scn | Bin 0 -> 1443 bytes demos/2d/fog_of_war/troll.gd | 43 +++++++++++++++++++ demos/2d/fog_of_war/troll.png | Bin 0 -> 7246 bytes demos/2d/fog_of_war/troll.scn | Bin 0 -> 1839 bytes 13 files changed, 182 insertions(+) create mode 100644 demos/2d/fog_of_war/.fscache create mode 100644 demos/2d/fog_of_war/engine.cfg create mode 100644 demos/2d/fog_of_war/floor.png create mode 100644 demos/2d/fog_of_war/fog.gd create mode 100644 demos/2d/fog_of_war/fog.png create mode 100644 demos/2d/fog_of_war/fog.scn create mode 100644 demos/2d/fog_of_war/fog.xml create mode 100644 demos/2d/fog_of_war/icon.png create mode 100644 demos/2d/fog_of_war/icon.png.flags create mode 100644 demos/2d/fog_of_war/tile_edit.scn create mode 100644 demos/2d/fog_of_war/troll.gd create mode 100644 demos/2d/fog_of_war/troll.png create mode 100644 demos/2d/fog_of_war/troll.scn (limited to 'demos/2d') diff --git a/demos/2d/fog_of_war/.fscache b/demos/2d/fog_of_war/.fscache new file mode 100644 index 0000000000..ba5e3995f3 --- /dev/null +++ b/demos/2d/fog_of_war/.fscache @@ -0,0 +1,11 @@ +::res://::1422910453 +floor.png::ImageTexture::1422910453:: +fog.gd::GDScript::1422910025:: +fog.png::ImageTexture::1422908128:: +fog.scn::PackedScene::1422909435:: +fog.xml::TileSet::1422909324:: +icon.png::ImageTexture::1422811193:: +tile_edit.scn::PackedScene::1422909313:: +troll.gd::GDScript::1422909940:: +troll.png::ImageTexture::1418669358:: +troll.scn::PackedScene::1418669358:: diff --git a/demos/2d/fog_of_war/engine.cfg b/demos/2d/fog_of_war/engine.cfg new file mode 100644 index 0000000000..5c4307b5bc --- /dev/null +++ b/demos/2d/fog_of_war/engine.cfg @@ -0,0 +1,12 @@ +[application] + +name="Fog of War" +main_scene="res://fog.scn" +icon="icon.png" + +[input] + +move_up=[key(Up)] +move_bottom=[key(Down)] +move_left=[key(Left)] +move_right=[key(Right)] diff --git a/demos/2d/fog_of_war/floor.png b/demos/2d/fog_of_war/floor.png new file mode 100644 index 0000000000..07b4f8c98f Binary files /dev/null and b/demos/2d/fog_of_war/floor.png differ diff --git a/demos/2d/fog_of_war/fog.gd b/demos/2d/fog_of_war/fog.gd new file mode 100644 index 0000000000..9da5680e4d --- /dev/null +++ b/demos/2d/fog_of_war/fog.gd @@ -0,0 +1,86 @@ + +extends TileMap + +# member variables here, example: +# var a=2 +# var b="textvar" + +# boundarys for the fog rectangle +var x_min = -20 # left start tile +var x_max = 20 # right end tile +var y_min = -20 # top start tile +var y_max = 20 # bottom end tile + +var position # players position + +# iteration variables +var x +var y + +# variable to check if player moved +var x_old +var y_old + +# array to build up the visible area like a square +# first value determines the width/height of the tip +# here it would be 2*2 + 1 = 5 tiles wide/high +# second value determines the total squares size +# here it would be 5*2 + 1 = 10 tiles wide/high +var l = range(2,5) + +# process that runs in realtime +func _fixed_process(delta): + position = get_node("../troll").get_pos() + + # calculate the corresponding tile + # from the players position + x = int(position.x/get_cell_size().x) + # switching from positive to negative tile positions + # causes problems because of rounding problems + if position.x < 0: + x -= 1 # correct negative values + + y = int(position.y/get_cell_size().y) + if position.y < 0: + y -= 1 + + # check if the player moved one tile further + if (x_old != x) or (y_old != y): + + # create the transparent part (visited area) + var end = l.size()-1 + var start = 0 + for steps in range(l.size()): + for m in range(x-l[end]-1,x+l[end]+2): + for n in range(y-l[start]-1,y+l[start]+2): + if get_cell(m,n) != 0: + set_cell(m,n,1,0,0) + end -= 1 + start += 1 + + # create the actual and active visible part + var end = l.size()-1 + var start = 0 + for steps in range(l.size()): + for m in range(x-l[end],x+l[end]+1): + for n in range(y-l[start],y+l[start]+1): + set_cell(m,n,-1) + end -= 1 + start += 1 + + x_old = x + y_old = y + + pass + +func _ready(): + # Initalization here + + # create a square filled with the 100% opaque fog + for x in range(x_min,x_max): + for y in range(y_min,y_max): + set_cell(x,y,0,0,0) + set_fixed_process(true) + pass + + diff --git a/demos/2d/fog_of_war/fog.png b/demos/2d/fog_of_war/fog.png new file mode 100644 index 0000000000..56980c298d Binary files /dev/null and b/demos/2d/fog_of_war/fog.png differ diff --git a/demos/2d/fog_of_war/fog.scn b/demos/2d/fog_of_war/fog.scn new file mode 100644 index 0000000000..4987f1ead5 Binary files /dev/null and b/demos/2d/fog_of_war/fog.scn differ diff --git a/demos/2d/fog_of_war/fog.xml b/demos/2d/fog_of_war/fog.xml new file mode 100644 index 0000000000..ed08d84a1f --- /dev/null +++ b/demos/2d/fog_of_war/fog.xml @@ -0,0 +1,29 @@ + + + + + + "fog opaque" + + -48, -48 + 0, 0 + 0, 0, 144, 144 + + + "fog transparent" + + -48, -48 + 0, 0 + 144, 0, 144, 144 + + + "floor" + + 0, 0 + 0, 0 + 0, 0, 0, 0 + + + + + \ No newline at end of file diff --git a/demos/2d/fog_of_war/icon.png b/demos/2d/fog_of_war/icon.png new file mode 100644 index 0000000000..0c422e37b0 Binary files /dev/null and b/demos/2d/fog_of_war/icon.png differ diff --git a/demos/2d/fog_of_war/icon.png.flags b/demos/2d/fog_of_war/icon.png.flags new file mode 100644 index 0000000000..dbef2209e8 --- /dev/null +++ b/demos/2d/fog_of_war/icon.png.flags @@ -0,0 +1 @@ +gen_mipmaps=true diff --git a/demos/2d/fog_of_war/tile_edit.scn b/demos/2d/fog_of_war/tile_edit.scn new file mode 100644 index 0000000000..aaca19d370 Binary files /dev/null and b/demos/2d/fog_of_war/tile_edit.scn differ diff --git a/demos/2d/fog_of_war/troll.gd b/demos/2d/fog_of_war/troll.gd new file mode 100644 index 0000000000..d118d3a2ba --- /dev/null +++ b/demos/2d/fog_of_war/troll.gd @@ -0,0 +1,43 @@ + +extends KinematicBody2D + +# This is a simple collision demo showing how +# the kinematic cotroller works. +# move() will allow to move the node, and will +# always move it to a non-colliding spot, +# as long as it starts from a non-colliding spot too. + + +#pixels / second +const MOTION_SPEED=160 + +func _fixed_process(delta): + + var motion = Vector2() + + if (Input.is_action_pressed("move_up")): + motion+=Vector2(0,-1) + if (Input.is_action_pressed("move_bottom")): + motion+=Vector2(0,1) + if (Input.is_action_pressed("move_left")): + motion+=Vector2(-1,0) + if (Input.is_action_pressed("move_right")): + motion+=Vector2(1,0) + + motion = motion.normalized() * MOTION_SPEED * delta + motion = move(motion) + + #make character slide nicely through the world + var slide_attempts = 4 + while(is_colliding() and slide_attempts>0): + motion = get_collision_normal().slide(motion) + motion=move(motion) + slide_attempts-=1 + + +func _ready(): + # Initalization here + set_fixed_process(true) + pass + + diff --git a/demos/2d/fog_of_war/troll.png b/demos/2d/fog_of_war/troll.png new file mode 100644 index 0000000000..69f195d034 Binary files /dev/null and b/demos/2d/fog_of_war/troll.png differ diff --git a/demos/2d/fog_of_war/troll.scn b/demos/2d/fog_of_war/troll.scn new file mode 100644 index 0000000000..f5d87c3631 Binary files /dev/null and b/demos/2d/fog_of_war/troll.scn differ -- cgit v1.2.3 From c21227a6368a7ff26bb1872b803ad7409684da7a Mon Sep 17 00:00:00 2001 From: fry- Date: Mon, 2 Feb 2015 23:29:19 +0100 Subject: added preview picture --- demos/2d/fog_of_war/icon.png | Bin 3639 -> 8681 bytes 1 file changed, 0 insertions(+), 0 deletions(-) (limited to 'demos/2d') diff --git a/demos/2d/fog_of_war/icon.png b/demos/2d/fog_of_war/icon.png index 0c422e37b0..a483390048 100644 Binary files a/demos/2d/fog_of_war/icon.png and b/demos/2d/fog_of_war/icon.png differ -- cgit v1.2.3