Quellcode durchsuchen

Merge branch 'imp/player-transition-during-level-switch'

DricomDragon vor 4 Jahren
Ursprung
Commit
800e23f460
8 geänderte Dateien mit 81 neuen und 66 gelöschten Zeilen
  1. 3 3
      CharTween.gd
  2. 23 22
      Game.gd
  3. 0 1
      Game.tscn
  4. 5 5
      MainCamera.gd
  5. 42 32
      Player.gd
  6. 4 1
      Player.tscn
  7. 1 1
      Spawner.gd
  8. 3 1
      Spawner.tscn

+ 3 - 3
CharTween.gd

@@ -1,6 +1,6 @@
 extends Tween
 
-export var duration = 0.1
+export var default_duration = 0.1
 
 # Called when the node enters the scene tree for the first time.
 func _ready():
@@ -10,10 +10,10 @@ func connect_into(o):
 	connect("tween_completed", o, "_on_tween_completed")
 	pass
 
-func move_char(c, t_pos):
+func move_char(c, t_pos, duration = default_duration):
 	interpolate_property(c, "position", c.get_position(), t_pos, duration, Tween.TRANS_LINEAR, Tween.EASE_OUT_IN)
 	pass
 
-func rotate_char(c, from, to):
+func rotate_char(c, from, to, duration = default_duration):
 	interpolate_property(c, "rotation_degrees", from, to, duration, Tween.TRANS_LINEAR, Tween.EASE_OUT)
 	pass

+ 23 - 22
Game.gd

@@ -1,7 +1,7 @@
 extends Node2D
 
 onready var cam = $MainCamera
-onready var timer = $StartTimer
+onready var start_timer = $StartTimer
 
 # Used to test a level in creation
 export (String) var levelToLoop
@@ -25,28 +25,36 @@ func _ready():
 		if !levelSceneToLoop:
 			push_error("Cant't load level " + levelToLoop)
 
+	# Create players
+	var player
+	for k in 2:
+		player = playerScene.instance()
+		player.grid = levelCurrentNode
+		player.turn_left_action = playerActionsLeft[k]
+		player.turn_right_action = playerActionsRight[k]
+		player.position = Vector2()
+		add_child(player)
+		start_timer.connect("timeout", player, "_on_round_start")
+
 func load_level(name):
 	var fullName = "res://Levels/" + name + ".tscn"
 	return load(fullName)
 
 func _unhandled_input(event):
 	if event.is_action("ui_accept") and event.is_pressed():
-		if get_tree().has_group("living"):
-			var living = get_tree().get_nodes_in_group("living")
-			if !living.empty():
+		if get_tree().has_group("running"):
+			var running = get_tree().get_nodes_in_group("running")
+			if !running.empty():
 				return
 		create_game()
 
 func create_game():
 	# Clear if required
-	if get_tree().has_group("players"):
-		var players = get_tree().get_nodes_in_group("players")
-		for p in players:
-			p.queue_free()
 	if levelCurrentNode:
 		remove_child(levelCurrentNode)
 		levelCurrentNode.queue_free()
 
+	# Create level node
 	if levelToLoop:
 		levelCurrentNode = levelSceneToLoop.instance()
 	else:
@@ -64,24 +72,17 @@ func create_game():
 
 	add_child(levelCurrentNode)
 
-	# Retrieve player spawn points
+	# Allocate spawn points to players
 	var spawners = get_tree().get_nodes_in_group("spawn")
 	spawners.shuffle()
 
-	# Create players
-	var player
+	var p = get_tree().get_nodes_in_group("players")
 
-	for k in 3:
-		player = playerScene.instance()
-		player.grid = levelCurrentNode
-		player.turn_left_action = playerActionsLeft[k]
-		player.turn_right_action = playerActionsRight[k]
-		player.position = spawners[k].position
-		player.rotation = spawners[k].rotation
-		add_child(player)
+	for k in p.size():
+		p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
 
 	# Delay start
-	timer.start()
+	start_timer.start()
 
-func _on_game_start():
-	get_tree().call_group("players", "_on_game_start")
+func _on_round_end():
+	get_tree().call_group("players", "_on_round_end")

+ 0 - 1
Game.tscn

@@ -26,4 +26,3 @@ align = 1
 __meta__ = {
 "_edit_use_anchors_": false
 }
-[connection signal="timeout" from="StartTimer" to="." method="_on_game_start"]

+ 5 - 5
MainCamera.gd

@@ -10,17 +10,17 @@ func move():
 	if !get_tree().has_group("players"):
 		return
 
-	var living
-	if get_tree().has_group("living"):
-		living = get_tree().get_nodes_in_group("living")
+	var running
+	if get_tree().has_group("running"):
+		running = get_tree().get_nodes_in_group("running")
 
-	if !living or living.size() == 0:
+	if !running or running.size() == 0:
 		# Show every players
 		var players = get_tree().get_nodes_in_group("players")
 		center_on(players)
 	else:
 		# Show remaining players only
-		center_on(living)
+		center_on(running)
 
 func center_on(players):
 	var position_accumulator = Vector2(0.0, 0.0)

+ 42 - 32
Player.gd

@@ -5,9 +5,9 @@ var grid:TileMap
 onready var tween = $Tween
 
 # Misc
-var cell_size
-var cell_half_size
-var alive = false
+const cell_size = 64
+const cell_half_size = 32
+var running = false
 
 # Enum
 enum Direction {
@@ -28,33 +28,21 @@ var posiy
 var dirx = 0
 var diry = 0
 
-var dire
+var dire = 0
 var dire_delta = 0
 
 var target_pos
 
+const LEVEL_SPAWN_DURATION = 2
+
 # Controls
 export var turn_left_action:String
 export var turn_right_action:String
 
 func _ready():
-	add_to_group("players")
-	spring()
-
-	assert(grid)
 	assert(turn_left_action)
 	assert(turn_right_action)
 
-	cell_size = int (grid.get_cell_size().x)
-	cell_half_size = cell_size / 2
-
-	# Adjust spawn situation
-	posix = int (position.x / 64)
-	posiy = int (position.y / 64)
-	dire = int((rotation_degrees + 45) / 90)
-
-	apply_turn()
-
 	tween.connect_into(self)
 
 func _unhandled_input(event):
@@ -66,24 +54,45 @@ func _unhandled_input(event):
 			prepare_turn(Side.RIGHT)
 			get_tree().set_input_as_handled()
 
-func _on_game_start():
-	move()
+func spawn(newGrid, newPosition, newOrientation):
+	# Update arena
+	grid = newGrid
+
+	# Adjust spawn situation
+	dire_delta = 0
+	posix = int (newPosition.x / cell_size)
+	posiy = int (newPosition.y / cell_size)
+	dire = int((newOrientation + 45) / 90)
+	apply_turn()
+
+	# Animate spawning
+	tween.rotate_char(self, rotation_degrees, newOrientation, LEVEL_SPAWN_DURATION)
+	tween.move_char(self, newPosition, LEVEL_SPAWN_DURATION)
+	tween.start()
+
+func _on_round_start():
+	assert(grid)
+	spring()
 
 func _on_tween_completed(_o, key):
 	if (key == ":position"):
 		move()
 
 func _on_crash(body):
-	die()
-	generate_wall()
-	# TODO : use enum for blocks
-	grid.set_cell(posix, posiy, 2)
+	if running:
+		die()
+		generate_wall()
+		# TODO : use enum for blocks
+		grid.set_cell(posix, posiy, 2)
 
 func generate_wall():
 	# TODO : use enum for blocks
 	grid.set_cell(posix - dirx, posiy - diry, 1)
 
 func prepare_turn(left_or_right:int):
+	if !running:
+		return
+
 	var current_angle = (dire + dire_delta) * 90
 
 	dire_delta += left_or_right
@@ -125,7 +134,7 @@ func apply_turn():
 		push_error("dire out of range")
 
 func move():
-	if !alive:
+	if !running:
 		return
 
 	generate_wall()
@@ -151,13 +160,14 @@ func has_block_on(left_or_right:int):
 	return grid.get_cell(bposx, bposy) == 1
 
 func die():
-	if alive:
-		remove_from_group("living")
-	alive = false
+	if running:
+		remove_from_group("running")
+	running = false
 
 func spring():
-	alive = true
-	add_to_group("living")
+	running = true
+	add_to_group("running")
+	move()
 
-func is_alive():
-	return alive
+func is_running():
+	return running

+ 4 - 1
Player.tscn

@@ -7,10 +7,13 @@
 [sub_resource type="RectangleShape2D" id=1]
 extents = Vector2( 16, 16 )
 
-[node name="Player" type="Area2D"]
+[node name="Player" type="Area2D" groups=[
+"players",
+]]
 script = ExtResource( 1 )
 
 [node name="Sprite" type="Sprite" parent="."]
+z_index = 1
 texture = ExtResource( 2 )
 __meta__ = {
 "_edit_lock_": true

+ 1 - 1
Spawner.gd

@@ -1,4 +1,4 @@
 extends Sprite
 
 func _ready():
-	add_to_group("spawn")
+	pass

+ 3 - 1
Spawner.tscn

@@ -3,6 +3,8 @@
 [ext_resource path="res://Spawner.gd" type="Script" id=1]
 [ext_resource path="res://Resources/Images/spawn.png" type="Texture" id=2]
 
-[node name="Spawn" type="Sprite"]
+[node name="Spawn" type="Sprite" groups=[
+"spawn",
+]]
 texture = ExtResource( 2 )
 script = ExtResource( 1 )