1
Fork 0
mirror of https://github.com/Steffo99/looping-for-loops.git synced 2024-11-21 23:54:23 +00:00

✏️ Refactor most code

This commit is contained in:
Steffo 2020-10-04 16:35:24 +02:00
parent 6766532474
commit dbc52c5be7
29 changed files with 146 additions and 126 deletions

View file

@ -6,19 +6,16 @@ class_name ConveyorBelt
export(float) var cb_speed = 100 setget set_cb_speed, get_cb_speed export(float) var cb_speed = 100 setget set_cb_speed, get_cb_speed
export(bool) var randomize_gear_starting_position = false export(bool) var randomize_gear_starting_position = false
signal cb_speed_changed(old, new)
func set_cb_speed(value): func set_cb_speed(value):
var old = cb_speed
cb_speed = value cb_speed = value
$Gears.set_cb_speed(value) emit_signal("cb_speed_changed", old, value)
func get_cb_speed(): func get_cb_speed():
return cb_speed return cb_speed
func _ready():
$Gears.set_randomize_start_position(randomize_gear_starting_position)
$Gears.set_cb_speed(cb_speed)
func get_relative_cb_speed(other_pos: Vector2): func get_relative_cb_speed(other_pos: Vector2):
var relative_position = other_pos - position var relative_position = other_pos - position
var speed_sign = sign(relative_position.dot(Vector2.UP.rotated(rotation))) var speed_sign = sign(relative_position.dot(Vector2.UP.rotated(rotation)))

View file

@ -1,9 +1,11 @@
[gd_scene load_steps=6 format=2] [gd_scene load_steps=6 format=2]
[ext_resource path="res://Tilesets/ConveyorBelt.tres" type="TileSet" id=1] [ext_resource path="res://Tilesets/ConveyorBelt.tres" type="TileSet" id=1]
[ext_resource path="res://Scripts/ConveyorBelt.gd" type="Script" id=2] [ext_resource path="res://Objects/ConveyorBelt/ConveyorBelt.gd" type="Script" id=2]
[ext_resource path="res://Scenes/Gear.tscn" type="PackedScene" id=3] [ext_resource path="res://Objects/ConveyorBelt/Gear.tscn" type="PackedScene" id=3]
[ext_resource path="res://Scripts/Gears.gd" type="Script" id=5] [ext_resource path="res://Objects/ConveyorBelt/Gears.gd" type="Script" id=5]
[sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id=1]
@ -125,3 +127,4 @@ position = Vector2( 800, 0 )
[node name="Gear32" parent="Gears" instance=ExtResource( 3 )] [node name="Gear32" parent="Gears" instance=ExtResource( 3 )]
position = Vector2( 840, 0 ) position = Vector2( 840, 0 )
[connection signal="cb_speed_changed" from="." to="Gears" method="_on_ConveyorBelt_cb_speed_changed"]

View file

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=4 format=2]
[ext_resource path="res://Sprites/conveyor_gear.png" type="Texture" id=1] [ext_resource path="res://Sprites/conveyor_gear.png" type="Texture" id=1]
[ext_resource path="res://Scripts/Gear.gd" type="Script" id=2] [ext_resource path="res://Objects/ConveyorBelt/Gear.gd" type="Script" id=2]
[sub_resource type="Animation" id=2] [sub_resource type="Animation" id=2]

View file

@ -1,18 +1,14 @@
extends Node2D extends Node2D
var cb_speed: float setget set_cb_speed, get_cb_speed
func set_cb_speed(value):
for children in get_children():
children.set_cb_speed(value)
func get_cb_speed():
return cb_speed
var randomize_start_position: bool setget set_randomize_start_position var randomize_start_position: bool setget set_randomize_start_position
func set_randomize_start_position(value): func set_randomize_start_position(value):
for children in get_children(): for children in get_children():
children.randomize_start_position = value children.randomize_start_position = value
func _on_ConveyorBelt_cb_speed_changed(old, new):
for children in get_children():
children.set_cb_speed(new)

View file

@ -1,4 +1,4 @@
extends ScrollingArea extends Area2D
class_name Buzzsaw class_name Buzzsaw

View file

