Browse Source

Update 3D platformer to use AnimationTree

Aaron Franke 5 years ago
parent
commit
486d582802
3 changed files with 74 additions and 89 deletions
  1. 10 17
      3d/platformer/player.gd
  2. 64 72
      3d/platformer/player.tscn
  3. 0 0
      3d/platformer/stage.tscn

+ 10 - 17
3d/platformer/player.gd

@@ -1,15 +1,13 @@
 extends KinematicBody
 
 const ANIM_FLOOR = 0
-const ANIM_AIR_UP = 1
-const ANIM_AIR_DOWN = 2
+const ANIM_AIR = 1
 
 const SHOOT_TIME = 1.5
 const SHOOT_SCALE = 2
 const CHAR_SCALE = Vector3(0.3, 0.3, 0.3)
 const TURN_SPEED = 40
 
-var facing_dir = Vector3.RIGHT
 var movement_dir = Vector3()
 var linear_velocity = Vector3()
 
@@ -27,7 +25,7 @@ var shoot_blend = 0
 onready var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") * ProjectSettings.get_setting("physics/3d/default_gravity_vector")
 
 func _ready():
-	get_node("AnimationTreePlayer").set_active(true)
+	get_node("AnimationTree").set_active(true)
 
 
 func _physics_process(delta):
@@ -41,13 +39,11 @@ func _physics_process(delta):
 	var hdir = hv.normalized() # Horizontal direction.
 	var hspeed = hv.length() # Horizontal speed.
 	
-	# Player input
+	# Player input.
 	var cam_basis = get_node("Target/Camera").get_global_transform().basis
 	var dir = Vector3() # Where does the player intend to walk to.
-	dir = Input.get_action_strength("move_right") * cam_basis[0]
-	dir -= Input.get_action_strength("move_left") * cam_basis[0]
-	dir += Input.get_action_strength("move_backwards") * cam_basis[2]
-	dir -= Input.get_action_strength("move_forward") * cam_basis[2]
+	dir = (Input.get_action_strength("move_right") - Input.get_action_strength("move_left")) * cam_basis[0]
+	dir += (Input.get_action_strength("move_backwards") - Input.get_action_strength("move_forward")) * cam_basis[2]
 	dir.y = 0
 	dir = dir.normalized()
 	
@@ -60,7 +56,6 @@ func _physics_process(delta):
 		if dir.length() > 0.1 and !sharp_turn:
 			if hspeed > 0.001:
 				hdir = adjust_facing(hdir, dir, delta, 1.0 / hspeed * TURN_SPEED, Vector3.UP)
-				facing_dir = hdir
 			else:
 				hdir = dir
 			
@@ -88,10 +83,7 @@ func _physics_process(delta):
 			jumping = true
 			get_node("SoundJump").play()
 	else:
-		if vv > 0:
-			anim = ANIM_AIR_UP
-		else:
-			anim = ANIM_AIR_DOWN
+		anim = ANIM_AIR
 		
 		if dir.length() > 0.1:
 			hv += dir * (accel * 0.2 * delta)
@@ -131,10 +123,11 @@ func _physics_process(delta):
 	prev_shoot = shoot_attempt
 	
 	if is_on_floor():
-		get_node("AnimationTreePlayer").blend2_node_set_amount("walk", hspeed / max_speed)
+		$AnimationTree["parameters/walk/blend_amount"] = hspeed / max_speed
 	
-	get_node("AnimationTreePlayer").transition_node_set_current("state", anim)
-	get_node("AnimationTreePlayer").blend2_node_set_amount("gun", min(shoot_blend, 1.0))
+	$AnimationTree["parameters/state/current"] = anim
+	$AnimationTree["parameters/air_dir/blend_amount"] = clamp(-linear_velocity.y / 4 + 0.5, 0, 1)
+	$AnimationTree["parameters/gun/blend_amount"] = min(shoot_blend, 1.0)
 
 
 func adjust_facing(p_facing, p_target, p_step, p_adjust_rate, current_gn):

+ 64 - 72
3d/platformer/player.tscn

@@ -1,4 +1,4 @@
-[gd_scene load_steps=21 format=2]
+[gd_scene load_steps=32 format=2]
 
 [ext_resource path="res://player.gd" type="Script" id=1]
 [ext_resource path="res://follow_camera.gd" type="Script" id=2]
@@ -1603,6 +1603,61 @@ tracks/26/imported = false
 tracks/26/enabled = true
 tracks/26/keys = PoolRealArray( 0, 1, 5.17831e-08, 0.00292331, 0.204329, 3.25963e-09, -2.17324e-11, 3.00563e-10, 1, 1, 1, 1, 1.25, 1, 5.17831e-08, 0.00292331, 0.204329, 3.25963e-09, -2.17324e-11, 3.00563e-10, 1, 1, 1, 1 )
 
+[sub_resource type="AnimationNodeAnimation" id=15]
+animation = "walk-cycle"
+
+[sub_resource type="AnimationNodeAnimation" id=16]
+animation = "jump-up-cycle"
+
+[sub_resource type="AnimationNodeAnimation" id=17]
+animation = "falling-cycle"
+
+[sub_resource type="AnimationNodeAnimation" id=18]
+animation = "shooting_standing"
+
+[sub_resource type="AnimationNodeAnimation" id=13]
+animation = "idle"
+
+[sub_resource type="AnimationNodeBlend2" id=23]
+
+[sub_resource type="AnimationNodeBlend2" id=19]
+
+[sub_resource type="AnimationNodeTimeScale" id=21]
+
+[sub_resource type="AnimationNodeTransition" id=22]
+input_count = 2
+input_0/name = "on floor"
+input_0/auto_advance = false
+input_1/name = "in air"
+input_1/auto_advance = false
+
+[sub_resource type="AnimationNodeBlend2" id=20]
+
+[sub_resource type="AnimationNodeBlendTree" id=14]
+graph_offset = Vector2( -294.243, -77.71 )
+nodes/Animation/node = SubResource( 13 )
+nodes/Animation/position = Vector2( -260, -280 )
+"nodes/Animation 2/node" = SubResource( 15 )
+"nodes/Animation 2/position" = Vector2( -260, -160 )
+"nodes/Animation 3/node" = SubResource( 16 )
+"nodes/Animation 3/position" = Vector2( -260, -40 )
+"nodes/Animation 4/node" = SubResource( 17 )
+"nodes/Animation 4/position" = Vector2( -260, 80 )
+"nodes/Animation 5/node" = SubResource( 18 )
+"nodes/Animation 5/position" = Vector2( -260, 200 )
+nodes/air_dir/node = SubResource( 23 )
+nodes/air_dir/position = Vector2( 140, 20 )
+nodes/gun/node = SubResource( 19 )
+nodes/gun/position = Vector2( 680, 140 )
+nodes/output/position = Vector2( 880, 140 )
+nodes/scale/node = SubResource( 21 )
+nodes/scale/position = Vector2( 180, -120 )
+nodes/state/node = SubResource( 22 )
+nodes/state/position = Vector2( 440, 0 )
+nodes/walk/node = SubResource( 20 )
+nodes/walk/position = Vector2( -40, -240 )
+node_connections = [ "scale", 0, "walk", "output", 0, "gun", "state", 0, "scale", "state", 1, "air_dir", "gun", 0, "state", "gun", 1, "Animation 5", "walk", 0, "Animation", "walk", 1, "Animation 2", "air_dir", 0, "Animation 3", "air_dir", 1, "Animation 4" ]
+
 [node name="Player" type="KinematicBody"]
 script = ExtResource( 1 )
 __meta__ = {
@@ -1781,77 +1836,14 @@ anims/shooting = SubResource( 10 )
 anims/shooting_standing = SubResource( 11 )
 anims/walk-cycle = SubResource( 12 )
 
-[node name="AnimationTreePlayer" type="AnimationTreePlayer" parent="."]
-master_player = NodePath("../AnimationPlayer")
-data = {
-"active": false,
-"connections": [ "walk", "scale", 0, "scale", "state", 0, "anim 3", "state", 1, "anim 4", "state", 2, "gun", "out", 0, "state", "gun", 0, "anim 5", "gun", 1, "anim", "walk", 0, "anim 2", "walk", 1 ],
-"master": NodePath("../AnimationPlayer"),
-"nodes": [ {
-"id": "scale",
-"position": Vector2( 450, 278 ),
-"scale": 1.2,
-"type": "timescale"
-}, {
-"id": "state",
-"position": Vector2( 610, 374 ),
-"transitions": [ {
-"auto_advance": false
-}, {
-"auto_advance": false
-}, {
-"auto_advance": false
-} ],
-"type": "transition",
-"xfade": 0.1
-}, {
-"filter": [  ],
-"from": "idle",
-"id": "anim",
-"position": Vector2( 44, 94 ),
-"type": "animation"
-}, {
-"id": "out",
-"position": Vector2( 960, 322 ),
-"type": "output"
-}, {
-"filter": [  ],
-"from": "shooting_standing",
-"id": "anim 5",
-"position": Vector2( 47, 528 ),
-"type": "animation"
-}, {
-"filter": [  ],
-"from": "jump-up-cycle",
-"id": "anim 3",
-"position": Vector2( 45, 311 ),
-"type": "animation"
-}, {
-"blend": 1.0,
-"filter": [ NodePath("Armature/Skeleton:r-foot"), NodePath("Armature/Skeleton:r-leg"), NodePath("Armature/Skeleton:waist"), NodePath("Armature/Skeleton:r-ARMCONTROL"), NodePath("Armature/Skeleton:r-ARMORIENT"), NodePath("Armature/Skeleton:r-LEGCONTROL"), NodePath("Armature/Skeleton:r-LEGORIENT"), NodePath("Armature/Skeleton:hip"), NodePath("Armature/Skeleton:l-foot"), NodePath("Armature/Skeleton:l-leg"), NodePath("Armature/Skeleton:neck"), NodePath("Armature/Skeleton:l-ARMCONTROL"), NodePath("Armature/Skeleton:l-ARMORIENT"), NodePath("Armature/Skeleton:l-LEGCONTROL"), NodePath("Armature/Skeleton:l-LEGORIENT"), NodePath("Armature/Skeleton:HEAD"), NodePath("Armature/Skeleton:head"), NodePath("Armature/Skeleton:r-thigh"), NodePath("Armature/Skeleton:MASTER"), NodePath("Armature/Skeleton:l-thigh"), NodePath("Armature"), NodePath("Armature/Skeleton:headtracker") ],
-"id": "gun",
-"position": Vector2( 779, 489 ),
-"type": "blend2"
-}, {
-"filter": [  ],
-"from": "walk-cycle",
-"id": "anim 2",
-"position": Vector2( 44, 202 ),
-"type": "animation"
-}, {
-"filter": [  ],
-"from": "falling-cycle",
-"id": "anim 4",
-"position": Vector2( 46, 419 ),
-"type": "animation"
-}, {
-"blend": 1.0,
-"filter": [  ],
-"id": "walk",
-"position": Vector2( 309, 182 ),
-"type": "blend2"
-} ]
-}
+[node name="AnimationTree" type="AnimationTree" parent="."]
+tree_root = SubResource( 14 )
+anim_player = NodePath("../AnimationPlayer")
+parameters/air_dir/blend_amount = 0.0
+parameters/gun/blend_amount = 0
+parameters/scale/scale = 1.2
+parameters/state/current = 0
+parameters/walk/blend_amount = 0.0
 
 [node name="Forward" type="TouchScreenButton" parent="."]
 position = Vector2( 72, 301 )

File diff suppressed because it is too large
+ 0 - 0
3d/platformer/stage.tscn


Some files were not shown because too many files changed in this diff