416 lines
13 KiB
C#
416 lines
13 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using Godot;
|
|
using Godot.Bridge;
|
|
using Godot.NativeInterop;
|
|
|
|
[ScriptPath("res://Scripts/Puzzles/CrateConveyor.cs")]
|
|
public class CrateConveyor : Node2D
|
|
{
|
|
public new class MethodName : Node2D.MethodName
|
|
{
|
|
public new static readonly StringName _EnterTree = "_EnterTree";
|
|
|
|
public new static readonly StringName _Process = "_Process";
|
|
|
|
public static readonly StringName PlayerHit = "PlayerHit";
|
|
|
|
public static readonly StringName PlayerInside = "PlayerInside";
|
|
|
|
public static readonly StringName PlayerOutside = "PlayerOutside";
|
|
|
|
public static readonly StringName Stop = "Stop";
|
|
}
|
|
|
|
public new class PropertyName : Node2D.PropertyName
|
|
{
|
|
public static readonly StringName speed = "speed";
|
|
|
|
public static readonly StringName crates = "crates";
|
|
|
|
public static readonly StringName colliders = "colliders";
|
|
|
|
public static readonly StringName conveyors = "conveyors";
|
|
|
|
public static readonly StringName running = "running";
|
|
|
|
public static readonly StringName marker = "marker";
|
|
|
|
public static readonly StringName hits = "hits";
|
|
}
|
|
|
|
public new class SignalName : Node2D.SignalName
|
|
{
|
|
}
|
|
|
|
[Export(PropertyHint.None, "")]
|
|
private Vector2 speed = Vector2.Down;
|
|
|
|
[Export(PropertyHint.None, "")]
|
|
private AnimationPlayer[] crates;
|
|
|
|
[Export(PropertyHint.None, "")]
|
|
private Area2D[] colliders;
|
|
|
|
[Export(PropertyHint.None, "")]
|
|
private Area2D conveyors;
|
|
|
|
[Export(PropertyHint.None, "")]
|
|
private bool running = true;
|
|
|
|
[Export(PropertyHint.None, "")]
|
|
private Node2D marker;
|
|
|
|
private int hits;
|
|
|
|
public override void _EnterTree()
|
|
{
|
|
for (int i = 0; i < colliders.Length; i++)
|
|
{
|
|
colliders[i].BodyEntered += PlayerHit;
|
|
}
|
|
conveyors.BodyEntered += PlayerInside;
|
|
conveyors.BodyExited += PlayerOutside;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (Player.instance.onPlatform == this)
|
|
{
|
|
Player.instance.controlling.GlobalPosition += speed * Main.deltaTime;
|
|
Player.instance.AddFollowerStep();
|
|
}
|
|
}
|
|
|
|
public void PlayerHit(Node other)
|
|
{
|
|
if (other != Player.instance?.controlling)
|
|
{
|
|
return;
|
|
}
|
|
if (Main.inEvent == null)
|
|
{
|
|
Main.inEvent = Coroutine.Start(Falling());
|
|
}
|
|
hits++;
|
|
if (hits == 4)
|
|
{
|
|
for (int i = 0; i < crates.Length; i++)
|
|
{
|
|
crates[i].SpeedScale *= 0.8f;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void PlayerInside(Node other)
|
|
{
|
|
if (other == Player.instance?.controlling)
|
|
{
|
|
Player.instance.onPlatform = this;
|
|
}
|
|
}
|
|
|
|
private void PlayerOutside(Node other)
|
|
{
|
|
if (other == Player.instance?.controlling && Player.instance.onPlatform == this)
|
|
{
|
|
Player.instance.onPlatform = null;
|
|
}
|
|
}
|
|
|
|
private IEnumerator Falling()
|
|
{
|
|
if (Player.instance.onPlatform == this)
|
|
{
|
|
Player.instance.onPlatform = null;
|
|
}
|
|
Audio.PlaySound("snd_grab.wav");
|
|
Audio.PlaySound("snd_fall2.wav");
|
|
Party.StopMoving();
|
|
yield return null;
|
|
Party.UpdateAnim("Hurt");
|
|
float x = Player.instance.controlling.GlobalPosition.X + (float)Main.RandomRange(-20, 20);
|
|
Vector2 globalPosition = Player.instance.controlling.GlobalPosition;
|
|
Vector2 end = new Vector2(x, marker.GlobalPosition.Y);
|
|
Vector2 mid = new Vector2(Mathf.Lerp(globalPosition.X, end.X, 0.5f), globalPosition.Y - 40f);
|
|
Party.UpdateFollowerEntities();
|
|
Coroutine c = null;
|
|
for (int i = 0; i < SaveFile.current.activeParty.Count; i++)
|
|
{
|
|
c = Coroutine.Start(CommonEvents.BeizierMove(Party.party[SaveFile.current.activeParty[i]].oEntity, globalPosition, mid, end, 30 + 10 * i));
|
|
}
|
|
while (!c.done)
|
|
{
|
|
yield return null;
|
|
}
|
|
Party.UpdateAnim();
|
|
Player.instance.ResetFollowStep();
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
Audio.PlaySound("snd_switchpull_n.wav");
|
|
running = !running;
|
|
for (int i = 0; i < crates.Length; i++)
|
|
{
|
|
if (!running)
|
|
{
|
|
crates[i].GetAnimation(crates[i].CurrentAnimation).LoopMode = Animation.LoopModeEnum.None;
|
|
continue;
|
|
}
|
|
crates[i].Play("new_animation");
|
|
crates[i].GetAnimation(crates[i].CurrentAnimation).LoopMode = Animation.LoopModeEnum.Linear;
|
|
}
|
|
Player.instance.canInput = true;
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
internal static List<MethodInfo> GetGodotMethodList()
|
|
{
|
|
return new List<MethodInfo>(6)
|
|
{
|
|
new MethodInfo(MethodName._EnterTree, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, null, null),
|
|
new MethodInfo(MethodName._Process, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, new List<PropertyInfo>
|
|
{
|
|
new PropertyInfo(Variant.Type.Float, "delta", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false)
|
|
}, null),
|
|
new MethodInfo(MethodName.PlayerHit, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, new List<PropertyInfo>
|
|
{
|
|
new PropertyInfo(Variant.Type.Object, "other", PropertyHint.None, "", PropertyUsageFlags.Default, new StringName("Node"), exported: false)
|
|
}, null),
|
|
new MethodInfo(MethodName.PlayerInside, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, new List<PropertyInfo>
|
|
{
|
|
new PropertyInfo(Variant.Type.Object, "other", PropertyHint.None, "", PropertyUsageFlags.Default, new StringName("Node"), exported: false)
|
|
}, null),
|
|
new MethodInfo(MethodName.PlayerOutside, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, new List<PropertyInfo>
|
|
{
|
|
new PropertyInfo(Variant.Type.Object, "other", PropertyHint.None, "", PropertyUsageFlags.Default, new StringName("Node"), exported: false)
|
|
}, null),
|
|
new MethodInfo(MethodName.Stop, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, null, null)
|
|
};
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
|
|
{
|
|
if (method == MethodName._EnterTree && args.Count == 0)
|
|
{
|
|
_EnterTree();
|
|
ret = default(godot_variant);
|
|
return true;
|
|
}
|
|
if (method == MethodName._Process && args.Count == 1)
|
|
{
|
|
_Process(VariantUtils.ConvertTo<double>(in args[0]));
|
|
ret = default(godot_variant);
|
|
return true;
|
|
}
|
|
if (method == MethodName.PlayerHit && args.Count == 1)
|
|
{
|
|
PlayerHit(VariantUtils.ConvertTo<Node>(in args[0]));
|
|
ret = default(godot_variant);
|
|
return true;
|
|
}
|
|
if (method == MethodName.PlayerInside && args.Count == 1)
|
|
{
|
|
PlayerInside(VariantUtils.ConvertTo<Node>(in args[0]));
|
|
ret = default(godot_variant);
|
|
return true;
|
|
}
|
|
if (method == MethodName.PlayerOutside && args.Count == 1)
|
|
{
|
|
PlayerOutside(VariantUtils.ConvertTo<Node>(in args[0]));
|
|
ret = default(godot_variant);
|
|
return true;
|
|
}
|
|
if (method == MethodName.Stop && args.Count == 0)
|
|
{
|
|
Stop();
|
|
ret = default(godot_variant);
|
|
return true;
|
|
}
|
|
return base.InvokeGodotClassMethod(in method, args, out ret);
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
protected override bool HasGodotClassMethod(in godot_string_name method)
|
|
{
|
|
if (method == MethodName._EnterTree)
|
|
{
|
|
return true;
|
|
}
|
|
if (method == MethodName._Process)
|
|
{
|
|
return true;
|
|
}
|
|
if (method == MethodName.PlayerHit)
|
|
{
|
|
return true;
|
|
}
|
|
if (method == MethodName.PlayerInside)
|
|
{
|
|
return true;
|
|
}
|
|
if (method == MethodName.PlayerOutside)
|
|
{
|
|
return true;
|
|
}
|
|
if (method == MethodName.Stop)
|
|
{
|
|
return true;
|
|
}
|
|
return base.HasGodotClassMethod(in method);
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
protected override bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value)
|
|
{
|
|
if (name == PropertyName.speed)
|
|
{
|
|
speed = VariantUtils.ConvertTo<Vector2>(in value);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.crates)
|
|
{
|
|
crates = VariantUtils.ConvertToSystemArrayOfGodotObject<AnimationPlayer>(in value);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.colliders)
|
|
{
|
|
colliders = VariantUtils.ConvertToSystemArrayOfGodotObject<Area2D>(in value);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.conveyors)
|
|
{
|
|
conveyors = VariantUtils.ConvertTo<Area2D>(in value);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.running)
|
|
{
|
|
running = VariantUtils.ConvertTo<bool>(in value);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.marker)
|
|
{
|
|
marker = VariantUtils.ConvertTo<Node2D>(in value);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.hits)
|
|
{
|
|
hits = VariantUtils.ConvertTo<int>(in value);
|
|
return true;
|
|
}
|
|
return base.SetGodotClassPropertyValue(in name, in value);
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
protected override bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value)
|
|
{
|
|
if (name == PropertyName.speed)
|
|
{
|
|
value = VariantUtils.CreateFrom(in speed);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.crates)
|
|
{
|
|
GodotObject[] array = crates;
|
|
value = VariantUtils.CreateFromSystemArrayOfGodotObject(array);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.colliders)
|
|
{
|
|
GodotObject[] array = colliders;
|
|
value = VariantUtils.CreateFromSystemArrayOfGodotObject(array);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.conveyors)
|
|
{
|
|
value = VariantUtils.CreateFrom(in conveyors);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.running)
|
|
{
|
|
value = VariantUtils.CreateFrom(in running);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.marker)
|
|
{
|
|
value = VariantUtils.CreateFrom(in marker);
|
|
return true;
|
|
}
|
|
if (name == PropertyName.hits)
|
|
{
|
|
value = VariantUtils.CreateFrom(in hits);
|
|
return true;
|
|
}
|
|
return base.GetGodotClassPropertyValue(in name, out value);
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
internal static List<PropertyInfo> GetGodotPropertyList()
|
|
{
|
|
return new List<PropertyInfo>
|
|
{
|
|
new PropertyInfo(Variant.Type.Vector2, PropertyName.speed, PropertyHint.None, "", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
|
|
new PropertyInfo(Variant.Type.Array, PropertyName.crates, PropertyHint.TypeString, "24/34:AnimationPlayer", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
|
|
new PropertyInfo(Variant.Type.Array, PropertyName.colliders, PropertyHint.TypeString, "24/34:Area2D", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
|
|
new PropertyInfo(Variant.Type.Object, PropertyName.conveyors, PropertyHint.NodeType, "Area2D", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
|
|
new PropertyInfo(Variant.Type.Bool, PropertyName.running, PropertyHint.None, "", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
|
|
new PropertyInfo(Variant.Type.Object, PropertyName.marker, PropertyHint.NodeType, "Node2D", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
|
|
new PropertyInfo(Variant.Type.Int, PropertyName.hits, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false)
|
|
};
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
protected override void SaveGodotObjectData(GodotSerializationInfo info)
|
|
{
|
|
base.SaveGodotObjectData(info);
|
|
info.AddProperty(PropertyName.speed, Variant.From(in speed));
|
|
StringName name = PropertyName.crates;
|
|
GodotObject[] array = crates;
|
|
info.AddProperty(name, Variant.CreateFrom(array));
|
|
StringName name2 = PropertyName.colliders;
|
|
array = colliders;
|
|
info.AddProperty(name2, Variant.CreateFrom(array));
|
|
info.AddProperty(PropertyName.conveyors, Variant.From(in conveyors));
|
|
info.AddProperty(PropertyName.running, Variant.From(in running));
|
|
info.AddProperty(PropertyName.marker, Variant.From(in marker));
|
|
info.AddProperty(PropertyName.hits, Variant.From(in hits));
|
|
}
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
protected override void RestoreGodotObjectData(GodotSerializationInfo info)
|
|
{
|
|
base.RestoreGodotObjectData(info);
|
|
if (info.TryGetProperty(PropertyName.speed, out var value))
|
|
{
|
|
speed = value.As<Vector2>();
|
|
}
|
|
if (info.TryGetProperty(PropertyName.crates, out var value2))
|
|
{
|
|
crates = value2.AsGodotObjectArray<AnimationPlayer>();
|
|
}
|
|
if (info.TryGetProperty(PropertyName.colliders, out var value3))
|
|
{
|
|
colliders = value3.AsGodotObjectArray<Area2D>();
|
|
}
|
|
if (info.TryGetProperty(PropertyName.conveyors, out var value4))
|
|
{
|
|
conveyors = value4.As<Area2D>();
|
|
}
|
|
if (info.TryGetProperty(PropertyName.running, out var value5))
|
|
{
|
|
running = value5.As<bool>();
|
|
}
|
|
if (info.TryGetProperty(PropertyName.marker, out var value6))
|
|
{
|
|
marker = value6.As<Node2D>();
|
|
}
|
|
if (info.TryGetProperty(PropertyName.hits, out var value7))
|
|
{
|
|
hits = value7.As<int>();
|
|
}
|
|
}
|
|
}
|