@ -1,7 +1,8 @@
[gd_scene load_steps=5 format=2] [gd_scene load_steps=6 format=2]
[ext_resource path="res://Sprites/saw.png" type="Texture" id=1] [ext_resource path="res://Sprites/saw.png" type="Texture" id=1]
[ext_resource path="res://Scripts/Obstacles/Buzzsaw.gd" type="Script" id=2] [ext_resource path="res://Objects/ScrollingObjects/Buzzsaw.gd" type="Script" id=2]
[ext_resource path="res://Objects/ScrollingObjects/ConveyorScrollParent.tscn" type="PackedScene" id=3]
[sub_resource type="CircleShape2D" id=1] [sub_resource type="CircleShape2D" id=1]
radius = 71.9263 radius = 71.9263
@ -34,3 +35,5 @@ shape = SubResource( 1 )
[node name="AnimationPlayer" type="AnimationPlayer" parent="."] [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/SpinClockwise = SubResource( 2 ) anims/SpinClockwise = SubResource( 2 )
[node name="ConveyorScrollParent" parent="." instance=ExtResource( 3 )]

View file

@ -0,0 +1,13 @@
extends ScrollParent
class_name ConveyorScrollParent
onready var conveyor_belt: ConveyorBelt = null
func _enter_tree():
conveyor_belt = get_tree().current_scene.get_node("ConveyorBelt")
conveyor_belt.connect("cb_speed_changed", self, "_on_ConveyorBelt_cb_speed_changed")
func _on_ConveyorBelt_cb_speed_changed(old, new):
scroll_velocity.x = new

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Objects/ScrollingObjects/ConveyorScrollParent.gd" type="Script" id=1]
[node name="ConveyorScrollParent" type="Node"]
script = ExtResource( 1 )

View file

@ -1,4 +1,4 @@
extends ScrollingBlock extends KinematicBody2D
class_name Press class_name Press
export(int, 0, 47) var subbeat_offset = 0 export(int, 0, 47) var subbeat_offset = 0
@ -6,21 +6,19 @@ export(int, 0, 47) var subbeat_offset = 0
var root_node: Node = null var root_node: Node = null
var conductor: Conductor = null var conductor: Conductor = null
func _enter_tree():
root_node = get_tree().current_scene
conductor = root_node.get_node("Conductor")
func _ready():
$CollisionShape2D.shape = RectangleShape2D.new()
$CollisionShape2D.shape.extents = Vector2(80, 76.5)
$Bottom/StompArea/CollisionShape2D.shape = RectangleShape2D.new()
$Bottom/StompArea/CollisionShape2D.shape.extents = Vector2(78, 4)
conductor.connect("subbeat", self, "_subbeat")
func _subbeat(subbeat_num): func _subbeat(subbeat_num):
if (subbeat_num - subbeat_offset) % 48 == 0: if (subbeat_num - subbeat_offset) % 48 == 0:
$AnimationPlayer.play("Stomp") $AnimationPlayer.play("Stomp")
elif (subbeat_num - subbeat_offset) % 48 == 24: elif (subbeat_num - subbeat_offset) % 48 == 24:
$AnimationPlayer.play_backwards("Stomp") $AnimationPlayer.play_backwards("Stomp")
func _enter_tree():
$CollisionShape2D.shape = RectangleShape2D.new()
$CollisionShape2D.shape.extents = Vector2(80, 76.5)
$Bottom/StompArea/CollisionShape2D.shape = RectangleShape2D.new()
$Bottom/StompArea/CollisionShape2D.shape.extents = Vector2(78, 4)
root_node = get_tree().current_scene
conductor = root_node.get_node("Conductor")
conductor.connect("subbeat", self, "_subbeat")
func _on_StompArea_body_entered(body):
print(body)

View file

@ -1,7 +1,7 @@
[gd_scene load_steps=5 format=2] [gd_scene load_steps=5 format=2]
[ext_resource path="res://Sprites/press_bottom.png" type="Texture" id=1] [ext_resource path="res://Sprites/press_bottom.png" type="Texture" id=1]
[ext_resource path="res://Scripts/Obstacles/Press.gd" type="Script" id=2] [ext_resource path="res://Objects/ScrollingObjects/Press.gd" type="Script" id=2]
[ext_resource path="res://Sprites/press_pipes.png" type="Texture" id=3] [ext_resource path="res://Sprites/press_pipes.png" type="Texture" id=3]
[sub_resource type="Animation" id=1] [sub_resource type="Animation" id=1]
@ -91,4 +91,3 @@ disabled = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, 76.5 ) position = Vector2( 0, 76.5 )
[connection signal="body_entered" from="Bottom/StompArea" to="." method="_on_StompArea_body_entered"]

