Skip to content

Commit 746928b

Browse files
committed
Update kinematic character
1 parent b0b63e8 commit 746928b

File tree

6 files changed

+79
-93
lines changed

6 files changed

+79
-93
lines changed

2d/kinematic_character/colworld.gd

Lines changed: 0 additions & 7 deletions
This file was deleted.

2d/kinematic_character/player.gd

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
extends KinematicBody2D
22

3-
# This demo shows how to build a kinematic controller.
4-
5-
# Member variables
6-
const GRAVITY = 500.0 # pixels/second/second
7-
8-
# Angle in degrees towards either side that the player can consider "floor"
3+
# Angle in degrees towards either side that the player can consider "floor".
94
const FLOOR_ANGLE_TOLERANCE = 40
105
const WALK_FORCE = 600
116
const WALK_MIN_SPEED = 10
@@ -14,55 +9,42 @@ const STOP_FORCE = 1300
149
const JUMP_SPEED = 200
1510
const JUMP_MAX_AIRBORNE_TIME = 0.2
1611

17-
const SLIDE_STOP_VELOCITY = 1.0 # one pixel/second
18-
const SLIDE_STOP_MIN_TRAVEL = 1.0 # one pixel
12+
const SLIDE_STOP_VELOCITY = 1.0 # Pixels/second
13+
const SLIDE_STOP_MIN_TRAVEL = 1.0 # Pixels
1914

2015
var velocity = Vector2()
2116
var on_air_time = 100
2217
var jumping = false
23-
2418
var prev_jump_pressed = false
2519

20+
onready var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
2621

2722
func _physics_process(delta):
28-
# Create forces
29-
var force = Vector2(0, GRAVITY)
30-
31-
var walk_left = Input.is_action_pressed("move_left")
32-
var walk_right = Input.is_action_pressed("move_right")
23+
var force = Vector2(0, gravity)
24+
var walk = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
3325
var jump = Input.is_action_pressed("jump")
3426

35-
var stop = true
36-
37-
if walk_left:
38-
if velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED:
39-
force.x -= WALK_FORCE
40-
stop = false
41-
elif walk_right:
42-
if velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED:
43-
force.x += WALK_FORCE
44-
stop = false
45-
46-
if stop:
27+
if (velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED) or (velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED):
28+
force.x += WALK_FORCE * walk
29+
30+
if abs(walk) < 0.5:
4731
var vsign = sign(velocity.x)
4832
var vlen = abs(velocity.x)
49-
5033
vlen -= STOP_FORCE * delta
5134
if vlen < 0:
5235
vlen = 0
53-
5436
velocity.x = vlen * vsign
5537

56-
# Integrate forces to velocity
38+
# Integrate forces to velocity.
5739
velocity += force * delta
58-
# Integrate velocity into motion and move
59-
velocity = move_and_slide(velocity, Vector2(0, -1))
40+
# Integrate velocity into motion and move.
41+
velocity = move_and_slide(velocity, Vector2.UP)
6042

6143
if is_on_floor():
6244
on_air_time = 0
6345

6446
if jumping and velocity.y > 0:
65-
# If falling, no longer jumping
47+
# If falling, no longer jumping.
6648
jumping = false
6749

6850
if on_air_time < JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping:

2d/kinematic_character/player.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
[sub_resource type="RectangleShape2D" id=1]
77
extents = Vector2( 7, 7 )
88

9-
[node name="player" type="KinematicBody2D"]
9+
[node name="Player" type="KinematicBody2D"]
1010
script = ExtResource( 1 )
1111

12-
[node name="sprite" type="Sprite" parent="."]
12+
[node name="Sprite" type="Sprite" parent="."]
1313
texture = ExtResource( 2 )
1414

1515
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]

2d/kinematic_character/project.godot

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ _global_script_class_icons={
1616
[application]
1717

1818
config/name="Kinematic Character"
19-
run/main_scene="res://colworld.tscn"
19+
run/main_scene="res://world.tscn"
2020
config/icon="res://icon.png"
2121

2222
[display]
@@ -37,33 +37,38 @@ singletons=[ ]
3737
jump={
3838
"deadzone": 0.5,
3939
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
40-
]
41-
}
42-
move_bottom={
43-
"deadzone": 0.5,
44-
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null)
40+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
41+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
42+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
43+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
44+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
4545
]
4646
}
4747
move_left={
4848
"deadzone": 0.5,
4949
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null)
50+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
51+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null)
52+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null)
5053
]
5154
}
5255
move_right={
5356
"deadzone": 0.5,
5457
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
55-
]
56-
}
57-
move_up={
58-
"deadzone": 0.5,
59-
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
58+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
59+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null)
60+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
6061
]
6162
}
6263

6364
[memory]
6465

6566
multithread/thread_rid_pool_prealloc=60
6667

68+
[physics]
69+
70+
2d/default_gravity=500
71+
6772
[rendering]
6873

6974
environment/default_clear_color=Color( 0.156, 0.1325, 0.25, 1 )

2d/kinematic_character/world.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extends Node2D
2+
3+
func _on_princess_body_enter(body):
4+
# The name of this editor-generated callback is unfortunate.
5+
if body.get_name() == "Player":
6+
$WinText.show()

2d/kinematic_character/colworld.tscn renamed to 2d/kinematic_character/world.tscn

Lines changed: 41 additions & 41 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)