Player movement and animation

This commit is contained in:
Felipe M 2021-01-29 17:09:52 +01:00
parent f456725cf2
commit 0dd3f24030
Signed by: fmartingr
GPG key ID: 716BC147715E716F
9 changed files with 700 additions and 38 deletions

View file

@ -1,5 +1,7 @@
extends KinematicBody2D
const DustEffect = preload("res://DustEffect.tscn")
export (int) var acceleration = 512
export (int) var max_speed = 64
export (float) var friction = 0.25
@ -9,11 +11,14 @@ export (int) var max_slope = 46
var motion = Vector2.ZERO
var snap_vector = Vector2.ZERO
var just_jumped= false
onready var sprite = $Sprite
onready var animation = $Animation
onready var coyoteJumpTimer = $CoyoteJumpTimer
func _physics_process(delta):
just_jumped = false
var input_vector = get_input_vector()
apply_horizontal_force(input_vector, delta)
apply_friction(input_vector)
@ -23,6 +28,13 @@ func _physics_process(delta):
update_animations(input_vector)
move()
func create_dust_effect():
var dustPosition = global_position
dustPosition.x += rand_range(-4, 4)
var dustEffect = DustEffect.instance()
dustEffect.global_position = dustPosition
get_tree().current_scene.add_child(dustEffect)
func get_input_vector():
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
@ -42,17 +54,19 @@ func update_snap_vector():
snap_vector = Vector2.DOWN
func jump_check():
if is_on_floor():
if is_on_floor() or coyoteJumpTimer.time_left > 0:
if Input.is_action_just_pressed("ui_select"):
motion.y = -jump_force
snap_vector = Vector2.ZERO
just_jumped = true
else:
if Input.is_action_just_released("ui_select"):# and motion.y < -jump_force/2:
if Input.is_action_just_released("ui_select") and motion.y < -jump_force/2:
motion.y = motion.y/2
func apply_gravity(delta):
motion.y += gravity * delta
motion.y = min(motion.y, jump_force)
if not is_on_floor():
motion.y += gravity * delta
motion.y = min(motion.y, jump_force)
func update_animations(input_vector):
if input_vector.x != 0:
@ -66,5 +80,28 @@ func update_animations(input_vector):
animation.play("Jump")
func move():
var was_on_air = not is_on_floor()
var was_on_floor = is_on_floor()
var last_position = position
var last_motion = motion
motion = move_and_slide_with_snap(motion, snap_vector * 4, Vector2.UP, true, 4, deg2rad(max_slope), false)
# Just landed
if was_on_air and is_on_floor():
# Keep previous momentum when landing on slopes
motion.x = last_motion.x
create_dust_effect()
# Just left ground
if was_on_floor and not is_on_floor() and not just_jumped:
# Fixes "gap" when jumping off a cliff [TODO]
motion.y = 0
position.y = last_position.y
coyoteJumpTimer.start()
# Prevent Sliding (hack) [NOT WORKING]
# If we are on floor, the floor isn't moving and our motion is really tiny
if is_on_floor() and get_floor_velocity().length() == 0 and abs(motion.x) < 1:
position.x = last_position.x

View file

@ -56,6 +56,23 @@ tracks/0/keys = {
"update": 1,
"values": [ 4, 5, 6, 7, 8, 9, 7 ]
}
tracks/1/type = "method"
tracks/1/path = NodePath(".")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/keys = {
"times": PoolRealArray( 0.3, 0.6 ),
"transitions": PoolRealArray( 1, 1 ),
"values": [ {
"args": [ ],
"method": "create_dust_effect"
}, {
"args": [ ],
"method": "create_dust_effect"
} ]
}
[node name="Player" type="KinematicBody2D"]
script = ExtResource( 2 )
@ -64,6 +81,7 @@ script = ExtResource( 2 )
position = Vector2( 0, -12 )
texture = ExtResource( 1 )
hframes = 12
frame = 4
[node name="Collision" type="CollisionShape2D" parent="."]
position = Vector2( 0, -7 )
@ -81,3 +99,7 @@ visible = false
position = Vector2( 0, -8 )
update_rotation = false
update_scale = false
[node name="CoyoteJumpTimer" type="Timer" parent="."]
wait_time = 0.2
one_shot = true

View file

@ -0,0 +1,11 @@
extends Node
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
VisualServer.set_default_clear_color(Color.black)

View file