View file

@ -0,0 +1,14 @@
extends Node
class_name ScrollParent
export(Vector2) var scroll_velocity: Vector2 = Vector2(-100, 0)
var parent = null
func _enter_tree():
parent = get_parent()
func _physics_process(delta):
parent.position += scroll_velocity * delta

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Objects/ScrollingObjects/ScrollParent.gd" type="Script" id=1]
[node name="ScrollParent" type="Node"]
script = ExtResource( 1 )

View file

@ -1,7 +1,7 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=4 format=2]
[ext_resource path="res://Sprites/crate.png" type="Texture" id=1] [ext_resource path="res://Sprites/crate.png" type="Texture" id=1]
[ext_resource path="res://Scripts/Obstacles/ScrollingBlock.gd" type="Script" id=2] [ext_resource path="res://Objects/ScrollingObjects/ConveyorScrollParent.tscn" type="PackedScene" id=2]
[sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 20, 20 ) extents = Vector2( 20, 20 )
@ -10,10 +10,11 @@ extents = Vector2( 20, 20 )
collision_layer = 8 collision_layer = 8
collision_mask = 2 collision_mask = 2
motion/sync_to_physics = true motion/sync_to_physics = true
script = ExtResource( 2 )
[node name="Sprite" type="Sprite" parent="."] [node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 1 ) texture = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="ConveyorScrollParent" parent="." instance=ExtResource( 2 )]

View file

@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Objects/ScrollingObjects/ConveyorScrollParent.tscn" type="PackedScene" id=2]
[node name="ScrollingPipe" type="KinematicBody2D"]
collision_layer = 8
collision_mask = 2
motion/sync_to_physics = true
[node name="ConveyorScrollParent" parent="." instance=ExtResource( 2 )]

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=2] [gd_scene load_steps=2 format=2]
[ext_resource path="res://Scripts/Conductor.gd" type="Script" id=1] [ext_resource path="res://Objects/SpecialObjects/Conductor.gd" type="Script" id=1]
[node name="Conductor" type="Node"] [node name="Conductor" type="Node"]
script = ExtResource( 1 ) script = ExtResource( 1 )

View file

@ -1,28 +1,30 @@
[gd_scene load_steps=9 format=2] [gd_scene load_steps=9 format=2]
[ext_resource path="res://Scenes/Player.tscn" type="PackedScene" id=1] [ext_resource path="res://Objects/SpecialObjects/Player.tscn" type="PackedScene" id=1]
[ext_resource path="res://Scenes/Conductor.tscn" type="PackedScene" id=2] [ext_resource path="res://Objects/SpecialObjects/Conductor.tscn" type="PackedScene" id=2]
[ext_resource path="res://Scenes/ConveyorBelt.tscn" type="PackedScene" id=3] [ext_resource path="res://Objects/ConveyorBelt/ConveyorBelt.tscn" type="PackedScene" id=3]
[ext_resource path="res://Scenes/SingleScreenWalls.tscn" type="PackedScene" id=4] [ext_resource path="res://Objects/SpecialObjects/SingleScreenWalls.tscn" type="PackedScene" id=4]
[ext_resource path="res://Scenes/Obstacles/Press.tscn" type="PackedScene" id=5] [ext_resource path="res://Objects/ScrollingObjects/Press.tscn" type="PackedScene" id=5]
[ext_resource path="res://Scenes/Obstacles/ScrollingPipe.tscn" type="PackedScene" id=6] [ext_resource path="res://Objects/ScrollingObjects/ScrollingPipe.tscn" type="PackedScene" id=6]
[ext_resource path="res://Scenes/Obstacles/Buzzsaw.tscn" type="PackedScene" id=7] [ext_resource path="res://Objects/ScrollingObjects/Buzzsaw.tscn" type="PackedScene" id=7]
[ext_resource path="res://Scenes/Obstacles/ScrollingPipesTileMap.tscn" type="PackedScene" id=8] [ext_resource path="res://Objects/ScrollingObjects/ScrollingPipesTileMap.tscn" type="PackedScene" id=8]
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
__meta__ = { __meta__ = {
"_edit_horizontal_guides_": [ ] "_edit_horizontal_guides_": [ ]
} }
[node name="Player" parent="." instance=ExtResource( 1 )] [node name="Conductor" parent="." instance=ExtResource( 2 )]
position = Vector2( 340, 500 )
[node name="SingleScreenWalls" parent="." instance=ExtResource( 4 )]
[node name="ConveyorBelt" parent="." instance=ExtResource( 3 )] [node name="ConveyorBelt" parent="." instance=ExtResource( 3 )]
position = Vector2( 640, 700 ) position = Vector2( 640, 700 )
[node name="SingleScreenWalls" parent="." instance=ExtResource( 4 )] [node name="Player" parent="." instance=ExtResource( 1 )]
position = Vector2( 340, 500 )
[node name="Conductor" parent="." instance=ExtResource( 2 )]
[node name="Buzzsaw" parent="." instance=ExtResource( 7 )] [node name="Buzzsaw" parent="." instance=ExtResource( 7 )]
position = Vector2( 1000, 540 ) position = Vector2( 1000, 540 )

