first
This commit is contained in:
101
scene/minigames/steering/steering_minigame.gd
Normal file
101
scene/minigames/steering/steering_minigame.gd
Normal file
@@ -0,0 +1,101 @@
|
||||
extends Node2D
|
||||
|
||||
@onready var background1 = $background1
|
||||
@onready var background2 = $background2
|
||||
|
||||
@onready var spaceship = $Spaceship
|
||||
@onready var spaceshipHitbox = $Spaceship/Area2D
|
||||
@onready var astroid = preload("res://scene/minigames/steering/Astroid.tscn")
|
||||
|
||||
@export var shipLogicRef: ShipLogic
|
||||
|
||||
var controllStick: Node3D
|
||||
var controllStickGoalPos: float = 0.0
|
||||
const CONTROLL_STICK_SNAPBACK: float = 24
|
||||
|
||||
var idleMoveAmplitude: float = 15
|
||||
var timePassed: float = 0
|
||||
var astroidTimer: float = 0
|
||||
var scrollSpeed: float = 8.5 #Pixels per seccond
|
||||
|
||||
signal collision
|
||||
|
||||
const POS_LEFT = 64
|
||||
const POS_MIDDLE = 128
|
||||
const POS_RIGHT = 192
|
||||
|
||||
var starshipGoalPos: int = 1
|
||||
const starshipHorizontalSpeed: float = 56
|
||||
|
||||
var rng = RandomNumberGenerator.new()
|
||||
|
||||
var active: bool = false
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
timePassed += delta
|
||||
astroidTimer += delta
|
||||
background1.position.y += scrollSpeed * delta
|
||||
background2.position.y += scrollSpeed * delta
|
||||
spaceship.position.y += idleMoveAmplitude * sin(timePassed)*delta
|
||||
if controllStick:
|
||||
controllStick.rotation_degrees.x = move_toward(controllStick.rotation_degrees.x,controllStickGoalPos,delta*CONTROLL_STICK_SNAPBACK)
|
||||
if abs(controllStick.rotation_degrees.x) > 15:
|
||||
controllStickGoalPos = 0
|
||||
|
||||
if astroidTimer >= 90:
|
||||
astroidTimer = rng.randf_range(0,20)
|
||||
var astroidCounter = 0
|
||||
if rng.randf() > 0.5:
|
||||
spawnAstroid(POS_LEFT)
|
||||
astroidCounter += 1
|
||||
if rng.randf() > 0.5:
|
||||
spawnAstroid(POS_MIDDLE)
|
||||
astroidCounter += 1
|
||||
if rng.randf() > 0.5 and astroidCounter < 2:
|
||||
spawnAstroid(POS_RIGHT)
|
||||
|
||||
infiniteScroll(background1)
|
||||
infiniteScroll(background2)
|
||||
|
||||
spaceship.position.x = move_toward(spaceship.position.x,goalPosToPos(),starshipHorizontalSpeed*delta)
|
||||
|
||||
if active:
|
||||
if Input.is_action_just_pressed("moveLeft"):
|
||||
starshipGoalPos = clamp(starshipGoalPos-1,0,2)
|
||||
if controllStick:
|
||||
controllStickGoalPos = -16
|
||||
if Input.is_action_just_pressed("moveRight"):
|
||||
starshipGoalPos = clamp(starshipGoalPos+1,0,2)
|
||||
if controllStick:
|
||||
controllStickGoalPos = 16
|
||||
|
||||
if Input.is_action_just_pressed("interact") or Input.is_action_just_pressed("moveDown"):
|
||||
active = false
|
||||
|
||||
func goalPosToPos() -> float:
|
||||
match starshipGoalPos:
|
||||
0:
|
||||
return POS_LEFT
|
||||
1:
|
||||
return POS_MIDDLE
|
||||
2:
|
||||
return POS_RIGHT
|
||||
return POS_MIDDLE
|
||||
|
||||
func activate() -> void:
|
||||
active = true
|
||||
|
||||
func infiniteScroll(sprite: Sprite2D):
|
||||
if sprite.position.y >=511:
|
||||
sprite.position.y = -511
|
||||
|
||||
func spawnAstroid(Xpos: float):
|
||||
var instance = astroid.instantiate()
|
||||
add_child(instance)
|
||||
instance.position = Vector2(Xpos,-10)
|
||||
instance.speed = rng.randf_range(3,9)
|
||||
instance.rotationSpeed = PI/16
|
||||
|
||||
|
||||
func _on_area_2d_area_entered(_area: Area2D) -> void:
|
||||
collision.emit()
|
||||
Reference in New Issue
Block a user