1
0
Fork 0
1bit-godot-course/metroidvania/Scenes/Player/Player.gd

124 lines
3.7 KiB
GDScript

extends KinematicBody2D
const DustEffect = preload("res://Scenes/Effects/DustEffect.tscn")
const PlayerBullet = preload("res://Scenes/Player/PlayerBullet.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
export (int) var bullet_speed = 250
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
onready var playerGun = $Sprite/PlayerGun
onready var muzzle = $Sprite/PlayerGun/Sprite/Muzzle
onready var fireBulletTimer = $FireBulletTimer
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()
if Input.is_action_pressed("fire") and fireBulletTimer.time_left == 0:
fire_bullet()
func fire_bullet():
var bullet = Utils.instance_scene_on_main(PlayerBullet, muzzle.global_position)
bullet.velocity = Vector2.RIGHT.rotated(playerGun.rotation) * bullet_speed
bullet.velocity.x *= sprite.scale.x # Flip left/right depending on players direction
bullet.rotation = bullet.velocity.angle()
fireBulletTimer.start()
func create_dust_effect():
var dust_position = global_position
dust_position.x += rand_range(-4, 4)
Utils.instance_scene_on_main(DustEffect, dust_position)
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):
sprite.scale.x = sign(get_local_mouse_position().x)
animation.playback_speed = 1
if input_vector.x != 0:
animation.playback_speed = sign(input_vector.x * sprite.scale.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