Skip to content

Commit

Permalink
Following tail
Browse files Browse the repository at this point in the history
- tail that follows you
- grows as you eat dots
- uses tweening
- is pretty cool
  • Loading branch information
MaxwellDexter committed Mar 28, 2020
1 parent a25a0a8 commit 98bfa69
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 12 deletions.
8 changes: 7 additions & 1 deletion Nodes/Game.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=6 format=2]

[ext_resource path="res://Images/space-wallpaper-5.jpg" type="Texture" id=1]
[ext_resource path="res://Nodes/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://Scripts/CameraFollow.gd" type="Script" id=3]
[ext_resource path="res://Nodes/Pickup Spawner.tscn" type="PackedScene" id=4]
[ext_resource path="res://Scripts/TailHolder.gd" type="Script" id=5]

[node name="Game" type="Node2D"]

Expand All @@ -13,6 +14,7 @@ texture = ExtResource( 1 )

[node name="Player" parent="." instance=ExtResource( 2 )]
position = Vector2( 44.42, 0 )
pickups_per_tail = 100

[node name="MainCamera" type="Camera2D" parent="."]
current = true
Expand All @@ -23,3 +25,7 @@ script = ExtResource( 3 )
[node name="Pickup Spawner" parent="." instance=ExtResource( 4 )]
space_between_pickups = 50
spawn_radius = 2000

[node name="TailHolder" type="Node2D" parent="."]
script = ExtResource( 5 )
offset = 30
11 changes: 8 additions & 3 deletions Nodes/Player.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=5 format=2]

[ext_resource path="res://Scripts/Player.gd" type="Script" id=1]
[ext_resource path="res://icon.png" type="Texture" id=2]
[ext_resource path="res://Textures/temp_player.png" type="Texture" id=2]
[ext_resource path="res://Scripts/SpriteSpin.gd" type="Script" id=3]

[sub_resource type="CircleShape2D" id=1]
radius = 50.0
Expand All @@ -22,7 +23,11 @@ pickup_points = 10
shape = SubResource( 1 )

[node name="Sprite" type="Sprite" parent="."]
scale = Vector2( 0.2, 0.2 )
editor/display_folded = true
texture = ExtResource( 2 )
script = ExtResource( 3 )
return_time = 0.5

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

[node name="Timer" type="Timer" parent="."]
12 changes: 12 additions & 0 deletions Nodes/Tail.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://Scripts/Tail.gd" type="Script" id=1]
[ext_resource path="res://Textures/temp_pickup.png" type="Texture" id=2]

[node name="Tail" type="Node2D"]
script = ExtResource( 1 )

[node name="Sprite" type="Sprite" parent="."]
modulate = Color( 0.313726, 1, 0.356863, 1 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 2 )
1 change: 0 additions & 1 deletion Scripts/PickupSpawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ func spawn_them_all():

func pickup_deleted():
current_pickups -= 1
print(current_pickups)
if current_pickups < 1:
spawn_them_all()
16 changes: 9 additions & 7 deletions Scripts/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export (float) var drag_release_time
var dragging
var holding_pos
onready var timer_drag_release = get_node("Timer")
onready var sprite = get_node("Sprite")

# score stuff
export (int) var pickup_points
export (int) var pickups_per_tail
var total_score

# camera stuff
Expand All @@ -34,10 +36,9 @@ func _ready():

func _physics_process(delta):
# rotation
if (dragging):
rotate(linear_velocity.length() * delta)
else:
look_at(get_transform().get_origin() + linear_velocity.normalized() * delta)
if dragging:
sprite.rotate((linear_velocity.x + linear_velocity.y) * delta * 0.05)
look_at(get_transform().get_origin() + linear_velocity.normalized() * delta)

# moving to touch
if holding_pos != null and not dragging:
Expand All @@ -61,14 +62,16 @@ func drag(event):

dragging = true
hold_this(event.position)
start_timer(drag_release_time * normalised_speed.length())
var time = drag_release_time * normalised_speed.length()
start_timer(time)

func hold_this(pos):
holding_pos = pos

# signal method
func release_timeout():
dragging = false
sprite.return_to_zero()

func start_timer(time):
timer_drag_release.set_wait_time(time)
Expand All @@ -78,5 +81,4 @@ func get_position_on_screen(pos):
return camera.get_camera_screen_center() - viewport_size/2 + pos

func add_score():
total_score += pickup_points
print(total_score)
total_score += pickup_points
30 changes: 30 additions & 0 deletions Scripts/SpriteSpin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extends Sprite

export (float) var return_time
var tween

func _ready():
tween = get_node("Tween")

func return_to_zero():
var tau = 6.283185

# setting it back to zero
var rots = rotation / tau
var floored_rotation = float(int(rots)) * tau
var scaled_rot = rotation - floored_rotation
set_rotation(scaled_rot)

# choose which way to turn
rots = rotation / tau
var desired_radians
if rots > 1 or rots < -1:
print("oh shit my guy, rotations for the player sprite was greater than one! was: ", rots)
if rots > 0.5 or rots < -0.5:
desired_radians = tau
else:
desired_radians = 0

# tween there
tween.interpolate_property(self, "rotation", rotation, desired_radians, return_time, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
tween.start()
22 changes: 22 additions & 0 deletions Scripts/Tail.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends Node2D

const SMOOTH_SPEED = 10

var follow_node
var offset
var speed_multiplier

func set_tail(player_node, player_offset, index):
follow_node = player_node
offset = player_offset
speed_multiplier = index

func _physics_process(delta):
#var length = follow_node.linear_velocity.normalized().length()
var forward = follow_node.global_transform.basis_xform(Vector2(-1,0))
var pos = follow_node.position + forward * offset

var position_difference = pos - position
var smoothed_velocity = (position_difference * SMOOTH_SPEED * delta) / speed_multiplier

position += smoothed_velocity
24 changes: 24 additions & 0 deletions Scripts/TailHolder.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extends Node2D

var tails = []
onready var tail = preload("res://Nodes/Tail.tscn")
export (int) var offset
var player

# maybe apply impulse??

func _ready():
player = get_parent().get_node("Player")

func _process(delta):
# totalscore / (points * pickup)
var tail_points = int(player.total_score / (player.pickup_points * player.pickups_per_tail))
if tails.size() < tail_points:
add_tail()

func add_tail():
var inst = tail.instance()
tails.append(inst)
inst.position = player.position
inst.set_tail(player, offset * tails.size(), tails.size())
add_child(inst)
Binary file added Textures/temp_player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Textures/temp_player.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/temp_player.png-e2d962cc47d0441e13206b3b1d4b953c.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://Textures/temp_player.png"
dest_files=[ "res://.import/temp_player.png-e2d962cc47d0441e13206b3b1d4b953c.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

0 comments on commit 98bfa69

Please sign in to comment.