GMTK-2024/gmtk_2024/scripts/player_movement.gd

172 lines
4.8 KiB
GDScript3
Raw Normal View History

2024-08-16 21:03:06 +02:00
extends CharacterBody2D
@export var sfx : AudioStreamPlayer2D
2024-08-19 17:18:09 +02:00
@export var sfx_large : AudioStreamPlayer2D
2024-08-17 22:26:53 +08:00
@export var speed = 340
@export var gravity = 50
var jump_count = 1
@export var jump_strength = 100
2024-08-18 23:30:19 +02:00
@export var jump_strength_large = 400
2024-08-17 22:26:53 +08:00
var is_touching_floor : bool = true
2024-08-17 22:49:59 +08:00
var jump_buffer_timer : float
2024-08-18 15:43:21 +02:00
var coyote_timer : float = 0.2
var input_direction = 0
2024-08-18 20:13:49 +02:00
@export var max_link_distance: float = 70.0
2024-08-18 15:43:21 +02:00
var data_link: Line2D
2024-08-18 16:29:21 +02:00
var target_scale
2024-08-18 15:43:21 +02:00
2024-08-18 20:13:49 +02:00
@export var scale_duration: float = 0.5
var current_selected = "none"
var target_scale_player: Vector2 = Vector2(1, 1)
2024-08-19 04:05:02 +02:00
var cursor_scale_up = preload("res://textures/cursor_scale_up.png")
var cursor_scale_down = preload("res://textures/cursor_scale_down.png")
2024-08-18 15:43:21 +02:00
func _ready():
data_link = $data_link
2024-08-16 21:03:06 +02:00
2024-08-20 04:52:54 +02:00
func _unhandled_input(event: InputEvent) -> void:
2024-08-18 20:13:49 +02:00
if event.is_action_pressed("click"):
var nearest_block = find_nearest_block()
if nearest_block:
if nearest_block.scale == Vector2(0.5, 0.5) and self.scale == Vector2(2.0, 2.0):
nearest_block.scale_up()
scale_down_player()
elif nearest_block.scale == Vector2(1.0, 1.0) and self.scale == Vector2(1.0, 1.0):
nearest_block.scale_down()
scale_up_player()
2024-08-18 23:30:19 +02:00
else:
var nearest_enemy = find_nearest_enemy()
if nearest_enemy:
if nearest_enemy.scale == Vector2(1, 1) and self.scale == Vector2(2.0, 2.0):
nearest_enemy.scale = Vector2(2,2)
scale_down_player()
elif nearest_enemy.scale == Vector2(2, 2) and self.scale == Vector2(1.0, 1.0):
nearest_enemy.scale = Vector2(1,1)
scale_up_player()
2024-08-18 20:13:49 +02:00
2024-08-17 22:26:53 +08:00
func get_input(delta):
2024-08-16 22:09:19 +02:00
var left = Input.is_action_pressed("player_left")
var right = Input.is_action_pressed("player_right")
if Input.is_action_just_pressed("r"):
get_tree().reload_current_scene()
2024-08-18 15:43:21 +02:00
2024-08-16 22:09:19 +02:00
if left and right:
2024-08-17 22:26:53 +08:00
input_direction = 0
2024-08-16 22:09:19 +02:00
elif left:
input_direction = -1
elif right:
input_direction = 1
2024-08-17 22:49:59 +08:00
else:
2024-08-16 22:09:19 +02:00
input_direction = 0
2024-08-18 15:43:21 +02:00
2024-08-16 21:03:06 +02:00
velocity.x = input_direction * speed
2024-08-18 15:43:21 +02:00
2024-08-17 22:26:53 +08:00
if is_on_floor():
is_touching_floor = true
jump_count = 1
coyote_timer = 0.2
2024-08-17 22:49:59 +08:00
if jump_buffer_timer > 0:
Jump()
2024-08-18 15:43:21 +02:00
2024-08-17 22:26:53 +08:00
if not is_on_floor():
2024-08-17 22:49:59 +08:00
if jump_buffer_timer > 0:
jump_buffer_timer -= delta
2024-08-17 22:26:53 +08:00
if coyote_timer > 0.0:
coyote_timer -= delta
else:
jump_count = 0
2024-08-18 15:43:21 +02:00
if Input.is_action_just_pressed("player_jump"):
2024-08-17 22:49:59 +08:00
if jump_count > 0:
Jump()
else:
2024-08-18 15:43:21 +02:00
jump_buffer_timer = 0.2
elif Input.is_action_pressed("player_jump"):
velocity.y += -1 * 25
2024-08-16 21:03:06 +02:00
func _physics_process(delta):
2024-08-17 22:26:53 +08:00
get_input(delta)
2024-08-16 21:42:27 +02:00
velocity.y += gravity
2024-08-16 21:03:06 +02:00
move_and_slide()
2024-08-18 23:30:19 +02:00
if(get_slide_collision_count() > 0):
for i in get_slide_collision_count():
var box = get_slide_collision(i).get_collider() as Box
if scale.x == 2:
if box and velocity.y >= 0:
2024-08-19 18:33:47 +02:00
box.velocity.x = velocity.x*1.2
2024-08-18 23:30:19 +02:00
else:
if box and velocity.y >= 0 and box.scale.x < 2:
box.velocity.x = velocity.x*0.8
2024-08-18 15:43:21 +02:00
update_data_link()
2024-08-19 04:05:02 +02:00
update_cursor()
2024-08-17 22:49:59 +08:00
func Jump():
2024-08-18 23:30:19 +02:00
if scale.x == 1:
velocity.y = -1 * jump_strength
2024-08-19 17:18:09 +02:00
sfx.play()
2024-08-18 23:30:19 +02:00
else :
velocity.y = -1 * jump_strength_large
2024-08-19 17:18:09 +02:00
sfx_large.play()
2024-08-17 22:49:59 +08:00
jump_count = 0
is_touching_floor = false
2024-08-17 17:33:34 +02:00
func _on_hurtbox_body_entered(body: Node2D) -> void:
get_tree().reload_current_scene()
2024-08-18 02:48:09 +02:00
2024-08-18 15:43:21 +02:00
func update_data_link():
var nearest_block = find_nearest_block()
if nearest_block:
data_link.visible = true
data_link.set_point_position(0, Vector2.ZERO)
data_link.set_point_position(1, get_transform().affine_inverse() * nearest_block.position)
else:
data_link.visible = false
2024-08-18 02:48:09 +02:00
2024-08-19 04:05:02 +02:00
func update_cursor():
var nearest_block = find_nearest_block()
if nearest_block:
if nearest_block.scale == Vector2(0.5, 0.5) and self.scale == Vector2(2.0, 2.0):
Input.set_custom_mouse_cursor(cursor_scale_up)
elif nearest_block.scale == Vector2(1.0, 1.0) and self.scale == Vector2(1.0, 1.0):
Input.set_custom_mouse_cursor(cursor_scale_down)
2024-08-18 15:43:21 +02:00
func find_nearest_block() -> Node2D:
if not get_tree():
return null
2024-08-18 15:43:21 +02:00
var closest_distance = max_link_distance
var closest_block: Node2D = null
for block in get_tree().get_nodes_in_group("scalable_blocks"):
var distance = global_position.distance_to(block.global_position)
if distance <= closest_distance:
closest_distance = distance
closest_block = block
return closest_block
2024-08-18 20:13:49 +02:00
2024-08-18 23:30:19 +02:00
func find_nearest_enemy() -> Node2D:
var closest_distance = max_link_distance
var closest_block: Node2D = null
for block in get_tree().get_nodes_in_group("enemy"):
var distance = global_position.distance_to(block.global_position)
if distance <= closest_distance:
closest_distance = distance
closest_block = block
return closest_block
2024-08-18 20:13:49 +02:00
func start_scaling(target_node: Node2D, scale_value: Vector2):
var tween = create_tween()
tween.tween_property(target_node, "scale", scale_value, scale_duration)
func scale_up_player():
target_scale_player = Vector2(2.0, 2.0)
start_scaling(self, target_scale_player)
func scale_down_player():
target_scale_player = Vector2(1.0, 1.0)
start_scaling(self, target_scale_player)