View file

@ -1,24 +1,33 @@
extends ExtendedKinematicBody2D extends KinematicBody2D
class_name Player class_name Player
export(Vector2) var gravity: Vector2 = Vector2(0, 10)
var speed: Vector2 = Vector2.ZERO
export(Vector2) var gravity: Vector2 = Vector2(0, 10)
export(float) var move_speed: float = 300 export(float) var move_speed: float = 300
export(float) var jump_speed: float = 425 export(float) var jump_speed: float = 425
export(float) var jump_buffer_msec: float = 80 export(float) var jump_buffer_msec: float = 80
export(float) var quick_fall_gravity_multiplier: float = 4 export(float) var quick_fall_gravity_multiplier: float = 4
export(bool) var stop_jump_on_bonk: bool = true export(bool) var stop_jump_on_bonk: bool = true
var speed: Vector2 = Vector2.ZERO
var can_jump: bool = false var can_jump: bool = false
var jump_buffer: int = 0 var jump_buffer: int = 0
var is_quick_falling: bool = false var is_quick_falling: bool = false
var quick_fall_buffer: int = 0 var quick_fall_buffer: int = 0
func get_floor():
for slide in get_slide_count():
var collision = get_slide_collision(slide)
if collision.normal != get_floor_normal():
continue
return collision.collider
func up_normal(): func up_normal():
return -gravity.normalized() return -gravity.normalized()
func _physics_process(delta): func _physics_process(delta):
var up_normal = up_normal() var up_normal = up_normal()
var floor_normal = get_floor_normal() var floor_normal = get_floor_normal()

View file

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=4 format=2]
[ext_resource path="res://Sprites/player_danny_devito.png" type="Texture" id=1] [ext_resource path="res://Sprites/player_danny_devito.png" type="Texture" id=1]
[ext_resource path="res://Scripts/Player.gd" type="Script" id=2] [ext_resource path="res://Objects/SpecialObjects/Player.gd" type="Script" id=2]
[sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 16, 16 ) extents = Vector2( 16, 16 )

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=4 format=2]
[ext_resource path="res://Scenes/Wall.tscn" type="PackedScene" id=1] [ext_resource path="res://Objects/SpecialObjects/Wall.tscn" type="PackedScene" id=1]
[sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 20, 360 ) extents = Vector2( 20, 360 )
@ -10,25 +10,19 @@ extents = Vector2( 680, 20 )
[node name="SingleScreenWalls" type="Node2D"] [node name="SingleScreenWalls" type="Node2D"]
[node name="WallLeft" type="StaticBody2D" parent="." instance=ExtResource( 1 )] [node name="WallLeft" parent="." instance=ExtResource( 1 )]
collision_layer = 16
collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="WallLeft"] [node name="CollisionShape2D" type="CollisionShape2D" parent="WallLeft"]
position = Vector2( -20, 360 ) position = Vector2( -20, 360 )
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="WallRight" type="StaticBody2D" parent="." instance=ExtResource( 1 )] [node name="WallRight" parent="." instance=ExtResource( 1 )]
collision_layer = 16
collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="WallRight"] [node name="CollisionShape2D" type="CollisionShape2D" parent="WallRight"]
position = Vector2( 1300, 360 ) position = Vector2( 1300, 360 )
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="WallTop" type="StaticBody2D" parent="." instance=ExtResource( 1 )] [node name="WallTop" parent="." instance=ExtResource( 1 )]
collision_layer = 16
collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="WallTop"] [node name="CollisionShape2D" type="CollisionShape2D" parent="WallTop"]
position = Vector2( 640, -20 ) position = Vector2( 640, -20 )

View file

@ -1,9 +0,0 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Scripts/Obstacles/ScrollingBlock.gd" type="Script" id=2]
[node name="ScrollingPipe" type="KinematicBody2D"]
collision_layer = 8
collision_mask = 2
motion/sync_to_physics = true
script = ExtResource( 2 )

View file

@ -1,10 +0,0 @@
extends KinematicBody2D
class_name ExtendedKinematicBody2D
func get_floor():
for slide in get_slide_count():
var collision = get_slide_collision(slide)
if collision.normal != get_floor_normal():
continue
return collision.collider

View file

@ -1,9 +0,0 @@
extends ExtendedKinematicBody2D
class_name ScrollingBlock
export(Vector2) var scroll_velocity: Vector2 = Vector2(-100, 0)
func _physics_process(delta):
position += scroll_velocity * delta

View file

@ -1,9 +1,4 @@
extends Area2D extends ScrollingObject2D
class_name ScrollingArea class_name ScrollingArea
export(Vector2) var scroll_velocity: Vector2 = Vector2(-100, 0)
func _physics_process(delta):
position += scroll_velocity * delta

View file

@ -9,61 +9,61 @@
config_version=4 config_version=4
_global_script_classes=[ { _global_script_classes=[ {
"base": "ScrollingArea", "base": "Area2D",
"class": "Buzzsaw", "class": "Buzzsaw",
"language": "GDScript", "language": "GDScript",
"path": "res://Scripts/Obstacles/Buzzsaw.gd" "path": "res://Objects/ScrollingObjects/Buzzsaw.gd"
}, { }, {
"base": "Node", "base": "Node",
"class": "Conductor", "class": "Conductor",
"language": "GDScript", "language": "GDScript",
"path": "res://Scripts/Conductor.gd" "path": "res://Objects/SpecialObjects/Conductor.gd"
}, { }, {
"base": "StaticBody2D", "base": "StaticBody2D",
"class": "ConveyorBelt", "class": "ConveyorBelt",
"language": "GDScript", "language": "GDScript",
"path": "res://Scripts/ConveyorBelt.gd" "path": "res://Objects/ConveyorBelt/ConveyorBelt.gd"
}, {
"base": "ScrollParent",
"class": "ConveyorScrollParent",
"language": "GDScript",
"path": "res://Objects/ScrollingObjects/ConveyorScrollParent.gd"
}, { }, {
"base": "KinematicBody2D", "base": "KinematicBody2D",
"class": "ExtendedKinematicBody2D",
"language": "GDScript",
"path": "res://Scripts/ExtendedKinematicBody2D.gd"
}, {
"base": "ExtendedKinematicBody2D",
"class": "Player", "class": "Player",
"language": "GDScript", "language": "GDScript",
"path": "res://Scripts/Player.gd" "path": "res://Objects/SpecialObjects/Player.gd"
}, { }, {
"base": "ScrollingBlock", "base": "KinematicBody2D",
"class": "Press", "class": "Press",
"language": "GDScript", "language": "GDScript",
"path": "res://Scripts/Obstacles/Press.gd" "path": "res://Objects/ScrollingObjects/Press.gd"
}, { }, {
"base": "Area2D", "base": "Node",
"class": "ScrollParent",
"language": "GDScript",
"path": "res://Objects/ScrollingObjects/ScrollParent.gd"
}, {
"base": "ScrollingObject2D",
"class": "ScrollingArea", "class": "ScrollingArea",
"language": "GDScript", "language": "GDScript",
"path": "res://ScrollingArea.gd" "path": "res://ScrollingArea.gd"
}, {
"base": "ExtendedKinematicBody2D",
"class": "ScrollingBlock",
"language": "GDScript",
"path": "res://Scripts/Obstacles/ScrollingBlock.gd"
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"Buzzsaw": "", "Buzzsaw": "",
"Conductor": "", "Conductor": "",
"ConveyorBelt": "", "ConveyorBelt": "",
"ExtendedKinematicBody2D": "", "ConveyorScrollParent": "",
"Player": "", "Player": "",
"Press": "", "Press": "",
"ScrollingArea": "", "ScrollParent": "",
"ScrollingBlock": "" "ScrollingArea": ""
} }
[application] [application]
config/name="ld47" config/name="ld47"
run/main_scene="res://Scenes/Main.tscn" run/main_scene="res://Objects/SpecialObjects/Main.tscn"
[display] [display]