From 3518260b5f0c883c76d6aa795f62b9fe19ab1fd0 Mon Sep 17 00:00:00 2001 From: nomaxg Date: Sat, 12 Jun 2021 19:42:19 -0400 Subject: [PATCH 1/4] added basic wall jumping --- entities/actor/actor.gd | 3 +- entities/actor/player.gd | 48 ++++++- entities/actor/player.tscn | 14 ++ entities/wall/wall.tscn | 16 +++ scenes/wall_jump.tscn | 278 +++++++++++++++++++++++++++++++++++++ 5 files changed, 352 insertions(+), 7 deletions(-) create mode 100644 entities/wall/wall.tscn create mode 100644 scenes/wall_jump.tscn diff --git a/entities/actor/actor.gd b/entities/actor/actor.gd index 3dd043f..aa3adc1 100644 --- a/entities/actor/actor.gd +++ b/entities/actor/actor.gd @@ -6,7 +6,8 @@ extends KinematicBody2D export var speed = Vector2(300.0, 700.0) -onready var gravity = ProjectSettings.get("physics/2d/default_gravity") +onready var default_gravity = ProjectSettings.get("physics/2d/default_gravity") +onready var gravity = default_gravity const FLOOR_NORMAL = Vector2.UP diff --git a/entities/actor/player.gd b/entities/actor/player.gd index 80e40c6..5152a82 100644 --- a/entities/actor/player.gd +++ b/entities/actor/player.gd @@ -5,6 +5,8 @@ extends Actor # var b = "text" const COYOTE_TIME = 0.1 +const WALL_IMPULSE = 3000 +const WALL_FRICTION = 0.1 onready var FLOOR_DETECT_DISTANCE = $PlatformDetector.cast_to.y @@ -12,11 +14,19 @@ onready var platform_detector = $PlatformDetector onready var animation_player = $AnimationPlayer onready var sprite = $SpriteContainer/Sprite onready var bounce_detector = $BounceDetector +onready var left_wall_cast = $LeftWallCast +onready var right_wall_cast = $RightWallCast var queue_jump = false var is_jumping = false +var just_wall_jumped var airborne: float = 0.0 +enum Wall { + NONE = 0 + RIGHT = 1, + LEFT = -1, +} # Called when the node enters the scene tree for the first time. func _ready(): pass @@ -25,10 +35,23 @@ func _ready(): #func _process(delta): # pass +func _wall_touched(): + if left_wall_cast.is_colliding(): + return Wall.LEFT + elif right_wall_cast.is_colliding(): + return Wall.RIGHT + else: + return Wall.NONE +func _is_touching_wall(): + var wall_touched = _wall_touched() + return wall_touched == Wall.RIGHT || wall_touched == Wall.LEFT + func _physics_process(_delta): var input_direction: float = get_direction() - + var wall_jumped_off = Wall.NONE + gravity = default_gravity + if is_on_floor(): airborne = 0 is_jumping = false @@ -40,17 +63,26 @@ func _physics_process(_delta): if can_jump(): if Input.is_action_pressed("jump"): + if _wall_touched() != Wall.NONE && Input.is_action_just_pressed("jump): + wall_jumped_off = _wall_touched() _velocity.y = -speed.y + is_jumping = true queue_jump = false Input.action_press("jump") var is_jump_interrupted = is_jumping and Input.is_action_just_released("jump") and _velocity.y < 0.0 - _velocity = calculate_move_velocity(_velocity, input_direction, speed, is_jump_interrupted) - + _velocity = calculate_move_velocity(_velocity, input_direction, speed, is_jump_interrupted, wall_jumped_off) + print(_velocity) + # Slightly raise the slime when jumping so that it can jump on rising platform if _velocity.y < 0: position.y -= 0.5 + + # Add friction when sliding down a wall + if _is_touching_wall() && _velocity.y > 0: + print("APPLY FRICTION") + gravity *= WALL_FRICTION var snap_vector = Vector2.ZERO if not (Input.is_action_just_pressed("jump") or queue_jump): @@ -73,16 +105,20 @@ func _physics_process(_delta): func can_jump(): - return is_on_floor() or (airborne < COYOTE_TIME and not is_jumping) + return is_on_floor() or (airborne < COYOTE_TIME and not is_jumping) or _is_touching_wall() func calculate_move_velocity( linear_velocity: Vector2, direction, speed, - is_jump_interrupted + is_jump_interrupted, + wall_jumped_off ) -> Vector2: var velocity = linear_velocity - velocity.x = speed.x * direction + if wall_jumped_off != Wall.NONE: + velocity.x = wall_jumped_off * -WALL_IMPULSE + else: + velocity.x = speed.x * direction if is_jump_interrupted: # Decrease the Y velocity by multiplying it, but don't set it to 0 # as to not be too abrupt. diff --git a/entities/actor/player.tscn b/entities/actor/player.tscn index b9a6822..74be06c 100644 --- a/entities/actor/player.tscn +++ b/entities/actor/player.tscn @@ -47,6 +47,7 @@ tracks/0/keys = { } [node name="Player" type="KinematicBody2D"] +position = Vector2( -3.31744, -1.99045 ) z_index = 1 script = ExtResource( 2 ) __meta__ = { @@ -98,3 +99,16 @@ region_rect = Rect2( 584.58, 101.432, 49.3323, 102.459 ) __meta__ = { "_edit_lock_": true } + +[node name="Timer" type="Timer" parent="."] +one_shot = true + +[node name="LeftWallCast" type="RayCast2D" parent="."] +position = Vector2( 0, -10 ) +enabled = true +cast_to = Vector2( -50, 0 ) + +[node name="RightWallCast" type="RayCast2D" parent="."] +position = Vector2( 0, -10 ) +enabled = true +cast_to = Vector2( 50, 0 ) diff --git a/entities/wall/wall.tscn b/entities/wall/wall.tscn new file mode 100644 index 0000000..78bffbf --- /dev/null +++ b/entities/wall/wall.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://assets/art/tileset/forest_tiles.png" type="Texture" id=1] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 31.7296, 96.0506 ) + +[node name="Wall" type="StaticBody2D"] + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) +region_enabled = true +region_rect = Rect2( 1.84998, 1.842, 62, 191 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) diff --git a/scenes/wall_jump.tscn b/scenes/wall_jump.tscn new file mode 100644 index 0000000..9b23e84 --- /dev/null +++ b/scenes/wall_jump.tscn @@ -0,0 +1,278 @@ +[gd_scene load_steps=24 format=2] + +[ext_resource path="res://assets/art/bg/3_forest_back.png" type="Texture" id=1] +[ext_resource path="res://assets/art/bg/2_forest_mid_back.png" type="Texture" id=2] +[ext_resource path="res://assets/art/bg/1_forest_mid_front.png" type="Texture" id=3] +[ext_resource path="res://assets/art/bg/0_forest_front.png" type="Texture" id=4] +[ext_resource path="res://entities/actor/player.tscn" type="PackedScene" id=5] +[ext_resource path="res://assets/art/tileset/forest_tiles.png" type="Texture" id=6] +[ext_resource path="res://assets/art/bg/4_sky.png" type="Texture" id=7] +[ext_resource path="res://entities/wall/wall.tscn" type="PackedScene" id=8] + +[sub_resource type="ConvexPolygonShape2D" id=1] +points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) + +[sub_resource type="ConvexPolygonShape2D" id=2] +points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) + +[sub_resource type="ConvexPolygonShape2D" id=3] +points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) + +[sub_resource type="ConvexPolygonShape2D" id=4] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=5] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=6] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=7] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=8] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=9] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=10] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=11] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=12] +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) + +[sub_resource type="TileSet" id=13] +0/name = "forest_tiles.png 0" +0/texture = ExtResource( 6 ) +0/tex_offset = Vector2( 0, 0 ) +0/modulate = Color( 1, 1, 1, 1 ) +0/region = Rect2( 0, 0, 576, 192 ) +0/tile_mode = 2 +0/autotile/icon_coordinate = Vector2( 0, 0 ) +0/autotile/tile_size = Vector2( 64, 64 ) +0/autotile/spacing = 0 +0/autotile/occluder_map = [ ] +0/autotile/navpoly_map = [ ] +0/autotile/priority_map = [ ] +0/autotile/z_index_map = [ ] +0/occluder_offset = Vector2( 0, 0 ) +0/navigation_offset = Vector2( 0, 0 ) +0/shape_offset = Vector2( 0, 0 ) +0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 ) +0/shape = SubResource( 1 ) +0/shape_one_way = false +0/shape_one_way_margin = 1.0 +0/shapes = [ { +"autotile_coord": Vector2( 1, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 1 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 2, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 2 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 3, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 3 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 4, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 4 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 5, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 5 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 6, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 6 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 7, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 7 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 8, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 8 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 7, 1 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 9 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 8, 1 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 10 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 7, 2 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 11 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 8, 2 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 12 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +} ] +0/z_index = 0 + +[sub_resource type="TileSet" id=14] +0/name = "forest_tiles.png 0" +0/texture = ExtResource( 6 ) +0/tex_offset = Vector2( 0, 0 ) +0/modulate = Color( 1, 1, 1, 1 ) +0/region = Rect2( 0, 0, 576, 192 ) +0/tile_mode = 2 +0/autotile/icon_coordinate = Vector2( 0, 0 ) +0/autotile/tile_size = Vector2( 64, 64 ) +0/autotile/spacing = 0 +0/autotile/occluder_map = [ ] +0/autotile/navpoly_map = [ ] +0/autotile/priority_map = [ ] +0/autotile/z_index_map = [ ] +0/occluder_offset = Vector2( 0, 0 ) +0/navigation_offset = Vector2( 0, 0 ) +0/shape_offset = Vector2( 0, 0 ) +0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 ) +0/shape_one_way = false +0/shape_one_way_margin = 0.0 +0/shapes = [ ] +0/z_index = 0 + +[sub_resource type="RectangleShape2D" id=15] +extents = Vector2( 64, 32 ) + +[node name="Node2D2" type="Node2D"] + +[node name="Player" parent="." instance=ExtResource( 5 )] +position = Vector2( 32, 800 ) + +[node name="Camera2D" type="Camera2D" parent="Player"] +current = true +limit_left = 0 +limit_bottom = 962 +limit_smoothed = true +drag_margin_h_enabled = true +drag_margin_v_enabled = true +smoothing_enabled = true + +[node name="Solid Forest Tiles" type="TileMap" parent="."] +tile_set = SubResource( 13 ) +format = 1 +tile_data = PoolIntArray( 917504, 0, 1, 917505, 0, 2, 917506, 0, 2, 917507, 0, 2, 917508, 0, 2, 917509, 0, 2, 917510, 0, 2, 917511, 0, 2, 917512, 0, 2, 917513, 0, 2, 917514, 0, 2, 917515, 0, 2, 917516, 0, 2, 917517, 0, 2, 917518, 0, 2, 917519, 0, 2, 917520, 0, 2, 917521, 0, 2, 917522, 0, 2, 917523, 0, 2, 917524, 0, 2, 917525, 0, 2, 917526, 0, 2, 917527, 0, 2, 917528, 0, 2, 917529, 0, 2, 917530, 0, 2, 917531, 0, 2, 917532, 0, 2, 917533, 0, 2, 917534, 0, 2, 917535, 0, 2, 983040, 0, 0, 983041, 0, 65536, 983042, 0, 131072, 983043, 0, 131072, 983044, 0, 131073 ) + +[node name="ParallaxBackground" type="ParallaxBackground" parent="."] + +[node name="Sky" type="ParallaxLayer" parent="ParallaxBackground"] +position = Vector2( -56, -128 ) +motion_mirroring = Vector2( 2048, 1024 ) + +[node name="Sprite" type="Sprite" parent="ParallaxBackground/Sky"] +position = Vector2( 0, -16 ) +texture = ExtResource( 7 ) +centered = false + +[node name="forest_back" type="ParallaxLayer" parent="ParallaxBackground"] +position = Vector2( 1016, 392 ) +motion_scale = Vector2( 0.3, 1 ) +motion_mirroring = Vector2( 2048, 0 ) +__meta__ = { +"_edit_group_": true +} + +[node name="Sprite" type="Sprite" parent="ParallaxBackground/forest_back"] +texture = ExtResource( 1 ) + +[node name="forest_mid_back" type="ParallaxLayer" parent="ParallaxBackground"] +position = Vector2( 1032, 392 ) +motion_scale = Vector2( 0.5, 1 ) +motion_mirroring = Vector2( 2048, 0 ) +__meta__ = { +"_edit_group_": true +} + +[node name="Sprite" type="Sprite" parent="ParallaxBackground/forest_mid_back"] +texture = ExtResource( 2 ) + +[node name="forest_mid_front" type="ParallaxLayer" parent="ParallaxBackground"] +position = Vector2( 1024, 400 ) +motion_scale = Vector2( 0.7, 1 ) +motion_mirroring = Vector2( 2048, 0 ) +__meta__ = { +"_edit_group_": true +} + +[node name="Sprite" type="Sprite" parent="ParallaxBackground/forest_mid_front"] +texture = ExtResource( 3 ) + +[node name="forest_front" type="ParallaxLayer" parent="ParallaxBackground"] +position = Vector2( 1056, 400 ) +motion_scale = Vector2( 0.9, 1 ) +motion_mirroring = Vector2( 2048, 0 ) +__meta__ = { +"_edit_group_": true +} + +[node name="Sprite" type="Sprite" parent="ParallaxBackground/forest_front"] +texture = ExtResource( 4 ) + +[node name="Forest Tiles" type="TileMap" parent="."] +tile_set = SubResource( 14 ) +format = 1 +tile_data = PoolIntArray( 786435, 0, 65543, 786436, 0, 65544 ) + +[node name="StaticBody2D" type="StaticBody2D" parent="Forest Tiles"] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Forest Tiles/StaticBody2D"] +position = Vector2( 256, 800 ) +shape = SubResource( 15 ) +one_way_collision = true + +[node name="StaticBody2D" parent="." instance=ExtResource( 8 )] +position = Vector2( 722.668, 798.777 ) + +[node name="StaticBody2D2" parent="." instance=ExtResource( 8 )] +position = Vector2( 722.668, 609.612 ) + +[node name="StaticBody2D3" parent="." instance=ExtResource( 8 )] +position = Vector2( 722.668, 418.97 ) + +[node name="StaticBody2D4" parent="." instance=ExtResource( 8 )] +position = Vector2( 527.592, 413.797 ) + +[node name="StaticBody2D7" parent="." instance=ExtResource( 8 )] +position = Vector2( 527.592, 224.633 ) + +[node name="StaticBody2D8" parent="." instance=ExtResource( 8 )] +position = Vector2( 722.668, 229.066 ) + +[node name="StaticBody2D5" parent="." instance=ExtResource( 8 )] +position = Vector2( 526.853, 605.918 ) + +[node name="StaticBody2D6" parent="." instance=ExtResource( 8 )] +position = Vector2( 526.853, 605.918 ) From 32f21c5600fc00b4bb2a76a8c3bd3357a6b60144 Mon Sep 17 00:00:00 2001 From: nomaxg Date: Sat, 12 Jun 2021 20:11:15 -0400 Subject: [PATCH 2/4] use min velocity instead of changing gravity --- entities/actor/actor.gd | 3 +-- entities/actor/player.gd | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/entities/actor/actor.gd b/entities/actor/actor.gd index aa3adc1..3dd043f 100644 --- a/entities/actor/actor.gd +++ b/entities/actor/actor.gd @@ -6,8 +6,7 @@ extends KinematicBody2D export var speed = Vector2(300.0, 700.0) -onready var default_gravity = ProjectSettings.get("physics/2d/default_gravity") -onready var gravity = default_gravity +onready var gravity = ProjectSettings.get("physics/2d/default_gravity") const FLOOR_NORMAL = Vector2.UP diff --git a/entities/actor/player.gd b/entities/actor/player.gd index 5152a82..00fcdfe 100644 --- a/entities/actor/player.gd +++ b/entities/actor/player.gd @@ -6,7 +6,7 @@ extends Actor const COYOTE_TIME = 0.1 const WALL_IMPULSE = 3000 -const WALL_FRICTION = 0.1 +const MIN_SLIDE_VELOCITY = 20 onready var FLOOR_DETECT_DISTANCE = $PlatformDetector.cast_to.y @@ -50,7 +50,6 @@ func _is_touching_wall(): func _physics_process(_delta): var input_direction: float = get_direction() var wall_jumped_off = Wall.NONE - gravity = default_gravity if is_on_floor(): airborne = 0 @@ -80,9 +79,8 @@ func _physics_process(_delta): position.y -= 0.5 # Add friction when sliding down a wall - if _is_touching_wall() && _velocity.y > 0: - print("APPLY FRICTION") - gravity *= WALL_FRICTION + if _is_touching_wall(): + _velocity.y = min(_velocity.y, MIN_SLIDE_VELOCITY) var snap_vector = Vector2.ZERO if not (Input.is_action_just_pressed("jump") or queue_jump): From 04e1c2460241f637485cbf33dd555cbee08e0411 Mon Sep 17 00:00:00 2001 From: nomaxg Date: Sat, 12 Jun 2021 20:20:33 -0400 Subject: [PATCH 3/4] removed print statement --- entities/actor/player.gd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entities/actor/player.gd b/entities/actor/player.gd index 00fcdfe..ef08283 100644 --- a/entities/actor/player.gd +++ b/entities/actor/player.gd @@ -62,7 +62,7 @@ func _physics_process(_delta): if can_jump(): if Input.is_action_pressed("jump"): - if _wall_touched() != Wall.NONE && Input.is_action_just_pressed("jump): + if _wall_touched() != Wall.NONE && Input.is_action_just_pressed("jump"): wall_jumped_off = _wall_touched() _velocity.y = -speed.y @@ -72,7 +72,6 @@ func _physics_process(_delta): var is_jump_interrupted = is_jumping and Input.is_action_just_released("jump") and _velocity.y < 0.0 _velocity = calculate_move_velocity(_velocity, input_direction, speed, is_jump_interrupted, wall_jumped_off) - print(_velocity) # Slightly raise the slime when jumping so that it can jump on rising platform if _velocity.y < 0: From efc1e6e15cdc2eb1dc545c82956e50376926cdd7 Mon Sep 17 00:00:00 2001 From: Ted Kern Date: Sun, 13 Jun 2021 02:22:44 -0700 Subject: [PATCH 4/4] refactor movement entirely --- entities/actor/player.gd | 188 +++++++++++++++++++++++-------------- entities/actor/player.tscn | 32 +++---- project.godot | 2 +- scenes/level1.tscn | 38 ++++---- 4 files changed, 150 insertions(+), 110 deletions(-) diff --git a/entities/actor/player.gd b/entities/actor/player.gd index ef08283..7039b5a 100644 --- a/entities/actor/player.gd +++ b/entities/actor/player.gd @@ -5,122 +5,164 @@ extends Actor # var b = "text" const COYOTE_TIME = 0.1 +const MIN_WALL_STICK_TIME = 0.1 const WALL_IMPULSE = 3000 -const MIN_SLIDE_VELOCITY = 20 +const MAX_SLIDE_VELOCITY = 100 -onready var FLOOR_DETECT_DISTANCE = $PlatformDetector.cast_to.y +export(Vector2) var wall_climb +export(Vector2) var wall_pop +export(Vector2) var wall_leap -onready var platform_detector = $PlatformDetector -onready var animation_player = $AnimationPlayer -onready var sprite = $SpriteContainer/Sprite -onready var bounce_detector = $BounceDetector -onready var left_wall_cast = $LeftWallCast -onready var right_wall_cast = $RightWallCast +onready var platform_detector: RayCast2D = $PlatformDetector +onready var animation_player: AnimationPlayer = $AnimationPlayer +onready var sprite: Sprite = $SpriteContainer/Sprite +onready var bounce_detector: RayCast2D = $BounceDetector +onready var left_wall_cast: RayCast2D = $LeftWallCast +onready var right_wall_cast: RayCast2D = $RightWallCast -var queue_jump = false +onready var FLOOR_DETECT_DISTANCE = platform_detector.cast_to.y + +var jump_queued = false var is_jumping = false -var just_wall_jumped +var wall_sliding = false +var just_wall_jumped = false var airborne: float = 0.0 +var wall_stick_time: float = 0.0 enum Wall { NONE = 0 RIGHT = 1, LEFT = -1, } + + # Called when the node enters the scene tree for the first time. func _ready(): pass - + # Called every frame. 'delta' is the elapsed time since the previous frame. #func _process(delta): # pass -func _wall_touched(): +func wall_touched() -> int: if left_wall_cast.is_colliding(): return Wall.LEFT - elif right_wall_cast.is_colliding(): + elif right_wall_cast.is_colliding(): return Wall.RIGHT else: return Wall.NONE -func _is_touching_wall(): - var wall_touched = _wall_touched() - return wall_touched == Wall.RIGHT || wall_touched == Wall.LEFT - + func _physics_process(_delta): var input_direction: float = get_direction() - var wall_jumped_off = Wall.NONE - - if is_on_floor(): - airborne = 0 - is_jumping = false - else: - airborne += _delta - - if bounce_detector.is_colliding() and Input.is_action_just_pressed("jump"): - queue_jump = true - - if can_jump(): - if Input.is_action_pressed("jump"): - if _wall_touched() != Wall.NONE && Input.is_action_just_pressed("jump"): - wall_jumped_off = _wall_touched() - _velocity.y = -speed.y - - is_jumping = true - queue_jump = false - Input.action_press("jump") - - var is_jump_interrupted = is_jumping and Input.is_action_just_released("jump") and _velocity.y < 0.0 - _velocity = calculate_move_velocity(_velocity, input_direction, speed, is_jump_interrupted, wall_jumped_off) - - # Slightly raise the slime when jumping so that it can jump on rising platform - if _velocity.y < 0: - position.y -= 0.5 - - # Add friction when sliding down a wall - if _is_touching_wall(): - _velocity.y = min(_velocity.y, MIN_SLIDE_VELOCITY) - + update_hoizontal_velocity(input_direction) + do_wall_slide(_delta) + do_jump(_delta) +# +# var snap_vector = Vector2.ZERO - if not (Input.is_action_just_pressed("jump") or queue_jump): + if not (Input.is_action_just_pressed("jump") or jump_queued): snap_vector = Vector2.DOWN * FLOOR_DETECT_DISTANCE _velocity = move_and_slide_with_snap( _velocity, snap_vector, FLOOR_NORMAL, not platform_detector.is_colliding(), 4, 0.9, false ) - - # When the character’s direction changes, we want to to scale the Sprite accordingly to flip it. - # This will make Robi face left or right depending on the direction you move. +# if input_direction != 0: if input_direction > 0: sprite.flip_h = false else: sprite.flip_h = true - +# var animation = get_new_animation() if animation != animation_player.current_animation: animation_player.play(animation) +func do_jump(delta): + if Input.is_action_just_pressed("jump"): + handle_jump_pressed() + elif Input.is_action_just_released("jump"): + handle_jump_released() + + if not is_on_floor(): + airborne += delta + return + + airborne = 0 + is_jumping = false + if jump_queued: + is_jumping = true + jump_queued = false + if _velocity.y < 0: + position.y -= 1 + _velocity.y = -speed.y + + +func is_moving_into_wall() -> bool: + return wall_touched() and sign(get_direction()) == wall_touched() + + +func get_walljump_type() -> Vector2: + if is_moving_into_wall(): + return wall_climb + #elif is_zero_approx(get_direction()): + # return wall_pop + else: + return wall_leap + + +func do_wall_slide(delta: float): + wall_sliding = wall_touched() and !is_on_floor() + if wall_sliding: + _velocity.y = min(_velocity.y, MAX_SLIDE_VELOCITY) + + if wall_stick_time < MIN_WALL_STICK_TIME: + _velocity.x = 0 + + if !is_moving_into_wall(): + wall_stick_time += delta + else: + wall_stick_time = 0 + else: + wall_stick_time = 0 + + +func handle_jump_pressed(): + if wall_sliding: + _velocity = -Vector2(wall_touched(), 1) * get_walljump_type() + is_jumping = true + elif can_jump(): + jump_queued = true + + +func handle_jump_released(): + if is_jumping and _velocity.y < 0: + _velocity.y = _velocity.y * .6 + + func can_jump(): - return is_on_floor() or (airborne < COYOTE_TIME and not is_jumping) or _is_touching_wall() - -func calculate_move_velocity( - linear_velocity: Vector2, - direction, - speed, - is_jump_interrupted, - wall_jumped_off - ) -> Vector2: - var velocity = linear_velocity - if wall_jumped_off != Wall.NONE: - velocity.x = wall_jumped_off * -WALL_IMPULSE - else: - velocity.x = speed.x * direction - if is_jump_interrupted: - # Decrease the Y velocity by multiplying it, but don't set it to 0 - # as to not be too abrupt. - velocity.y *= 0.6 - return velocity + return is_on_floor() or (airborne < COYOTE_TIME and not is_jumping) + +#func calculate_move_velocity( +# linear_velocity: Vector2, +# direction, +# speed, +# is_jump_interrupted, +# wall_jumped_off +# ) -> Vector2: +# var velocity = linear_velocity +# if wall_jumped_off != Wall.NONE: +# velocity.x = wall_jumped_off * -WALL_IMPULSE +# else: +# velocity.x = speed.x * direction +# if is_jump_interrupted: +# # Decrease the Y velocity by multiplying it, but don't set it to 0 +# # as to not be too abrupt. +# velocity.y *= 0.6 +# return velocity + +func update_hoizontal_velocity(direction): + _velocity.x = direction * speed.x func get_direction() -> float: diff --git a/entities/actor/player.tscn b/entities/actor/player.tscn index 74be06c..5739f2b 100644 --- a/entities/actor/player.tscn +++ b/entities/actor/player.tscn @@ -3,14 +3,14 @@ [ext_resource path="res://assets/art/player/green_slime_walk_right_spritesheet.png" type="Texture" id=1] [ext_resource path="res://entities/actor/player.gd" type="Script" id=2] -[sub_resource type="CapsuleShape2D" id=1] +[sub_resource type="CapsuleShape2D" id=8] radius = 15.0 -height = 25.0 [sub_resource type="Animation" id=4] -resource_name = "falling" +resource_name = "jumping" +length = 0.7 -[sub_resource type="Animation" id=2] +[sub_resource type="Animation" id=5] resource_name = "idle" length = 0.1 tracks/0/type = "value" @@ -26,11 +26,10 @@ tracks/0/keys = { "values": [ 0 ] } -[sub_resource type="Animation" id=5] -resource_name = "jumping" +[sub_resource type="Animation" id=6] length = 0.7 -[sub_resource type="Animation" id=3] +[sub_resource type="Animation" id=7] resource_name = "walk" length = 0.1 tracks/0/type = "value" @@ -47,17 +46,19 @@ tracks/0/keys = { } [node name="Player" type="KinematicBody2D"] -position = Vector2( -3.31744, -1.99045 ) z_index = 1 script = ExtResource( 2 ) __meta__ = { "_edit_lock_": true } +wall_climb = Vector2( 50, 700 ) +wall_pop = Vector2( 200, 700 ) +wall_leap = Vector2( 300, 700 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2( 0, -15 ) rotation = 1.5708 -shape = SubResource( 1 ) +shape = SubResource( 8 ) __meta__ = { "_edit_lock_": true } @@ -73,9 +74,9 @@ __meta__ = { [node name="AnimationPlayer" type="AnimationPlayer" parent="."] anims/falling = SubResource( 4 ) -anims/idle = SubResource( 2 ) -anims/jumping = SubResource( 5 ) -anims/walk = SubResource( 3 ) +anims/idle = SubResource( 5 ) +anims/jumping = SubResource( 6 ) +anims/walk = SubResource( 7 ) [node name="BounceDetector" type="RayCast2D" parent="."] enabled = true @@ -100,15 +101,12 @@ __meta__ = { "_edit_lock_": true } -[node name="Timer" type="Timer" parent="."] -one_shot = true - [node name="LeftWallCast" type="RayCast2D" parent="."] position = Vector2( 0, -10 ) enabled = true -cast_to = Vector2( -50, 0 ) +cast_to = Vector2( -30, 0 ) [node name="RightWallCast" type="RayCast2D" parent="."] position = Vector2( 0, -10 ) enabled = true -cast_to = Vector2( 50, 0 ) +cast_to = Vector2( 30, 0 ) diff --git a/project.godot b/project.godot index c06d210..7fb3a98 100644 --- a/project.godot +++ b/project.godot @@ -14,7 +14,7 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://entities/actor/actor.gd" }, { -"base": "EditorVCSInterface", +"base": "", "class": "GitAPI", "language": "NativeScript", "path": "res://git_api.gdns" diff --git a/scenes/level1.tscn b/scenes/level1.tscn index f3d6cc8..e8673c5 100644 --- a/scenes/level1.tscn +++ b/scenes/level1.tscn @@ -8,6 +8,9 @@ [ext_resource path="res://assets/art/bg/1_forest_mid_front.png" type="Texture" id=6] [ext_resource path="res://assets/art/bg/0_forest_front.png" type="Texture" id=7] +[sub_resource type="ConvexPolygonShape2D" id=1] +points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) + [sub_resource type="ConvexPolygonShape2D" id=2] points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) @@ -15,7 +18,7 @@ points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) [sub_resource type="ConvexPolygonShape2D" id=4] -points = PoolVector2Array( 0, 0, 64, 0, 64, 64, 0, 64 ) +points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) [sub_resource type="ConvexPolygonShape2D" id=5] points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) @@ -41,10 +44,7 @@ points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) [sub_resource type="ConvexPolygonShape2D" id=12] points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) -[sub_resource type="ConvexPolygonShape2D" id=13] -points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) - -[sub_resource type="TileSet" id=1] +[sub_resource type="TileSet" id=13] 0/name = "forest_tiles.png 0" 0/texture = ExtResource( 1 ) 0/tex_offset = Vector2( 0, 0 ) @@ -62,80 +62,80 @@ points = PoolVector2Array( 64, 64, 0, 64, 0, 0, 64, 0 ) 0/navigation_offset = Vector2( 0, 0 ) 0/shape_offset = Vector2( 0, 0 ) 0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 ) -0/shape = SubResource( 2 ) +0/shape = SubResource( 1 ) 0/shape_one_way = false 0/shape_one_way_margin = 1.0 0/shapes = [ { "autotile_coord": Vector2( 1, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 2 ), +"shape": SubResource( 1 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 2, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 3 ), +"shape": SubResource( 2 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 3, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 4 ), +"shape": SubResource( 3 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 4, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 5 ), +"shape": SubResource( 4 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 5, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 6 ), +"shape": SubResource( 5 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 6, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 7 ), +"shape": SubResource( 6 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 7, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 8 ), +"shape": SubResource( 7 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 8, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 9 ), +"shape": SubResource( 8 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 7, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 10 ), +"shape": SubResource( 9 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 8, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 11 ), +"shape": SubResource( 10 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 7, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 12 ), +"shape": SubResource( 11 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 8, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 13 ), +"shape": SubResource( 12 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) } ] 0/z_index = 0 @@ -181,7 +181,7 @@ drag_margin_v_enabled = true smoothing_enabled = true [node name="Solid Forest Tiles" type="TileMap" parent="."] -tile_set = SubResource( 1 ) +tile_set = SubResource( 13 ) format = 1 tile_data = PoolIntArray( 917504, 0, 1, 917505, 0, 2, 917506, 0, 2, 917507, 0, 2, 917508, 0, 2, 917509, 0, 2, 917510, 0, 2, 917511, 0, 2, 917512, 0, 2, 917513, 0, 2, 917514, 0, 2, 917515, 0, 2, 917516, 0, 2, 917517, 0, 2, 917518, 0, 2, 917519, 0, 2, 917520, 0, 2, 917521, 0, 2, 917522, 0, 2, 917523, 0, 2, 917524, 0, 2, 917525, 0, 2, 917526, 0, 2, 917527, 0, 2, 917528, 0, 2, 917529, 0, 2, 917530, 0, 2, 917531, 0, 2, 917532, 0, 2, 917533, 0, 2, 917534, 0, 2, 917535, 0, 2, 983040, 0, 0, 983041, 0, 65536, 983042, 0, 131072, 983043, 0, 131072, 983044, 0, 131073 )