aboutsummaryrefslogtreecommitdiff
path: root/client/scripts/player.gd
diff options
context:
space:
mode:
authornokoe <nokoe@mailbox.org>2024-06-21 15:20:17 +0200
committermetamuffin <metamuffin@disroot.org>2024-06-23 19:27:04 +0200
commitd6a0899e9641c68b96463669bd655629eb430311 (patch)
treef51fbedad2a303035a414d446b4222b1d997fa6d /client/scripts/player.gd
parent68a671883983252edcc28fced0279eb37e6ba297 (diff)
downloadhurrycurry-d6a0899e9641c68b96463669bd655629eb430311.tar
hurrycurry-d6a0899e9641c68b96463669bd655629eb430311.tar.bz2
hurrycurry-d6a0899e9641c68b96463669bd655629eb430311.tar.zst
multiplayer movement
Diffstat (limited to 'client/scripts/player.gd')
-rw-r--r--client/scripts/player.gd98
1 files changed, 9 insertions, 89 deletions
diff --git a/client/scripts/player.gd b/client/scripts/player.gd
index c4cec300..3ad77322 100644
--- a/client/scripts/player.gd
+++ b/client/scripts/player.gd
@@ -1,96 +1,16 @@
+class_name Player
extends Node3D
const PLAYER_SIZE: float = 0.4
const SPEED: float = 25.
-@export var map: Node3D
-@export var camera: FollowCamera
+var mesh = preload("res://scenes/player.tscn").instantiate()
-var facing = Vector2(1, 0)
-var velocity = Vector2(0, 0)
+func _init(id: int, new_name: String, pos: Vector2, _character: int):
+ add_child(mesh)
+ position = Vector3(pos.x, 0, pos.y)
+ name = new_name
-func _process(delta):
- var input = Vector2(Input.get_axis("left", "right"), Input.get_axis("forward", "backwards")).normalized()
- input = input.rotated(-camera.angle_target)
-
- if input.length() > 0.1:
- facing = lerp_vector2_exp(facing, input, delta * 10.)
-
- self.rotation.y = facing.angle()
-
- velocity.x += input.x * delta * 0.5
- velocity.y += input.y * delta * 0.5
-
- position = Vector3(
- position.x + (velocity.x * delta * SPEED),
- 0,
- position.z + (velocity.y * delta * SPEED)
- )
-
- # collide
- collide(delta)
-
- velocity = lerp_vector2_exp(velocity, Vector2(0, 0), delta * 5.)
-
-func collide(_delta: float):
- for i in map.get_children():
- if is_instance_of(i, FullTile):
- var tile: FullTile = i
- var d = aabb_circle_distance(
- tile.position.x,
- tile.position.z,
- tile.position.x + 1,
- tile.position.z + 1,
- position.x,
- position.z
- )
-
- if d > PLAYER_SIZE:
- continue
-
- var h = 0.01
- var d_sample_x = aabb_circle_distance(
- tile.position.x,
- tile.position.z,
- tile.position.x + 1,
- tile.position.z + 1,
- position.x + h,
- position.z
- )
- var d_sample_y = aabb_circle_distance(
- tile.position.x,
- tile.position.z,
- tile.position.x + 1,
- tile.position.z + 1,
- position.x,
- position.z + h
- )
- var grad_x = (d_sample_x - d) / h
- var grad_y = (d_sample_y - d) / h
-
- position.x += (PLAYER_SIZE - d) * grad_x
- position.z += (PLAYER_SIZE - d) * grad_y
-
- var vdotn = (grad_x * velocity.x) + (grad_y * velocity.y)
-
- velocity.x -= grad_x * vdotn
- velocity.y -= grad_y * vdotn
- # TODO: Player collisions
-
-func lerp_vector2_exp(current: Vector2, target: Vector2, delta: float) -> Vector2:
- return Vector2(
- target.x + (current.x - target.x) * exp( - delta),
- target.y + (current.y - target.y) * exp( - delta)
- )
-
-func aabb_circle_distance(
- min_x: float,
- min_y: float,
- max_x: float,
- max_y: float,
- px: float,
- py: float
-) -> float:
- var dx = px - max(min_x, min(max_x, px))
- var dy = py - max(min_y, min(max_y, py))
- return sqrt(dx * dx + dy * dy)
+func update_position(new_position: Vector2, new_rotation: float):
+ position = Vector3(new_position.x, 0, new_position.y)
+ rotation.y = new_rotation