game: add magnet pickup
This commit is contained in:
28
scenes/pickups/pickup_base.gd
Normal file
28
scenes/pickups/pickup_base.gd
Normal file
@@ -0,0 +1,28 @@
|
||||
class_name PickupBase
|
||||
extends Node2D
|
||||
|
||||
@export var value: float
|
||||
@export var max_speed: float = 100.0
|
||||
@export var min_speed: float = 10.0
|
||||
|
||||
var player: Player
|
||||
|
||||
func _ready() -> void:
|
||||
player = get_tree().get_first_node_in_group(GlobalConst.GROUP_PLAYER)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not player:
|
||||
return
|
||||
|
||||
var to_player = player.global_position - global_position
|
||||
var dist = to_player.length()
|
||||
var attract_radius = player.player_stats.get_final("pickup_radius", player.modifiers)
|
||||
if dist < 4:
|
||||
return
|
||||
if dist < attract_radius:
|
||||
var dir = to_player.normalized()
|
||||
var speed = lerp(min_speed, max_speed, (attract_radius - dist) / attract_radius)
|
||||
global_position += dir * speed * delta
|
||||
|
||||
func pickup() -> void:
|
||||
push_error("%s did not implement pickup()" % self)
|
1
scenes/pickups/pickup_base.gd.uid
Normal file
1
scenes/pickups/pickup_base.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bo2oowrbsp2k8
|
24
scenes/pickups/pickup_base.tscn
Normal file
24
scenes/pickups/pickup_base.tscn
Normal file
@@ -0,0 +1,24 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://dr80h4envloce"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bo2oowrbsp2k8" path="res://scenes/pickups/pickup_base.gd" id="1_oto2a"]
|
||||
|
||||
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_rs1tx"]
|
||||
size = Vector2(10, 10)
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_oto2a"]
|
||||
radius = 3.16228
|
||||
|
||||
[node name="PickupBase" type="Node2D" groups=["pickup"]]
|
||||
script = ExtResource("1_oto2a")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
position = Vector2(-1, 1)
|
||||
texture = SubResource("PlaceholderTexture2D_rs1tx")
|
||||
|
||||
[node name="Area2D" type="Area2D" parent="."]
|
||||
collision_layer = 16
|
||||
collision_mask = 0
|
||||
monitoring = false
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||
shape = SubResource("CircleShape2D_oto2a")
|
36
scenes/pickups/pickup_magnet.gd
Normal file
36
scenes/pickups/pickup_magnet.gd
Normal file
@@ -0,0 +1,36 @@
|
||||
class_name PickupMagnet
|
||||
extends PickupBase
|
||||
|
||||
@export var duration: float = 15.0
|
||||
|
||||
@onready var collision_shape_2d: CollisionShape2D = $Area2D/CollisionShape2D
|
||||
|
||||
func pickup() -> void:
|
||||
if not player:
|
||||
push_error("pickup called on %s without player set" % self)
|
||||
return
|
||||
|
||||
var mod: PlayerStatsModifier = PlayerStatsModifier.new()
|
||||
mod.description = "Magnet massively increases pickup range for a limited amout of time"
|
||||
mod.title = "Magnet"
|
||||
mod.tex = GlobalConst.placeholder_tex
|
||||
mod.internal_name = "magnet"
|
||||
mod.rarity = GlobalConst.ModRarity.RARE
|
||||
mod.stat_name = "pickup_radius"
|
||||
mod.value = 5000.0
|
||||
mod.type = PlayerStatsModifier.ModifierType.ABSOLUTE
|
||||
player.modifiers.append(mod)
|
||||
|
||||
# Hide, and stop processing until timeout
|
||||
visible = false
|
||||
set_process(false)
|
||||
set_physics_process(false)
|
||||
collision_shape_2d.disabled = true
|
||||
|
||||
await get_tree().create_timer(duration).timeout
|
||||
if is_instance_valid(player):
|
||||
for i in range(player.modifiers.size()):
|
||||
if player.modifiers[i].internal_name == mod.internal_name:
|
||||
player.modifiers.remove_at(i)
|
||||
break
|
||||
queue_free()
|
1
scenes/pickups/pickup_magnet.gd.uid
Normal file
1
scenes/pickups/pickup_magnet.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://54b8m2gk20ji
|
12
scenes/pickups/pickup_magnet.tscn
Normal file
12
scenes/pickups/pickup_magnet.tscn
Normal file
@@ -0,0 +1,12 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://bbev8m5g0p3a3"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://dr80h4envloce" path="res://scenes/pickups/pickup_base.tscn" id="1_hm4ld"]
|
||||
[ext_resource type="Script" uid="uid://54b8m2gk20ji" path="res://scenes/pickups/pickup_magnet.gd" id="2_00la4"]
|
||||
[ext_resource type="Texture2D" uid="uid://c80xxb85fehng" path="res://assets/sprites/magnet.png" id="2_yx0vp"]
|
||||
|
||||
[node name="PickupMagnet" instance=ExtResource("1_hm4ld")]
|
||||
script = ExtResource("2_00la4")
|
||||
duration = 15.0
|
||||
|
||||
[node name="Sprite2D" parent="." index="0"]
|
||||
texture = ExtResource("2_yx0vp")
|
Reference in New Issue
Block a user