Replay
This commit is contained in:
		
							parent
							
								
									ed6def77f5
								
							
						
					
					
						commit
						7b5d84e662
					
				
					 9 changed files with 131 additions and 4 deletions
				
			
		| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
[gd_scene load_steps=15 format=2]
 | 
					[gd_scene load_steps=17 format=2]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[ext_resource path="res://Tile Sets/SnowySlopes.tres" type="TileSet" id=1]
 | 
					[ext_resource path="res://Tile Sets/SnowySlopes.tres" type="TileSet" id=1]
 | 
				
			||||||
[ext_resource path="res://Units/DownhillAutoscrollerPlayer.tscn" type="PackedScene" id=2]
 | 
					[ext_resource path="res://Units/DownhillAutoscrollerPlayer.tscn" type="PackedScene" id=2]
 | 
				
			||||||
| 
						 | 
					@ -11,6 +11,8 @@
 | 
				
			||||||
[ext_resource path="res://Sounds/sfx_jump_07-80241.mp3" type="AudioStream" id=9]
 | 
					[ext_resource path="res://Sounds/sfx_jump_07-80241.mp3" type="AudioStream" id=9]
 | 
				
			||||||
[ext_resource path="res://Sounds/land2-43790.mp3" type="AudioStream" id=10]
 | 
					[ext_resource path="res://Sounds/land2-43790.mp3" type="AudioStream" id=10]
 | 
				
			||||||
[ext_resource path="res://Sounds/ski-67717.wav" type="AudioStream" id=11]
 | 
					[ext_resource path="res://Sounds/ski-67717.wav" type="AudioStream" id=11]
 | 
				
			||||||
 | 
					[ext_resource path="res://Scripts/Units/FollowReplay.gd" type="Script" id=12]
 | 
				
			||||||
 | 
					[ext_resource path="res://Scripts/PlayerRecorder.gd" type="Script" id=13]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[sub_resource type="Animation" id=3]
 | 
					[sub_resource type="Animation" id=3]
 | 
				
			||||||
resource_name = "InitialFade"
 | 
					resource_name = "InitialFade"
 | 
				
			||||||
| 
						 | 
					@ -114,11 +116,16 @@ position = Vector2( 91, -197 )
 | 
				
			||||||
[node name="Camera2D" type="Camera2D" parent="Player"]
 | 
					[node name="Camera2D" type="Camera2D" parent="Player"]
 | 
				
			||||||
