Player gun, bullet and its collisiions. Refactor.
This commit is contained in:
parent
0dd3f24030
commit
ffb7c8bf99
28 changed files with 562 additions and 131 deletions
|
@ -1,107 +0,0 @@
|
|||
extends KinematicBody2D
|
||||
|
||||
const DustEffect = preload("res://DustEffect.tscn")
|
||||
|
||||
export (int) var acceleration = 512
|
||||
export (int) var max_speed = 64
|
||||
export (float) var friction = 0.25
|
||||
export (int) var gravity = 200
|
||||
export (int) var jump_force = 128
|
||||
export (int) var max_slope = 46
|
||||
|
||||
var motion = Vector2.ZERO
|
||||
var snap_vector = Vector2.ZERO
|
||||
var just_jumped= false
|
||||
|
||||
onready var sprite = $Sprite
|
||||
onready var animation = $Animation
|
||||
onready var coyoteJumpTimer = $CoyoteJumpTimer
|
||||
|
||||
func _physics_process(delta):
|
||||
just_jumped = false
|
||||
var input_vector = get_input_vector()
|
||||
apply_horizontal_force(input_vector, delta)
|
||||
apply_friction(input_vector)
|
||||
update_snap_vector()
|
||||
jump_check()
|
||||
apply_gravity(delta)
|
||||
update_animations(input_vector)
|
||||
move()
|
||||
|
||||
func create_dust_effect():
|
||||
var dustPosition = global_position
|
||||
dustPosition.x += rand_range(-4, 4)
|
||||
var dustEffect = DustEffect.instance()
|
||||
dustEffect.global_position = dustPosition
|
||||
get_tree().current_scene.add_child(dustEffect)
|
||||
|
||||
func get_input_vector():
|
||||
var input_vector = Vector2.ZERO
|
||||
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
|
||||
return input_vector
|
||||
|
||||
func apply_horizontal_force(input_vector, delta):
|
||||
if input_vector.x != 0:
|
||||
motion.x += input_vector.x * acceleration * delta
|
||||
motion.x = clamp(motion.x, -max_speed, max_speed) # Set the max motion
|
||||
|
||||
func apply_friction(input_vector):
|
||||
if input_vector.x == 0 and is_on_floor():
|
||||
motion.x = lerp(motion.x, 0, friction)
|
||||
|
||||
func update_snap_vector():
|
||||
if is_on_floor():
|
||||
snap_vector = Vector2.DOWN
|
||||
|
||||
func jump_check():
|
||||
if is_on_floor() or coyoteJumpTimer.time_left > 0:
|
||||
if Input.is_action_just_pressed("ui_select"):
|
||||
motion.y = -jump_force
|
||||
snap_vector = Vector2.ZERO
|
||||
just_jumped = true
|
||||
else:
|
||||
if Input.is_action_just_released("ui_select") and motion.y < -jump_force/2:
|
||||
motion.y = motion.y/2
|
||||
|
||||
func apply_gravity(delta):
|
||||
if not is_on_floor():
|
||||
motion.y += gravity * delta
|
||||
motion.y = min(motion.y, jump_force)
|
||||
|
||||
func update_animations(input_vector):
|
||||
if input_vector.x != 0:
|
||||
# Reverses the sprite if the direction is -x ?
|
||||
sprite.scale.x = sign(input_vector.x)
|
||||
animation.play("Run")
|
||||
else:
|
||||
animation.play("Idle")
|
||||
|
||||
if not is_on_floor():
|
||||
animation.play("Jump")
|
||||
|
||||
func move():
|
||||
var was_on_air = not is_on_floor()
|
||||
var was_on_floor = is_on_floor()
|
||||
var last_position = position
|
||||
var last_motion = motion
|
||||
|
||||
motion = move_and_slide_with_snap(motion, snap_vector * 4, Vector2.UP, true, 4, deg2rad(max_slope), false)
|
||||
|
||||
# Just landed
|
||||
if was_on_air and is_on_floor():
|
||||
# Keep previous momentum when landing on slopes
|
||||
motion.x = last_motion.x
|
||||
create_dust_effect()
|
||||
|
||||
# Just left ground
|
||||
if was_on_floor and not is_on_floor() and not just_jumped:
|
||||
# Fixes "gap" when jumping off a cliff [TODO]
|
||||
motion.y = 0
|
||||
position.y = last_position.y
|
||||
coyoteJumpTimer.start()
|
||||
|
||||
# Prevent Sliding (hack) [NOT WORKING]
|
||||
# If we are on floor, the floor isn't moving and our motion is really tiny
|
||||
if is_on_floor() and get_floor_velocity().length() == 0 and abs(motion.x) < 1:
|
||||
position.x = last_position.x
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue