mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 23:10:08 +01:00
Update kinematic character
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
func _on_princess_body_enter(body):
|
||||
# The name of this editor-generated callback is unfortunate
|
||||
if body.get_name() == "player":
|
||||
$youwin.show()
|
||||
@@ -1,11 +1,6 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
# This demo shows how to build a kinematic controller.
|
||||
|
||||
# Member variables
|
||||
const GRAVITY = 500.0 # pixels/second/second
|
||||
|
||||
# Angle in degrees towards either side that the player can consider "floor"
|
||||
# Angle in degrees towards either side that the player can consider "floor".
|
||||
const FLOOR_ANGLE_TOLERANCE = 40
|
||||
const WALK_FORCE = 600
|
||||
const WALK_MIN_SPEED = 10
|
||||
@@ -14,55 +9,42 @@ const STOP_FORCE = 1300
|
||||
const JUMP_SPEED = 200
|
||||
const JUMP_MAX_AIRBORNE_TIME = 0.2
|
||||
|
||||
const SLIDE_STOP_VELOCITY = 1.0 # one pixel/second
|
||||
const SLIDE_STOP_MIN_TRAVEL = 1.0 # one pixel
|
||||
const SLIDE_STOP_VELOCITY = 1.0 # Pixels/second
|
||||
const SLIDE_STOP_MIN_TRAVEL = 1.0 # Pixels
|
||||
|
||||
var velocity = Vector2()
|
||||
var on_air_time = 100
|
||||
var jumping = false
|
||||
|
||||
var prev_jump_pressed = false
|
||||
|
||||
onready var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
||||
|
||||
func _physics_process(delta):
|
||||
# Create forces
|
||||
var force = Vector2(0, GRAVITY)
|
||||
|
||||
var walk_left = Input.is_action_pressed("move_left")
|
||||
var walk_right = Input.is_action_pressed("move_right")
|
||||
var force = Vector2(0, gravity)
|
||||
var walk = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
||||
var jump = Input.is_action_pressed("jump")
|
||||
|
||||
var stop = true
|
||||
|
||||
if walk_left:
|
||||
if velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED:
|
||||
force.x -= WALK_FORCE
|
||||
stop = false
|
||||
elif walk_right:
|
||||
if velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED:
|
||||
force.x += WALK_FORCE
|
||||
stop = false
|
||||
|
||||
if stop:
|
||||
if (velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED) or (velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED):
|
||||
force.x += WALK_FORCE * walk
|
||||
|
||||
if abs(walk) < 0.5:
|
||||
var vsign = sign(velocity.x)
|
||||
var vlen = abs(velocity.x)
|
||||
|
||||
vlen -= STOP_FORCE * delta
|
||||
if vlen < 0:
|
||||
vlen = 0
|
||||
|
||||
velocity.x = vlen * vsign
|
||||
|
||||
# Integrate forces to velocity
|
||||
# Integrate forces to velocity.
|
||||
velocity += force * delta
|
||||
# Integrate velocity into motion and move
|
||||
velocity = move_and_slide(velocity, Vector2(0, -1))
|
||||
# Integrate velocity into motion and move.
|
||||
velocity = move_and_slide(velocity, Vector2.UP)
|
||||
|
||||
if is_on_floor():
|
||||
on_air_time = 0
|
||||
|
||||
if jumping and velocity.y > 0:
|
||||
# If falling, no longer jumping
|
||||
# If falling, no longer jumping.
|
||||
jumping = false
|
||||
|
||||
if on_air_time < JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping:
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
[sub_resource type="RectangleShape2D" id=1]
|
||||
extents = Vector2( 7, 7 )
|
||||
|
||||
[node name="player" type="KinematicBody2D"]
|
||||
[node name="Player" type="KinematicBody2D"]
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="sprite" type="Sprite" parent="."]
|
||||
[node name="Sprite" type="Sprite" parent="."]
|
||||
texture = ExtResource( 2 )
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
|
||||
@@ -16,7 +16,7 @@ _global_script_class_icons={
|
||||
[application]
|
||||
|
||||
config/name="Kinematic Character"
|
||||
run/main_scene="res://colworld.tscn"
|
||||
run/main_scene="res://world.tscn"
|
||||
config/icon="res://icon.png"
|
||||
|
||||
[display]
|
||||
@@ -37,26 +37,27 @@ singletons=[ ]
|
||||
jump={
|
||||
"deadzone": 0.5,
|
||||
"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)
|
||||
]
|
||||
}
|
||||
move_bottom={
|
||||
"deadzone": 0.5,
|
||||
"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)
|
||||
, 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)
|
||||
, 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)
|
||||
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
|
||||
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
|
||||
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
|
||||
]
|
||||
}
|
||||
move_left={
|
||||
"deadzone": 0.5,
|
||||
"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)
|
||||
, 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)
|
||||
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null)
|
||||
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null)
|
||||
]
|
||||
}
|
||||
move_right={
|
||||
"deadzone": 0.5,
|
||||
"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)
|
||||
]
|
||||
}
|
||||
move_up={
|
||||
"deadzone": 0.5,
|
||||
"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)
|
||||
, 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)
|
||||
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null)
|
||||
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
@@ -64,6 +65,10 @@ move_up={
|
||||
|
||||
multithread/thread_rid_pool_prealloc=60
|
||||
|
||||
[physics]
|
||||
|
||||
2d/default_gravity=500
|
||||
|
||||
[rendering]
|
||||
|
||||
environment/default_clear_color=Color( 0.156, 0.1325, 0.25, 1 )
|
||||
|
||||
6
2d/kinematic_character/world.gd
Normal file
6
2d/kinematic_character/world.gd
Normal file
@@ -0,0 +1,6 @@
|
||||
extends Node2D
|
||||
|
||||
func _on_princess_body_enter(body):
|
||||
# The name of this editor-generated callback is unfortunate.
|
||||
if body.get_name() == "Player":
|
||||
$WinText.show()
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user