Commit 0004da18 authored by Nathan Lovato's avatar Nathan Lovato
Browse files

Port the code to typed GDscript

parent 7274db7b
......@@ -2,19 +2,19 @@
extends Node2D
onready var pillar_spawner = get_node("PillarSpawner")
onready var score_label = get_node("BackgroundLayer/ScoreLabel")
onready var pillar_spawner := get_node("PillarSpawner")
onready var score_label : Label = get_node("BackgroundLayer/ScoreLabel")
var score = 0
var score := 0
func _ready():
func _ready() -> void:
pillar_spawner.connect("pillar_checkpoint_reached", self, "increase_score")
func _unhandled_input(event):
func _unhandled_input(event) -> void:
if event.is_action_pressed("reset"):
get_tree().reload_current_scene()
func increase_score():
func increase_score() -> void:
score_label.score += 1
extends Camera2D
onready var start_offset = position
onready var start_offset := position
func _ready():
func _ready() -> void:
set_as_toplevel(true)
position = owner.position + start_offset
func _physics_process(delta):
func _physics_process(delta) -> void:
position.x = owner.position.x + start_offset.x
extends KinematicBody2D
onready var tween = get_node("Tween")
onready var sprite = get_node("Sprite")
onready var collision_shape = get_node("CollisionShape2D")
onready var tween : Tween = get_node("Tween")
onready var sprite : Sprite = get_node("Sprite")
onready var collision_shape : CollisionShape2D = get_node("CollisionShape2D")
export(float) var gravity = 4000.0
export(float) var flap_force = 1200.0
export(float) var max_fall_speed = 1200.0
export(float) var max_angular_velocity = PI
export(float) var angular_acceleration = 2.0 * PI
export(float) var speed_horizontal = 280.0
export var gravity := 4000.0
export var flap_force := 1200.0
export var max_fall_speed := 1200.0
export var max_angular_velocity := PI
export var angular_acceleration := 2.0 * PI
export var speed_horizontal := 280.0
var _velocity = Vector2(speed_horizontal, 0.0)
var _angular_velocity = 0.0
var _idle = true setget set_idle
var _target_angle = 0.0
var _velocity := Vector2(speed_horizontal, 0.0)
var _angular_velocity := 0.0
var _idle := true setget set_idle
var _target_angle := 0.0
var _active = true setget _set_active
var _active := true setget _set_active
func _ready():
func _ready() -> void:
self._idle = true
func set_idle(value):
func set_idle(value) -> void:
_idle = value
set_physics_process(not value)
func _unhandled_input(event):
func _unhandled_input(event) -> void:
if event.is_action_pressed("flap"):
if _idle:
self._idle = false
flap()
func flap():
func flap() -> void:
_angular_velocity = 0.0
_velocity.y = -flap_force
_target_angle = -PI / 5.0
......@@ -47,7 +47,7 @@ func flap():
tween.start()
func _physics_process(delta):
func _physics_process(delta) -> void:
_velocity.y += gravity * delta
_velocity.y = min(_velocity.y, max_fall_speed)
......@@ -56,13 +56,13 @@ func _physics_process(delta):
_angular_velocity = min(_angular_velocity, max_angular_velocity)
sprite.rotate(_angular_velocity * delta)
var motion = _velocity * delta
var collision = move_and_collide(motion)
var motion : Vector2 = _velocity * delta
var collision := move_and_collide(motion)
if collision:
die()
func die():
func die() -> void:
set_physics_process(false)
set_process_unhandled_input(false)
tween.interpolate_property(
......@@ -72,12 +72,12 @@ func die():
tween.start()
func _on_Tween_tween_completed(object, key):
func _on_Tween_tween_completed(object, key) -> void:
if key == ':scale':
self._active = false
func _set_active(value):
func _set_active(value) -> void:
_active = value
visible = value
collision_shape.disabled = not value
extends Node2D
"""
"""
export var sprite_scene : PackedScene
var count_to_fill_screen : int
var camera : Camera2D
func setup(camera_node : Camera2D):
camera = camera_node
func _ready() -> void:
var sprite : Sprite = sprite_scene.instance()
count_to_fill_screen = ceil(ProjectSettings.get('display/window/size/width') / sprite.texture.get_width())
for i in range(count_to_fill_screen):
var new_sprite = sprite_scene.instance()
new_sprite.position.x = position.x + i * sprite.texture.get_width()
......@@ -2,18 +2,18 @@ extends StaticBody2D
signal checkpoint_reached
onready var visibility_notifier_2d = get_node("VisibilityNotifier2D")
onready var checkpoint = get_node("Checkpoint")
onready var notifier : VisibilityNotifier2D = get_node("VisibilityNotifier2D")
onready var checkpoint : Area2D = get_node("Checkpoint")
func _ready():
var pos = global_position
func _ready() -> void:
var p = global_position
set_as_toplevel(true)
position = pos
global_position = p
visibility_notifier_2d.connect("screen_exited", self, "queue_free")
notifier.connect("screen_exited", self, "queue_free")
checkpoint.connect("body_entered", self, "_on_Checkpoint_body_entered")
func _on_Checkpoint_body_entered(body):
func _on_Checkpoint_body_entered(body) -> void:
emit_signal("checkpoint_reached")
......@@ -9,30 +9,30 @@ and increase the Gnu's horizontal move speed
signal pillar_checkpoint_reached
onready var spawn_area = get_node("SpawnArea")
onready var spawn_area := get_node("SpawnArea")
export(float) var spawn_interval = 400.0
export var spawn_interval := 400.0
var pillar_scene = preload("res://Pillar/Pillar.tscn")
var WINDOW_WIDTH = ProjectSettings.get('display/window/size/width')
var pillar_scene := preload("res://Pillar/Pillar.tscn")
var WINDOW_WIDTH : int = ProjectSettings.get('display/window/size/width')
func _ready():
func _ready() -> void:
randomize()
for i in range(3):
spawn_pillar()
func spawn_pillar():
var obstacle = pillar_scene.instance()
obstacle.global_position = get_random_position()
obstacle.connect("tree_exited", self, "spawn_pillar")
obstacle.connect("checkpoint_reached", self, "emit_signal", ["pillar_checkpoint_reached"])
add_child(obstacle)
func spawn_pillar() -> void:
var pillar := pillar_scene.instance()
pillar.global_position = get_random_position()
pillar.connect("tree_exited", self, "spawn_pillar")
pillar.connect("checkpoint_reached", self, "emit_signal", ["pillar_checkpoint_reached"])
add_child(pillar)
position.x += spawn_interval
func get_random_position():
func get_random_position() -> Vector2:
return Vector2(
position.x + WINDOW_WIDTH,
spawn_area.get_random_vertical_position())
......@@ -2,11 +2,11 @@ tool
extends Node2D
export(float) var min_extents = 100.0 setget set_min_extents
export(float) var max_extents = 400.0 setget set_max_extents
export var min_extents := 100.0 setget set_min_extents
export var max_extents := 400.0 setget set_max_extents
func set_max_extents(value):
func set_max_extents(value) -> void:
max_extents = value
if max_extents < min_extents:
self.min_extents = max_extents
......@@ -14,27 +14,27 @@ func set_max_extents(value):
update()
func set_min_extents(value):
func set_min_extents(value) -> void:
min_extents = min(value, max_extents)
update()
func get_random_vertical_position(ratio=1.0):
func get_random_vertical_position(ratio=1.0) -> float:
assert ratio >= 0.0 and ratio <= 1.0
var extents = lerp(min_extents, max_extents, ratio) / 2.0
var rand_position = rand_range(-extents, extents)
var extents : float = lerp(min_extents, max_extents, ratio) / 2.0
var rand_position := rand_range(-extents, extents)
return position.y + rand_position
func _draw():
func _draw() -> void:
if not Engine.editor_hint:
return
var window_width = ProjectSettings.get('display/window/size/width')
var window_width : int = ProjectSettings.get('display/window/size/width')
var rectangle_min = Rect2(
var rectangle_min := Rect2(
Vector2(0.0, -min_extents / 2.0),
Vector2(window_width, min_extents))
var rectangle_max = Rect2(
var rectangle_max := Rect2(
Vector2(0.0, -max_extents / 2.0),
Vector2(window_width, max_extents))
......
extends Label
onready var tween = get_node("Tween")
onready var tween : Tween = get_node("Tween")
const WHITE_OPAQUE = Color('aaffffff')
const WHITE_TRANSPARENT = Color('44ffffff')
const WHITE_OPAQUE := Color('aaffffff')
const WHITE_TRANSPARENT := Color('44ffffff')
var score = 0 setget set_score
var score := 0 setget set_score
func _ready():
func _ready() -> void:
modulate = WHITE_TRANSPARENT
func set_score(value):
func set_score(value) -> void:
score = value
text = str(score)
tween.interpolate_property(
......
......@@ -12,7 +12,7 @@ export var sprite_scene : PackedScene
var WINDOW_WIDTH : int = ProjectSettings.get('display/window/size/width')
func _ready():
func _ready() -> void:
if Engine.editor_hint:
if not sprite_scene:
return
......@@ -24,7 +24,7 @@ func _ready():
spawn_sprite()
func spawn_sprite():
func spawn_sprite() -> void:
"""Offsets the spawner node and spawns a new sprite"""
var sprite : Sprite = sprite_scene.instance()
sprite.connect("tree_exited", self, "spawn_sprite")
......
extends Camera2D
func _ready():
set_as_toplevel(true)
position = owner.position
func _physics_process(delta):
position.x = owner.position.x
extends KinematicBody2D
onready var tween = get_node("Tween")
onready var sprite = get_node("Sprite")
export(float) var gravity = 4000.0
export(float) var flap_force = 1200.0
export(float) var max_fall_speed = 1200.0
export(float) var max_angular_velocity = PI
export(float) var angular_acceleration = 2.0 * PI
export(float) var speed_horizontal = 280.0
var _velocity = Vector2(speed_horizontal, 0.0)
var _angular_velocity = 0.0
var _idle = true setget set_idle
var _target_angle = 0.0
func _ready():
self._idle = true
func set_idle(value):
_idle = value
set_physics_process(not value)
func _unhandled_input(event):
if event.is_action_pressed("flap"):
if _idle:
self._idle = false
flap()
func flap():
_angular_velocity = 0.0
_velocity.y = -flap_force
_target_angle = -PI / 5.0
if not tween.is_active():
tween.interpolate_property(sprite, 'rotation', sprite.rotation, _target_angle, 0.12, Tween.TRANS_CUBIC, Tween.EASE_OUT)
tween.start()
func _physics_process(delta):
_velocity.y += gravity * delta
_velocity.y = min(_velocity.y, max_fall_speed)
if not tween.is_active() and sprite.rotation < PI / 5.0:
_angular_velocity += angular_acceleration * delta
_angular_velocity = min(_angular_velocity, max_angular_velocity)
sprite.rotate(_angular_velocity * delta)
var motion = _velocity * delta
var collision = move_and_collide(motion)
if collision:
die()
func die():
set_physics_process(false)
set_process_unhandled_input(false)
tween.interpolate_property(sprite, 'scale', sprite.scale, Vector2(), 0.4, Tween.TRANS_QUAD, Tween.EASE_OUT)
tween.start()
func _on_Tween_tween_completed(object, key):
if key == ':scale':
queue_free()
[gd_scene load_steps=5 format=2]
[ext_resource path="res://gnu/Gnu.gd" type="Script" id=1]
[ext_resource path="res://gnu/Camera2D.gd" type="Script" id=2]
[ext_resource path="res://assets/gnu.png" type="Texture" id=3]
[sub_resource type="CircleShape2D" id=1]
radius = 55.8274
[node name="Gnu" type="KinematicBody2D"]
script = ExtResource( 1 )
[node name="Camera2D" type="Camera2D" parent="."]
current = true
process_mode = 0
drag_margin_left = 0.0
drag_margin_top = 0.0
drag_margin_right = 0.0
drag_margin_bottom = 0.0
script = ExtResource( 2 )
[node name="Sprite" type="Sprite" parent="."]
editor/display_folded = true
position = Vector2( -66.5, 1 )
scale = Vector2( 0.786026, 0.786026 )
texture = ExtResource( 3 )
offset = Vector2( 84.6028, -31.3285 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 )
[node name="Tween" type="Tween" parent="."]
[connection signal="tween_completed" from="Tween" to="." method="_on_Tween_tween_completed"]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment