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

77 lines
2.2 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
var velocity = Vector2.ZERO
var grip = 1.0
var stamina = MAX_STAMINA
var speed_boost = 0
onready var world = $"/root/scene/world" as GameWorld
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):
2020-07-06 12:38:05 +00:00
var 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"))
if motion.length() > EPSILON:
$Sprite/AnimationTree.set("parameters/direction/blend_position", motion)
2020-07-06 19:58:42 +00:00
# Check sprinting
var speed = BASE_SPEED
if Input.is_action_pressed("sprint"):
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
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 stamina+EPSILON < MAX_STAMINA:
draw_circle_arc_poly(Vector2(-10, -30), 6, 0, stamina/MAX_STAMINA * 360, Color.orange)
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)