@ -1,36 +1,11 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]
[ext_resource path="res://Scenes/Brick.tscn" type="PackedScene" id=1]
[ext_resource path="res://TileMap.tscn" type="PackedScene" id=1]
[ext_resource path="res://Scenes/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://Scenes/World.gd" type="Script" id=3]
[node name="World" type="Node"]
[node name="Brick" parent="." instance=ExtResource( 1 )]
position = Vector2( 0, 160 )
[node name="Brick2" parent="." instance=ExtResource( 1 )]
position = Vector2( 16, 160 )
[node name="Brick3" parent="." instance=ExtResource( 1 )]
position = Vector2( 32, 160 )
[node name="Brick4" parent="." instance=ExtResource( 1 )]
position = Vector2( 48, 160 )
[node name="Brick5" parent="." instance=ExtResource( 1 )]
position = Vector2( 64, 160 )
[node name="Brick6" parent="." instance=ExtResource( 1 )]
position = Vector2( 80, 160 )
[node name="Brick7" parent="." instance=ExtResource( 1 )]
position = Vector2( 96, 160 )
[node name="Brick8" parent="." instance=ExtResource( 1 )]
position = Vector2( 48, 144 )
[node name="Brick9" parent="." instance=ExtResource( 1 )]
position = Vector2( 64, 144 )
script = ExtResource( 3 )
[node name="Player" parent="." instance=ExtResource( 2 )]
position = Vector2( 32, 96 )
@ -43,10 +18,7 @@ position = Vector2( 32, 88 )
current = true
smoothing_enabled = true
[node name="Terrain" type="StaticBody2D" parent="."]
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Terrain"]
position = Vector2( -16, -32 )
polygon = PoolVector2Array( 0, 112, 0, 144, 48, 144, 64, 128, 96, 128, 128, 96, 160, 96, 160, 112, 224, 112, 224, 80, 240, 112, 160, 176, -32, 176, -32, 112 )
[node name="TileMap" parent="." instance=ExtResource( 1 )]
tile_data = PoolIntArray( 65536, 0, 4, 65537, 0, 196609, 65538, 0, 196609, 65539, 0, 196609, 65540, 0, 196609, 65541, 0, 6, 65542, 0, 5, 65543, 0, 196609, 65544, 0, 196609, 65545, 0, 196609, 65546, 0, 6, 65547, 0, 2, 196607, 0, 4, 131072, 0, 196615, 131077, 0, 131072, 131078, 0, 131074, 131082, 0, 65536, 131083, 0, 131077, 131084, 0, 1, 131085, 0, 5, 131086, 0, 196609, 131087, 0, 196609, 131088, 0, 196609, 131089, 0, 196609, 131090, 0, 196609, 131091, 0, 7, 262143, 0, 65539, 196618, 0, 131072, 196619, 0, 131073, 196620, 0, 131073, 196621, 0, 131074, 196627, 0, 196612, 196628, 0, 7, 327679, 0, 65539, 262164, 0, 65539, 393215, 0, 65539, 327700, 0, 65539, 458751, 0, 65539, 393236, 0, 65539, 524287, 0, 65539, 458762, 1, 3, 458763, 1, 4, 458764, 0, 1, 458765, 0, 2, 458771, 0, 196608, 458772, 0, 262151, 589823, 0, 65539, 524294, 1, 0, 524295, 0, 1, 524296, 0, 1, 524297, 0, 1, 524298, 0, 131078, 524299, 0, 65537, 524300, 0, 65537, 524301, 0, 65538, 524308, 0, 65539, 655359, 0, 131076, 589824, 0, 2, 589829, 1, 0, 589830, 1, 1, 589831, 0, 65537, 589832, 0, 65537, 589833, 0, 65537, 589834, 0, 65537, 589835, 0, 65537, 589836, 0, 65537, 589837, 0, 131077, 589838, 0, 1, 589839, 0, 1, 589840, 0, 1, 589841, 0, 2, 589844, 0, 65539, 720895, 0, 65536, 655360, 0, 131077, 655361, 0, 1, 655362, 0, 1, 655363, 0, 1, 655364, 0, 1, 655365, 1, 1, 655366, 0, 65537, 655367, 0, 65537, 655368, 0, 65541, 655369, 0, 131073, 655370, 0, 131073, 655371, 0, 131073, 655372, 0, 65542, 655373, 0, 65537, 655374, 0, 65537, 655375, 0, 65537, 655376, 0, 65537, 655377, 0, 262149, 655378, 0, 196609, 655379, 0, 196609, 655380, 0, 196615, 786431, 0, 131072, 720896, 0, 65542, 720897, 0, 65537, 720898, 0, 65537, 720899, 0, 65537, 720900, 0, 65537, 720901, 0, 65537, 720902, 0, 65537, 720903, 0, 65541, 720904, 0, 131074, 720908, 0, 131072, 720909, 0, 65542, 720910, 0, 65537, 720911, 0, 65537, 720912, 0, 65541, 720913, 0, 131074, 786432, 0, 65536, 786433, 0, 65537, 786434, 0, 65537, 786435, 0, 65537, 786436, 0, 65537, 786437, 0, 65537, 786438, 0, 65541, 786439, 0, 131074, 786445, 0, 131072, 786446, 0, 131073, 786447, 0, 131073, 786448, 0, 131074, 851968, 0, 131072, 851969, 0, 65542, 851970, 0, 65537, 851971, 0, 65537, 851972, 0, 65541, 851973, 0, 131073, 851974, 0, 131074, 917505, 0, 131072, 917506, 0, 131073, 917507, 0, 131073, 917508, 0, 131074 )
[editable path="Player"]