mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 23:10:08 +01:00
Platformer 2D: Simplify controller with proper is_on_floor usage
This commit is contained in:
@@ -3,7 +3,6 @@ extends KinematicBody2D
|
||||
const GRAVITY_VEC = Vector2(0, 900)
|
||||
const FLOOR_NORMAL = Vector2(0, -1)
|
||||
const SLOPE_SLIDE_STOP = 25.0
|
||||
const MIN_ONAIR_TIME = 0.1
|
||||
const WALK_SPEED = 250 # pixels/sec
|
||||
const JUMP_SPEED = 480
|
||||
const SIDING_CHANGE_SPEED = 10
|
||||
@@ -11,41 +10,35 @@ const BULLET_VELOCITY = 1000
|
||||
const SHOOT_TIME_SHOW_WEAPON = 0.2
|
||||
|
||||
var linear_vel = Vector2()
|
||||
var onair_time = 0 #
|
||||
var on_floor = false
|
||||
var shoot_time=99999 #time since last shot
|
||||
var shoot_time = 99999 # time since last shot
|
||||
|
||||
var anim=""
|
||||
var anim = ""
|
||||
|
||||
#cache the sprite here for fast access (we will set scale to flip it often)
|
||||
# cache the sprite here for fast access (we will set scale to flip it often)
|
||||
onready var sprite = $sprite
|
||||
|
||||
func _physics_process(delta):
|
||||
#increment counters
|
||||
|
||||
onair_time += delta
|
||||
# Increment counters
|
||||
shoot_time += delta
|
||||
|
||||
### MOVEMENT ###
|
||||
|
||||
# Apply Gravity
|
||||
# Apply gravity
|
||||
linear_vel += delta * GRAVITY_VEC
|
||||
# Move and Slide
|
||||
# Move and slide
|
||||
linear_vel = move_and_slide(linear_vel, FLOOR_NORMAL, SLOPE_SLIDE_STOP)
|
||||
# Detect Floor
|
||||
if is_on_floor():
|
||||
onair_time = 0
|
||||
|
||||
on_floor = onair_time < MIN_ONAIR_TIME
|
||||
# Detect if we are on floor - only works if called *after* move_and_slide
|
||||
var on_floor = is_on_floor()
|
||||
|
||||
### CONTROL ###
|
||||
|
||||
# Horizontal Movement
|
||||
# Horizontal movement
|
||||
var target_speed = 0
|
||||
if Input.is_action_pressed("move_left"):
|
||||
target_speed += -1
|
||||
target_speed -= 1
|
||||
if Input.is_action_pressed("move_right"):
|
||||
target_speed += 1
|
||||
target_speed += 1
|
||||
|
||||
target_speed *= WALK_SPEED
|
||||
linear_vel.x = lerp(linear_vel.x, target_speed, 0.1)
|
||||
@@ -58,10 +51,10 @@ func _physics_process(delta):
|
||||
# Shooting
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
var bullet = preload("res://bullet.tscn").instance()
|
||||
bullet.position = $sprite/bullet_shoot.global_position #use node for shoot position
|
||||
bullet.position = $sprite/bullet_shoot.global_position # use node for shoot position
|
||||
bullet.linear_velocity = Vector2(sprite.scale.x * BULLET_VELOCITY, 0)
|
||||
bullet.add_collision_exception_with(self) # don't want player to collide with bullet
|
||||
get_parent().add_child(bullet) #don't want bullet to move with me, so add it as child of parent
|
||||
get_parent().add_child(bullet) # don't want bullet to move with me, so add it as child of parent
|
||||
$sound_shoot.play()
|
||||
shoot_time = 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user