Initial commit
This commit is contained in:
49
scripts/platform_handler.gd
Normal file
49
scripts/platform_handler.gd
Normal file
@@ -0,0 +1,49 @@
|
||||
extends Node
|
||||
|
||||
var rng = RandomNumberGenerator.new()
|
||||
var PlatformScene := preload("res://scenes/platform.tscn")
|
||||
var platforms := []
|
||||
var dead = false
|
||||
@onready var height: float = get_viewport().size.y
|
||||
@onready var width: float = get_viewport().size.x
|
||||
@onready var player: CharacterBody2D = %Player
|
||||
const N = 10
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
for i in range(N):
|
||||
var y = i * height / N
|
||||
# TODO: don't place platforms where others exist already
|
||||
place_platform(y)
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
var move = 0
|
||||
if player.position.y < height/2 and not dead:
|
||||
move = height/2 - player.position.y
|
||||
player.position.y = height/2
|
||||
elif player.position.y > height or dead:
|
||||
dead = true
|
||||
move = -40
|
||||
player.position.y -= 100/2.87
|
||||
|
||||
|
||||
|
||||
for platform in platforms:
|
||||
platform.position.y += move
|
||||
if platform.position.y > height + platform.get_node("Sprite2D").texture.get_height():
|
||||
platform.position = Vector2(rng.randf_range(0, width), 0)
|
||||
|
||||
|
||||
|
||||
|
||||
func place_platform(y: float = 0) -> void:
|
||||
var platform_instance = PlatformScene.instantiate()
|
||||
var x = rng.randf_range(0, width)
|
||||
platform_instance.position = Vector2(x, y)
|
||||
add_child(platform_instance)
|
||||
platforms.append(platform_instance)
|
||||
Reference in New Issue
Block a user