Skip to content

Commit

Permalink
Add hook-based movement prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanLovato committed Apr 25, 2019
1 parent 03f2eb3 commit 6d50ccf
Show file tree
Hide file tree
Showing 43 changed files with 721 additions and 109 deletions.
30 changes: 25 additions & 5 deletions metroidvania-prototypes/hook!/Arrow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@ extends Node2D

onready var head: = $Head
onready var tail: = $Tail
onready var tween: Tween = $Tween

var target_length: = 40.0 setget set_target_length
var hook_position: = Vector2.ZERO setget set_hook_position
var length: = 40.0 setget set_length

func set_target_length(value: float) -> void:
target_length = value
tail.points[-1].x = target_length
head.position.x = tail.points[-1].x
onready var start_length: float = head.position.x

func set_hook_position(value:Vector2) -> void:
hook_position = value
var to_target: = hook_position - global_position
self.length = to_target.length()
rotation = to_target.angle()
tween.interpolate_property(
self, 'length', length, start_length,
0.25, Tween.TRANS_QUAD, Tween.EASE_OUT)
tween.start()
visible = true


func set_length(value: float) -> void:
length = value
tail.points[-1].x = length
head.position.x = tail.points[-1].x + tail.position.x


func _on_Tween_tween_completed(object: Object, key: NodePath) -> void:
visible = false
19 changes: 10 additions & 9 deletions metroidvania-prototypes/hook!/Arrow.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,43 @@
script = ExtResource( 1 )

[node name="Tail" type="Line2D" parent="."]
points = PoolVector2Array( 70, 0, 30, 0 )
position = Vector2( 20, 0 )
points = PoolVector2Array( 40, 0, 30, 0 )
width = 8.0
default_color = Color( 0.92549, 0.756863, 0.0980392, 1 )
texture_mode = 3080292
joint_mode = 2
begin_cap_mode = 2
end_cap_mode = 2

[node name="Head" type="Line2D" parent="."]
position = Vector2( 70, 0 )
rotation = -0.00175857
position = Vector2( 68, 0 )
points = PoolVector2Array( -10.0176, 9.9824, 0, 0, -9.9824, -10.0176 )
width = 8.0
default_color = Color( 0.92549, 0.756863, 0.0980392, 1 )
texture_mode = 3080292
joint_mode = 2
begin_cap_mode = 2
end_cap_mode = 2

[node name="Tail2" type="Line2D" parent="."]
position = Vector2( -122, 0 )
visible = false
position = Vector2( -112, 0.031641 )
points = PoolVector2Array( 70, 0, 50, 0 )
width = 8.0
default_color = Color( 0.92549, 0.756863, 0.0980392, 1 )
texture_mode = 3080292
joint_mode = 2
begin_cap_mode = 2
end_cap_mode = 2

[node name="Tail3" type="Line2D" parent="."]
position = Vector2( -122, 0 )
visible = false
position = Vector2( -112, 0.031641 )
points = PoolVector2Array( 50, 10, 50, -10 )
width = 8.0
default_color = Color( 0.92549, 0.756863, 0.0980392, 1 )
texture_mode = 3080292
joint_mode = 2
begin_cap_mode = 2
end_cap_mode = 2

[node name="Tween" type="Tween" parent="."]

[connection signal="tween_completed" from="Tween" to="." method="_on_Tween_tween_completed"]
101 changes: 91 additions & 10 deletions metroidvania-prototypes/hook!/Character.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
extends KinematicBody2D

onready var hook: = $Hook

enum {
IDLE,
RUN,
AIR,
}
var states_strings: = {
IDLE: "idle",
RUN: "run",
AIR: "air",
}

const FLOOR_NORMAL: = Vector2(0, -1)

export var speed_normal: = 400.0
export var speed_sprint: = 700.0
export var speed_ground: = 500.0
export var jump_force: = 900.0
export var gravity: = 3600.0
export var gravity: = 3000.0

export var air_acceleration: = 3000.0
export var air_max_speed: = 700.0
export var air_max_speed_vertical: = Vector2(-1500.0, 1500.0)

var _velocity: = Vector2.ZERO
var _info_dict: = {} setget _set_info_dict

var _state: int = IDLE
onready var _transitions: = {
IDLE: [RUN, AIR],
RUN: [IDLE, AIR],
AIR: [IDLE],
}


func _ready() -> void:
hook.connect("hooked_onto_target", self, "_on_Hook_hooked_onto_target")

var _velocity: = Vector2()

func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("jump") and is_on_floor():
Expand All @@ -16,12 +45,64 @@ func _unhandled_input(event: InputEvent) -> void:

func _physics_process(delta):
var move_direction: = get_move_direction()
var speed: = speed_sprint if Input.is_action_pressed("sprint") else speed_normal
_velocity.x = move_direction * speed

# Horizontal movement
match _state:
IDLE:
if move_direction.x:
change_state(RUN)

RUN:
if not move_direction.x:
change_state(IDLE)
_velocity.x = move_direction.x * speed_ground

AIR:
_velocity.x += air_acceleration * move_direction.x * delta
if abs(_velocity.x) > air_max_speed:
_velocity.x = air_max_speed * sign(_velocity.x)

# Vertical movement
_velocity.y += gravity * delta
var resulting_velocity: = move_and_slide(_velocity, FLOOR_NORMAL)
_velocity.y = resulting_velocity.y
_velocity.y = clamp(_velocity.y, air_max_speed_vertical.x, air_max_speed_vertical.y)
var slide_velocity: = move_and_slide(_velocity, FLOOR_NORMAL)
_velocity.y = slide_velocity.y
self._info_dict["velocity"] = _velocity

# State updates after movement
if is_on_floor() and _state == AIR:
change_state(IDLE)
if not is_on_floor() and _state in [IDLE, RUN]:
change_state(AIR)


func change_state(target_state:int) -> void:
if not target_state in _transitions[_state]:
return
_state = target_state
enter_state()
Events.emit_signal("player_state_changed", states_strings[_state])


func enter_state() -> void:
match _state:
IDLE:
_velocity.x = 0.0
_:
return


func get_move_direction() -> Vector2:
return Vector2(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
Input.get_action_strength("aim_down") - Input.get_action_strength("aim_up")
)


func _set_info_dict(value:Dictionary) -> void:
_info_dict = value
Events.emit_signal("player_info_updated", _info_dict)


func get_move_direction() -> float:
return Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
func _on_Hook_hooked_onto_target(force:Vector2) -> void:
_velocity += force
7 changes: 7 additions & 0 deletions metroidvania-prototypes/hook!/Character.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ position = Vector2( 0, -30 )
shape = SubResource( 1 )

[node name="Hook" parent="." instance=ExtResource( 2 )]
position = Vector2( 0, -30 )

[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2( 0, -160 )
current = true
drag_margin_h_enabled = false
drag_margin_v_enabled = false

20 changes: 20 additions & 0 deletions metroidvania-prototypes/hook!/DrawingUtils.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extends Node2D
class_name DrawingUtils

const DEFAULT_POINTS_COUNT: = 32

const COLOR_BLUE_LIGHT: = Color("09a6ca")
const COLOR_BLUE_DEEP: = Color("0046ff")

const COLOR_SUCCESS: = Color("2cb638")
const COLOR_WARNING: = Color("ff9b00")
const COLOR_ERROR: = Color("ff004e")


func draw_circle_outline(obj: CanvasItem=null, position:=Vector2.ZERO, radius:float=30.0, color:=Color(), thickness:=1.0) -> void:
var points_array: = PoolVector2Array()
for i in range(DEFAULT_POINTS_COUNT + 1):
var angle: = 2 * PI * i / DEFAULT_POINTS_COUNT
var point: = position + Vector2(cos(angle) * radius, sin(angle) * radius)
points_array.append(point)
obj.draw_polyline(points_array, color, thickness, true)
4 changes: 4 additions & 0 deletions metroidvania-prototypes/hook!/Events.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extends Node

signal player_info_updated(dict)
signal player_state_changed(state_name)
7 changes: 7 additions & 0 deletions metroidvania-prototypes/hook!/Events.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://Events.gd" type="Script" id=1]

[node name="Events" type="Node"]
script = ExtResource( 1 )

Loading

0 comments on commit 6d50ccf

Please sign in to comment.