Skip to content

Commit

Permalink
Added fade transition for scene switching
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
bdero committed Mar 23, 2016
1 parent 735de8f commit e7810da
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
3 changes: 3 additions & 0 deletions assets/images/black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fade.scn
Binary file not shown.
2 changes: 1 addition & 1 deletion scripts/entities/door.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func _on_Area_body_enter_shape( body_id, body, body_shape, area_shape ):
animations.play("Double Opening", -1)
animations.seek(pos)
elif area_shape == 1: # Loading zone
globals.set_scene("test_level1")
globals.transition_scene("test_level1")
elif area_shape == 2: # Camera zone
take_camera()

Expand Down
19 changes: 19 additions & 0 deletions scripts/fade_resize.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends Sprite

var animations

func fade_in():
animations.play("FadeIn")

func fade_out():
animations.play("FadeIn", 0.5, -3, true)

func _ready():
animations = get_node("animation")
set_process(true)

func _process(delta):
var vp = get_viewport_rect().size
var scale = vp
set_scale(scale*2)
set_pos(scale/2)
44 changes: 41 additions & 3 deletions scripts/globals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ var time = 0
var rings = 0

var camera
var fade

var switch_timer = null
var next_scene_name

var player = null
var character = null
Expand All @@ -15,20 +19,27 @@ var level = null
var paused = false

func _ready():
var main_scene = get_tree().get_root().get_node("main")

camera = ResourceLoader.load("res://camera.scn").instance()
get_tree().get_root().get_node("main").add_child(camera)
main_scene.add_child(camera)

fade = ResourceLoader.load("res://fade.scn").instance()
main_scene.add_child(fade)

set_scene("test_level0")
fade.fade_in()

set_fixed_process(true)

func _fixed_process(delta):
if not paused:
time += delta

if(Input.is_action_pressed("key_1")):
set_scene("test_level0")
transition_scene("test_level0")
elif(Input.is_action_pressed("key_2")):
set_scene("test_level1")
transition_scene("test_level1")

func get_time_str():
# minutes, seconds, and milliseconds
Expand Down Expand Up @@ -56,6 +67,33 @@ func register_player(player_object):
player = player_object
character = player.get_node("character")

func transition_scene(scene):
if switch_timer != null:
return

next_scene_name = scene

switch_timer = Timer.new()
switch_timer.set_wait_time(0.8)
switch_timer.set_one_shot(true)
switch_timer.set_autostart(true)
switch_timer.connect("timeout", self, "_on_switch_timeout")
add_child(switch_timer)

fade.fade_out()

func _on_switch_timeout():
if next_scene_name == null:
print("WARNING: Scene switch timeout called, but there's no scene to switch to.")
return
set_scene(next_scene_name)

fade.fade_in()

switch_timer.queue_free()
switch_timer = null
next_scene_name = null

func set_scene(scene):
# Clean up the current level if it's initialized
if level != null:
Expand Down

0 comments on commit e7810da

Please sign in to comment.