offset = Vector2( 0, -15 )
 | 
					offset = Vector2( 0, -15 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[node name="PlayerRecorder" type="Node" parent="Player"]
 | 
				
			||||||
 | 
					script = ExtResource( 13 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[node name="SpectatorCam" type="Camera2D" parent="."]
 | 
					[node name="SpectatorCam" type="Camera2D" parent="."]
 | 
				
			||||||
offset = Vector2( 0, -15 )
 | 
					offset = Vector2( 0, -15 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[node name="Rival" parent="." instance=ExtResource( 6 )]
 | 
					[node name="Rival" parent="." instance=ExtResource( 6 )]
 | 
				
			||||||
position = Vector2( 91, -197 )
 | 
					position = Vector2( 91, -197 )
 | 
				
			||||||
 | 
					script = ExtResource( 12 )
 | 
				
			||||||
 | 
					unit_type = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[node name="CanvasLayer" type="CanvasLayer" parent="."]
 | 
					[node name="CanvasLayer" type="CanvasLayer" parent="."]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										25
									
								
								Scripts/PlayerRecorder.gd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								Scripts/PlayerRecorder.gd
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,25 @@
 | 
				
			||||||
 | 
					extends Node
 | 
				
			||||||
 | 
					class_name PlayerRecorder
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var replay: ReplayRecording
 | 
				
			||||||
 | 
					var this_frame: bool = true
 | 
				
			||||||
 | 
					var sprite: int = 0
 | 
				
			||||||
 | 
					var player: DownhillAutoscrollerPlayer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export var save_to: String
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func _ready():
 | 
				
			||||||
 | 
						replay = ReplayRecording.new()
 | 
				
			||||||
 | 
						replay.time_per_frame = 1.0 / 30.0
 | 
				
			||||||
 | 
						player = get_parent()
 | 
				
			||||||
 | 
						player.recorder = self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func _process(delta):
 | 
				
			||||||
 | 
						if this_frame:
 | 
				
			||||||
 | 
							this_frame = false
 | 
				
			||||||
 | 
							replay.positions.append(player.pos)
 | 
				
			||||||
 | 
							replay.sprites.append(sprite)
 | 
				
			||||||
 | 
							replay.frames += 1
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						else:
 | 
				
			||||||
 | 
							this_frame = true
 | 
				
			||||||
							
								
								
									
										53
									
								
								Scripts/ReplayRecording.gd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								Scripts/ReplayRecording.gd
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,53 @@
 | 
				
			||||||
 | 
					extends Object
 | 
				
			||||||
 | 
					class_name ReplayRecording
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var time_per_frame: float
 | 
				
			||||||
 | 
					var frames: int = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var positions: Array
 | 
				
			||||||
 | 
					var sprites: Array
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func load_from(file: File):
 | 
				
			||||||
 | 
						time_per_frame = file.get_real()
 | 
				
			||||||
 | 
						frames = file.get_32()
 | 
				
			||||||
 | 
						positions = []
 | 
				
			||||||
 | 
						sprites = []
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						for i in range(frames):
 | 
				
			||||||
 | 
							var x: float = file.get_real()
 | 
				
			||||||
 | 
							var y: float = file.get_real()
 | 
				
			||||||
 | 
							positions.append(Vector2(x, y))
 | 
				
			||||||
 | 
							sprites.append(file.get_8())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func load_from_uri(uri: String):
 | 
				
			||||||
 | 
						var file: File = File.new()
 | 
				
			||||||
 | 
						file.open(uri, File.READ)
 | 
				
			||||||
 | 
						load_from(file)
 | 
				
			||||||
 | 
						file.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func save_to(file: File):
 | 
				
			||||||
 | 
						file.store_real(time_per_frame)
 | 
				
			||||||
 | 
						file.store_32(frames)
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						for i in range(frames):
 | 
				
			||||||
 | 
							file.store_real(positions[i].x)
 | 
				
			||||||
 | 
							file.store_real(positions[i].y)
 | 
				
			||||||
 | 
							file.store_8(sprites[i])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func save_to_uri(uri: String):
 | 
				
			||||||
 | 
						var file: File = File.new()
 | 
				
			||||||
 | 
						file.open(uri, File.WRITE)
 | 
				
			||||||
 | 
						save_to(file)
 | 
				
			||||||
 | 
						file.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func position(frame: int) -> Vector2:
 | 
				
			||||||
 | 
						if frame < frames:
 | 
				
			||||||
 | 
							return positions[frame]
 | 
				
			||||||
 | 
						else:
 | 
				
			||||||
 | 
							return positions[-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func sprite(frame: int) -> int:
 | 
				
			||||||
 | 
						if frame < frames:
 | 
				
			||||||
 | 
							return sprites[frame]
 | 
				
			||||||
 | 
						else:
 | 
				
			||||||
 | 
							return sprites[-1]
 | 
				
			||||||
| 
						 | 
					@ -35,6 +35,8 @@ var time_elapsed : float
 | 
				
			||||||
var is_flash : bool = false
 | 
					var is_flash : bool = false
 | 
				
			||||||
var flash_start_timestamp : float
 | 
					var flash_start_timestamp : float
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var recorder: PlayerRecorder = null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Called when the node enters the scene tree for the first time
 | 
					# Called when the node enters the scene tree for the first time
 | 
				
			||||||
func _ready():
 | 
					func _ready():
 | 
				
			||||||
	for action_num in Constants.UNIT_TYPE_ACTIONS[unit_type]:
 | 
						for action_num in Constants.UNIT_TYPE_ACTIONS[unit_type]:
 | 
				
			||||||
| 
						 | 
					@ -243,6 +245,10 @@ func set_sprite(sprite_class : int, index : int = 0):
 | 
				
			||||||
	var true_index : int = index
 | 
						var true_index : int = index
 | 
				
			||||||
	if true_index > len(node_list) - 1:
 | 
						if true_index > len(node_list) - 1:
 | 
				
			||||||
		true_index = 0
 | 
							true_index = 0
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						if recorder != null:
 | 
				
			||||||
 | 
							recorder.sprite = (index << 3) | sprite_class
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	var new_sprite : Node2D = node_list[true_index]
 | 
						var new_sprite : Node2D = node_list[true_index]
 | 
				
			||||||
	if (is_flash):
 | 
						if (is_flash):
 | 
				
			||||||
		if int((time_elapsed - flash_start_timestamp) / Constants.FLASH_CYCLE) % 2 == 1:
 | 
							if int((time_elapsed - flash_start_timestamp) / Constants.FLASH_CYCLE) % 2 == 1:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,7 +19,6 @@ var jump_audiostream_player : AudioStreamPlayer
 | 
				
			||||||
var land_audiostream_player : AudioStreamPlayer
 | 
					var land_audiostream_player : AudioStreamPlayer
 | 
				
			||||||
var snow_audiostream_player : AudioStreamPlayer
 | 
					var snow_audiostream_player : AudioStreamPlayer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
func init_unit_w_scene(scene):
 | 
					func init_unit_w_scene(scene):
 | 
				
			||||||
	.init_unit_w_scene(scene)
 | 
						.init_unit_w_scene(scene)
 | 
				
			||||||
	hit_audiostream_player = scene.get_node("HitAudioStreamPlayer")
 | 
						hit_audiostream_player = scene.get_node("HitAudioStreamPlayer")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										21
									
								
								Scripts/Units/FollowReplay.gd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Scripts/Units/FollowReplay.gd
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,21 @@
 | 
				
			||||||
 | 
					extends Unit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var replay : ReplayRecording = null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func process_unit(delta : float, time_elapsed : float):
 | 
				
			||||||
 | 
						self.time_elapsed = time_elapsed
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						if replay != null:
 | 
				
			||||||
 | 
							handle_replay(time_elapsed)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func handle_replay(at_time: float):
 | 
				
			||||||
 | 
						var frame: int = int(at_time / replay.time_per_frame)
 | 
				
			||||||
 | 
						var subframe: float = (at_time / replay.time_per_frame) - frame
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						pos = lerp(replay.position(frame), replay.position(frame + 1), subframe)
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						var sprite: int = replay.sprite(frame)
 | 
				
			||||||
 | 
						var index: int = sprite >> 3
 | 
				
			||||||
 | 
						sprite &= 7
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						set_sprite(sprite, index)
 | 
				
			||||||
| 
						 | 
					@ -17,5 +17,7 @@ force/max_rate=false
 | 
				
			||||||
force/max_rate_hz=44100
 | 
					force/max_rate_hz=44100
 | 
				
			||||||
edit/trim=false
 | 
					edit/trim=false
 | 
				
			||||||
edit/normalize=false
 | 
					edit/normalize=false
 | 
				
			||||||
edit/loop=false
 | 
					edit/loop_mode=0
 | 
				
			||||||
 | 
					edit/loop_begin=0
 | 
				
			||||||
 | 
					edit/loop_end=-1
 | 
				
			||||||
compress/mode=0
 | 
					compress/mode=0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,5 +17,7 @@ force/max_rate=false
 | 
				
			||||||
force/max_rate_hz=44100
 | 
					force/max_rate_hz=44100
 | 
				
			||||||
edit/trim=false
 | 
					edit/trim=false
 | 
				
			||||||
edit/normalize=false
 | 
					edit/normalize=false
 | 
				
			||||||
edit/loop=false
 | 
					edit/loop_mode=0
 | 
				
			||||||
 | 
					edit/loop_begin=0
 | 
				
			||||||
 | 
					edit/loop_end=-1
 | 
				
			||||||
compress/mode=0
 | 
					compress/mode=0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -34,6 +34,16 @@ _global_script_classes=[ {
 | 
				
			||||||
"language": "GDScript",
 | 
					"language": "GDScript",
 | 
				
			||||||
"path": "res://Scripts/Units/Player.gd"
 | 
					"path": "res://Scripts/Units/Player.gd"
 | 
				
			||||||
}, {
 | 
					}, {
 | 
				
			||||||
 | 
					"base": "Node",
 | 
				
			||||||
 | 
					"class": "PlayerRecorder",
 | 
				
			||||||
 | 
					"language": "GDScript",
 | 
				
			||||||
 | 
					"path": "res://Scripts/PlayerRecorder.gd"
 | 
				
			||||||
 | 
					}, {
 | 
				
			||||||
 | 
					"base": "Object",
 | 
				
			||||||
 | 
					"class": "ReplayRecording",
 | 
				
			||||||
 | 
					"language": "GDScript",
 | 
				
			||||||
 | 
					"path": "res://Scripts/ReplayRecording.gd"
 | 
				
			||||||
 | 
					}, {
 | 
				
			||||||
"base": "Area2D",
 | 
					"base": "Area2D",
 | 
				
			||||||
"class": "Unit",
 | 
					"class": "Unit",
 | 
				
			||||||
"language": "GDScript",
 | 
					"language": "GDScript",
 | 
				
			||||||
| 
						 | 
					@ -45,6 +55,8 @@ _global_script_class_icons={
 | 
				
			||||||
"NPCExample": "",
 | 
					"NPCExample": "",
 | 
				
			||||||
"NPCUnit": "",
 | 
					"NPCUnit": "",
 | 
				
			||||||
"Player": "",
 | 
					"Player": "",
 | 
				
			||||||
 | 
					"PlayerRecorder": "",
 | 
				
			||||||
 | 
					"ReplayRecording": "",
 | 
				
			||||||
"Unit": ""
 | 
					"Unit": ""
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue