Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/wall jump #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 124 additions & 49 deletions entities/actor/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,164 @@ extends Actor
# var b = "text"

const COYOTE_TIME = 0.1
const MIN_WALL_STICK_TIME = 0.1
const WALL_IMPULSE = 3000
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 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 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() -> int:
if left_wall_cast.is_colliding():
return Wall.LEFT
elif right_wall_cast.is_colliding():
return Wall.RIGHT
else:
return Wall.NONE


func _physics_process(_delta):
var input_direction: float = get_direction()

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"):
_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)

# Slightly raise the slime when jumping so that it can jump on rising platform
if _velocity.y < 0:
position.y -= 0.5

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)

func calculate_move_velocity(
linear_velocity: Vector2,
direction,
speed,
is_jump_interrupted
) -> Vector2:
var velocity = linear_velocity
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 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:
Expand Down
34 changes: 23 additions & 11 deletions entities/actor/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -52,11 +51,14 @@ 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
}
Expand All @@ -72,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
Expand All @@ -98,3 +100,13 @@ region_rect = Rect2( 584.58, 101.432, 49.3323, 102.459 )
__meta__ = {
"_edit_lock_": true
}

[node name="LeftWallCast" type="RayCast2D" parent="."]
position = Vector2( 0, -10 )
enabled = true
cast_to = Vector2( -30, 0 )

[node name="RightWallCast" type="RayCast2D" parent="."]
position = Vector2( 0, -10 )
enabled = true
cast_to = Vector2( 30, 0 )
16 changes: 16 additions & 0 deletions entities/wall/wall.tscn
Original file line number Diff line number Diff line change
@@ -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 )
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading