# Hurry Curry! - a game about cooking # Copyright 2024 metamuffin # Copyright 2024 tpart # Copyright 2024 nokoe # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, version 3 of the License only. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # class_name ControllablePlayer extends Player const PLAYER_SPEED = 55 const PLAYER_FRICTION = 15 const BOOST_FACTOR = 2.5 const BOOST_DURATION = 0.3 const BOOST_RESTORE = 0.5 var onscreen_controls = preload("res://player/onscreen_controls/controls.tscn").instantiate() var facing := Vector2(1, 0) var velocity_ := Vector2(0, 0) var direction := Vector2(0, 0) var stamina := 0. var chat_open := false var enable_input := true var input_rotation = 0 var vibration_timer := Timer.new() var current_vibration_strength := 0. var current_vibration_change := 0. var target: Vector2i = Vector2i(0, 0) func _ready(): vibration_timer = Timer.new() vibration_timer.wait_time = 0.1 vibration_timer.timeout.connect(_on_vibration_timeout) add_child(vibration_timer) var timer = Timer.new() timer.one_shot = false timer.wait_time = 1. / 50. add_child(timer) timer.start() timer.connect("timeout", func(): if game.mp != null and game.join_state == Game.JoinState.JOINED: game.mp.send_movement(game.player_id, position_, direction, boosting) ) add_child(onscreen_controls) super() const MAX_DT = 1. / 50. func _process(delta): marker.position = G.interpolate(marker.position, marker_target, delta * 30.) while delta > 0.001: var dt = min(delta, MAX_DT) _process_movement(dt) delta -= dt super(delta) update_touch_scrolls() var moving_duration = 0 var fps_rotation_target = 0 func _process_movement(delta): var input = Input.get_vector("left", "right", "forwards", "backwards") if is_input_enabled() else Vector2.ZERO if Global.get_setting("gameplay.first_person"): fps_rotation_target += input.x * delta * 3. if abs(input.x) > 0.1: input.y -= 0.5 input.x = 0. input.y = min(input.y, 0) input = input.rotated(fps_rotation_target) else: input = input.rotated(input_rotation) var boost = Input.is_action_pressed("boost") or (Global.get_setting("gameplay.latch_boost") and boosting) # if Input.is_action_pressed("interact") or Input.is_action_just_released("interact"): input *= 0 else: target = Vector2i( int(floor(movement_base.position.x + sin(movement_base.rotation.y))), int(floor(movement_base.position.z + cos(movement_base.rotation.y))) ) if Global.get_setting("gameplay.accessible_movement"): if input.length() < 0.5: moving_duration -= delta * 2 else: moving_duration += delta moving_duration = clamp(moving_duration, 0, 1) input *= min(1, moving_duration) interact() var was_boosting = boosting direction = input update(delta, boost) if boosting and not was_boosting: Input.start_joy_vibration(0, 0, input.length(), 0.15) Input.vibrate_handheld(75, input.length() * 0.1) walking = input.length_squared() > 0.1 position_anim = position_ rotation_anim = rotation_ func update(dt: float, boost: bool): direction = direction.limit_length(1.) if direction.length() > 0.05: facing = direction + (facing - direction) * exp(-dt * 10.) if direction.length() < 0.5: direction *= 0 rotation_ = atan2(facing.x, facing.y); boost = boost and direction.length() > 0.1 boosting = boost and (boosting or stamina >= 1.0) and stamina > 0 if boosting: stamina -= dt / BOOST_DURATION else: stamina += dt / BOOST_RESTORE stamina = max(min(stamina, 1.0), 0.0) var speed = PLAYER_SPEED * (BOOST_FACTOR if boosting else 1.) velocity_ += direction * dt * speed position_ += velocity_ * dt velocity_ = velocity_ * exp(-dt * PLAYER_FRICTION) collide(dt) func collide(dt: float): for xo in range(-1, 2): for yo in range(-1, 2): var tile = Vector2i(xo, yo) + Vector2i(position_) if !game.get_tile_collision(tile): continue tile = Vector2(tile) var d = aabb_point_distance(tile, tile + Vector2.ONE, position_) if d > PLAYER_SIZE: continue var h = 0.01; var d_sample_x = aabb_point_distance(tile, tile + Vector2.ONE, position_ + Vector2(h, 0)) var d_sample_y = aabb_point_distance(tile, tile + Vector2.ONE, position_ + Vector2(0, h)) var grad = (Vector2(d_sample_x - d, d_sample_y - d)) / h position_ += (PLAYER_SIZE - d) * grad; velocity_ -= grad * grad.dot(velocity_) for player: Player in game.players.values(): var diff = position_ - player.position_ var d = diff.length() if d < 0.01: continue if d >= PLAYER_SIZE * 2: continue var norm = diff.normalized(); var f = 100 / (1 + d) velocity_.x += norm.x * f * dt velocity_.y += norm.y * f * dt func is_input_enabled() -> bool: return not game.menu.covered and enable_input func update_touch_scrolls(): # TODO: Don't call this function every frame, but only when input menu # covered value is updated if onscreen_controls.touch_enabled: onscreen_controls.visible = is_input_enabled() func aabb_point_distance(mi: Vector2, ma: Vector2, p: Vector2) -> float: return (p - p.clamp(mi, ma)).length() func update_position(_new_position: Vector2, _new_rotation: float, _new_boosting: bool): pass func progress(position__: float, speed: float, warn: bool): super(position__, speed, warn) if warn: current_vibration_strength = position__ current_vibration_change = speed var vibration_strength := pow(current_vibration_strength, 3) Input.start_joy_vibration(0, vibration_strength, 0, 0.1) Input.vibrate_handheld(100, vibration_strength) vibration_timer.start() if speed == 0: current_vibration_strength = 0. vibration_timer.stop() func _on_vibration_timeout(): current_vibration_strength = clampf(current_vibration_strength + current_vibration_change * 0.1, 0, 1) if current_vibration_strength == 0.: return var vibration_strength := pow(current_vibration_strength, 3) Input.start_joy_vibration(0, vibration_strength, 0, 0.1) Input.vibrate_handheld(100, vibration_strength) vibration_timer.start() func put_item(tile: Tile): super(tile) Input.start_joy_vibration(0, 0.1, 0.0, 0.075) Input.vibrate_handheld(75, 0.1) func take_item(tile: Tile): super(tile) Input.start_joy_vibration(0, 0.1, 0.0, 0.075) Input.vibrate_handheld(75, 0.1) func interact(): if not is_input_enabled(): return var tile = game.map.get_tile_instance(target) if tile != null: marker.set_interactive(game.get_tile_interactive(target)) marker.visible = true marker_target = tile.item_base.global_position if Input.is_action_just_pressed("interact"): game.mp.send_tile_interact(game.player_id, target, true) tile.interact() marker.set_interacting(true) elif Input.is_action_just_released("interact"): game.mp.send_tile_interact(game.player_id, target, false) marker.set_interacting(false) else: marker.visible = false