Commit 64950735 authored by Nathan Lovato's avatar Nathan Lovato
Browse files

Finish coding the main gameplay loop

Procedural generation of obstacles, the gnu's death and the core loop are in place
parent f37d21f0
Pipeline #4944 passed with stage
in 4 minutes and 17 seconds
extends Node2D
func _unhandled_input(event):
if event.is_action_pressed("reset"):
get_tree().reload_current_scene()
[gd_scene load_steps=4 format=2]
[ext_resource path="res://Game.gd" type="Script" id=1]
[ext_resource path="res://gnu/Gnu.tscn" type="PackedScene" id=2]
[ext_resource path="res://Pillar.tscn" type="PackedScene" id=3]
[ext_resource path="res://Gnu/Gnu.tscn" type="PackedScene" id=2]
[ext_resource path="res://PillarSpawner/PillarSpawner.tscn" type="PackedScene" id=3]
[node name="Game" type="Node2D"]
script = ExtResource( 1 )
......@@ -10,9 +10,6 @@ script = ExtResource( 1 )
[node name="Gnu" parent="." instance=ExtResource( 2 )]
position = Vector2( 190, 960 )
[node name="Pillar" parent="." instance=ExtResource( 3 )]
position = Vector2( 680, 990 )
[node name="Pillar2" parent="." instance=ExtResource( 3 )]
position = Vector2( 1306, 990 )
[node name="PillarSpawner" parent="." instance=ExtResource( 3 )]
position = Vector2( -10, 950 )
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"]
extends StaticBody2D
signal checkpoint_reached
onready var visibility_notifier_2d = get_node("VisibilityNotifier2D")
onready var checkpoint = get_node("Checkpoint")
func _ready():
var pos = global_position
set_as_toplevel(true)
position = pos
visibility_notifier_2d.connect("screen_exited", self, "queue_free")
checkpoint.connect("body_entered", self, "emit_signal", ["checkpoint_reached"])
[gd_scene load_steps=5 format=2]
[ext_resource path="res://Pillar/Pillar.gd" type="Script" id=1]
[ext_resource path="res://assets/pillar_top.png" type="Texture" id=2]
[ext_resource path="res://assets/pillar_bottom.png" type="Texture" id=3]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 20, 240 )
[node name="Pillar" type="StaticBody2D"]
script = ExtResource( 1 )
[node name="pillar_top" type="Sprite" parent="."]
position = Vector2( -10, -584 )
texture = ExtResource( 2 )
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
position = Vector2( 0, 46 )
polygon = PoolVector2Array( -120, -240, 100, -240, 100, -1060, -120, -1060 )
[node name="pillar_bottom" type="Sprite" parent="."]
position = Vector2( 0, 526 )
texture = ExtResource( 3 )
[node name="CollisionPolygon2D2" type="CollisionPolygon2D" parent="."]
position = Vector2( 0, 1226 )
polygon = PoolVector2Array( -120, -240, 100, -240, 100, -1060, -120, -1060 )
[node name="Checkpoint" type="Area2D" parent="."]
position = Vector2( 119.5, -20 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Checkpoint"]
shape = SubResource( 1 )
[node name="VisibilityNotifier2D" type="VisibilityNotifier2D" parent="."]
position = Vector2( 140, 0 )
rect = Rect2( -20, -20, 40, 40 )
extends Node2D
"""
Spawns instances of the Pillar in a vertical area, outside the game's view
Places the Pillars based on their pivot
TODO: To increase the difficulty, we can slowly expand the spawn area,
and increase the Gnu's horizontal move speed
"""
onready var spawn_area = get_node("SpawnArea")
export(float) var spawn_interval = 400.0
var pillar_scene = preload("res://Pillar/Pillar.tscn")
var WINDOW_WIDTH = ProjectSettings.get('display/window/size/width')
func _ready():
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")
add_child(obstacle)
position.x += spawn_interval
func get_random_position():
var vertical_range = spawn_area.get_vertical_range()
return Vector2(
position.x + WINDOW_WIDTH,
rand_range(vertical_range.x, vertical_range.y))
\ No newline at end of file
[gd_scene load_steps=3 format=2]
[ext_resource path="res://PillarSpawner/PillarSpawner.gd" type="Script" id=1]
[ext_resource path="res://PillarSpawner/SpawnArea.gd" type="Script" id=2]
[node name="PillarSpawner" type="Node2D"]
position = Vector2( 0, 960 )
script = ExtResource( 1 )
[node name="SpawnArea" type="Node2D" parent="."]
script = ExtResource( 2 )
vertical_extents = 800.0
tool
extends Node2D
export(float) var vertical_extents setget set_vertical_extents
func set_vertical_extents(value):
vertical_extents = value
update()
func get_vertical_range():
return Vector2(
position.y - vertical_extents / 2.0,
position.y + vertical_extents / 2.0)
func _draw():
var window_width = ProjectSettings.get('display/window/size/width')
var rectangle = Rect2(
Vector2(0.0, -vertical_extents / 2.0),
Vector2(window_width, vertical_extents))
draw_rect(rectangle, Color("ffffff"), false)
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