This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyssey-old/Actors/Player/Player.gd

105 lines
2.9 KiB
GDScript3
Raw Normal View History

2020-07-06 12:38:05 +00:00
extends KinematicBody2D
2020-07-06 19:58:42 +00:00
const BASE_SPEED = 300.0
2020-07-06 12:38:05 +00:00
const EPSILON = 0.1
2020-07-06 19:58:42 +00:00
const MAX_STAMINA = 3.0
const BOOST_COEFF = 50.0
const STAMINA_RECOVER_RATE = 0.3
const NET_KEY_TRANSFORM_DELAY = 0.2
2020-07-06 19:58:42 +00:00
var velocity = Vector2.ZERO
var grip = 1.0
var stamina = MAX_STAMINA
var speed_boost = 0
var transform_update_remaining = 0.0
puppet var pup_motion = Vector2.ZERO
puppet var pup_velocity = Vector2.ZERO
puppet var pup_transform = Transform()
2020-07-06 19:58:42 +00:00
2020-07-12 15:26:40 +00:00
onready var scene = $"/root/scene"
onready var world = $"/root/scene/world"
2020-07-12 15:26:40 +00:00
onready var camera = $Camera
2020-07-06 12:38:05 +00:00
export var is_controlled = false setget set_is_controlled
func _ready():
$Camera.current = is_controlled
2020-07-06 19:58:42 +00:00
func _physics_process(delta):
var motion = Vector2.ZERO
if is_network_master():
2020-07-20 09:15:39 +00:00
if not scene.writing:
motion = Vector2(
Input.get_action_strength("ui_right")-Input.get_action_strength("ui_left"),
Input.get_action_strength("ui_down")-Input.get_action_strength("ui_up"))
# Check sprinting
var speed = BASE_SPEED
2020-07-20 09:15:39 +00:00
if Input.is_action_pressed("sprint") and not scene.writing:
if motion.length() > EPSILON and stamina > 0:
speed_boost += BOOST_COEFF * delta * ease(stamina/MAX_STAMINA, 1.1)
stamina -= delta
update()
else:
if stamina < MAX_STAMINA:
stamina += delta * STAMINA_RECOVER_RATE
update()
# Apply friction to speed boost and the boost itself, if any
if speed_boost > 0:
speed_boost /= 2
speed *= 1.0 + speed_boost
if speed_boost < EPSILON:
speed_boost = 0
# Set velocity
velocity = velocity * (1.0-grip) + motion.clamped(1.0) * speed * grip
rset("pup_motion", motion)
rset("pup_velocity", velocity)
rset("pup_transform", global_transform)
else:
velocity = pup_velocity
motion = pup_motion
if transform_update_remaining <= 0.0:
transform_update_remaining = NET_KEY_TRANSFORM_DELAY
global_transform = pup_transform
else:
transform_update_remaining -= delta
2020-07-06 12:38:05 +00:00
if motion.length() > EPSILON:
$Sprite/AnimationTree.set("parameters/direction/blend_position", motion)
2020-07-06 19:58:42 +00:00
velocity = move_and_slide(velocity)
# Check what tile I'm on
if world.map != null:
grip = 0.0
for tilemap in world.map.tilemaps:
var cell = tilemap.get_cellv(tilemap.world_to_map(transform.origin))
if cell >= 0:
grip = 1.0
2020-07-06 12:38:05 +00:00
func set_is_controlled(val):
is_controlled = val
$Camera.current = val
2020-07-06 19:58:42 +00:00
func _draw():
if is_network_master():
if stamina+EPSILON < MAX_STAMINA:
draw_circle_arc_poly(Vector2(-10, -30), 6, 0, stamina/MAX_STAMINA * 360, Color.orange)
2020-07-06 19:58:42 +00:00
func draw_circle_arc_poly(center, radius, angle_from, angle_to, color):
var nb_points = 32
var points_arc = PoolVector2Array()
points_arc.push_back(center)
var colors = PoolColorArray([color])
for i in range(nb_points + 1):
var angle_point = deg2rad(angle_from + i * (angle_to - angle_from) / nb_points - 90)
points_arc.push_back(center + Vector2(cos(angle_point), sin(angle_point)) * radius)
draw_polygon(points_arc, colors)
2020-07-20 13:04:56 +00:00
2020-07-22 15:34:04 +00:00
func hear(_event):
2020-07-21 17:47:33 +00:00
pass