runic-godot/data/character/character.gd

102 lines
2.6 KiB
GDScript

extends CharacterBody2D
var base_speed = 150
var sprint_speed = 300
var exausted_speed = 80
var speed = base_speed
var accel = 10
var immobile = false
var is_sprinting = false
var inventory = {
"points": 0,
"stone": 0,
"wood": 0,
"food": 0
}
var status = {
"health": 100,
"stamina": 100,
"exausted": false,
"exaust_speed": 1,
"stamina_regen_speed": 0.5,
"level": 1
}
var current_tool = "none"
func next_level_points():
return status.level*10
func change_tool(tool):
current_tool = tool
$body_pivot/body/tool.texture = load(game_data.tools[tool].image)
$body_pivot/body/tool.position = game_data.tools[tool].offset
func _ready():
change_tool("stick") # This is when the starting tool is set
func _physics_process(delta):
var pre_velocity = Vector2(0, 0)
if !immobile:
pre_velocity = Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = lerp(velocity, pre_velocity * speed, accel*delta)
move_and_slide()
func _process(_delta):
if Input.is_action_pressed("game_interact"):
if !$animation.is_playing():
$animation.play("hit")
if $body_pivot/raycast.is_colliding():
var hit_object = $body_pivot/raycast.get_collider()
if hit_object.type == "resource":
inventory[hit_object.resource_type] += 1
inventory.points += 1
hit_object.get_node("animation").play("hit")
is_sprinting = Input.is_action_pressed("move_sprint") && !status.exausted and velocity.distance_to(Vector2.ZERO) > 0
if is_sprinting:
speed = sprint_speed
status.stamina -= status.exaust_speed
if status.stamina <= 0:
is_sprinting = false
status.exausted = true
else:
if status.stamina < 100:
status.stamina += status.stamina_regen_speed
if status.exausted:
speed = exausted_speed
else:
speed = base_speed
if status.stamina == 100 and status.exausted:
status.exausted = false
# Update inventory HUD
$hud/inventory/stone_label.text = str(inventory.stone)
$hud/inventory/wood_label.text = str(inventory.wood)
$hud/level/points.text = str(inventory.points) + "/" + str(next_level_points())
$hud/level/level_bar.value = inventory.points
# Update status HUD
$hud/bars/health.value = status.health
$hud/bars/stamina.value = status.stamina
$hud/level/level.text = "Level " + str(status.level)
$hud/level/level_bar.max_value = next_level_points()
# Level up
if inventory.points >= (next_level_points()):
inventory.points -= next_level_points()
status.level += 1
level_up()
func level_up():
pass
func _input(event):
if event is InputEventMouseMotion:
var mouse_pos = get_global_mouse_position()
$body_pivot.rotation = atan2((mouse_pos.y-position.y), (mouse_pos.x-position.x)) + (PI * 0.5)