OutOfBody/Scripts/EvilEye.cs

147 lines
3.7 KiB
C#
Raw Permalink Normal View History

2024-01-02 20:10:53 -05:00
using Godot;
using System;
public partial class EvilEye : StaticBody2D
{
private Area2D probe; // probe for positioning lazer beams
private AnimatedSprite2D lidSprite; // animated sprite of eye lid
private AnimatedSprite2D ballSprite; // animated sprite of eye ball
private bool spriteStop; // for sprites which only happen once
public int frame = 0; // frame of lazer's animation
private int pause; // timer to help with syncing up the lazer beams
[Export]
public bool red; //do you want it cherry flavored?
[Export] //can be tweaked in inspector
public bool activated; // if activated
[Export]
public int direction; // left-1 right-2 up-3 down-4
private void _on_area_2d_area_entered(Area2D area)
{
if (area.IsInGroup("Cage")) // if probe detects an obstical
{
activated = !activated; //inverts the activation status of the eye (on -> off, off -> on)
}
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
//setting variables that can't be preloaded
probe = GetNode<Area2D>("FacingProbe");
lidSprite = GetNode<AnimatedSprite2D>("EvilEyeLid");
ballSprite = GetNode<AnimatedSprite2D>("EvilEyeBall");
//fun fact: despite only having one animation, it has to be activated within the code or else it's a still image
if(red == false)
{
ballSprite.Play("Idle");
}
if(red == true)
{
ballSprite.Play("RedIdle");
}
//direction for placing newly spawned lazerbeams
switch (direction)
{
case 1: lidSprite.Rotation = 3.1416f; probe.Position = new Vector2(-20,0); break;
case 2: lidSprite.Rotation = 0; probe.Position = new Vector2(20,0); break;
case 3: lidSprite.Rotation = 4.712f; probe.Position = new Vector2(0,-20); break;
case 4: lidSprite.Rotation = 1.5708f; probe.Position = new Vector2(0,20); break;
}
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
//everything below this is so that all of the lazers sync with eachother
if (pause == 0)
{
//allowing the lazers to move
for(int i = 0; i < GetChildCount(); i++)
{
if(GetChild(i).IsInGroup("Lazer"))
{
var child = GetChild(i) as lazer;
child.pause = 0;
}
}
//creates more lazers if activated
if(activated == true)
{
var packedLazer = GD.Load<PackedScene>("res://Entities/Lazer.tscn");
var lazer = packedLazer.Instantiate() as lazer;
lazer.Position = probe.Position;
lazer.direction = direction;
AddChild(lazer);
}
//this syncs animations
if(red == false)
{
for(int i = 0; i < GetChildCount(); i++)
{
if(GetChild(i).IsInGroup("Lazer"))
{
var child = GetChild(i) as lazer;
child.sprite.Play(frame.ToString());
}
}
}
if(red == true)
{
for(int i = 0; i < GetChildCount(); i++)
{
if(GetChild(i).IsInGroup("Lazer"))
{
var child = GetChild(i) as lazer;
child.sprite.Play("Red" + frame.ToString());
}
}
}
//small frame timer
frame++;
if(frame > 3) {frame = 0;}
pause = 3;
}
else
{
pause--;
}
animations();
}
private void animations()
{
//since these are one time only animations, I need to stop them from looping
//similar to the cage, these are dependant on activation status and not a timer
if(red == false)
{
if (activated == true && spriteStop == false)
{
lidSprite.Play("Active");
spriteStop = true;
}
if (activated == false)
{
lidSprite.Play("Idle");
spriteStop = false;
}
}
if(red == true)
{
if (activated == true && spriteStop == false)
{
lidSprite.Play("RedActive");
spriteStop = true;
}
if (activated == false)
{
lidSprite.Play("RedIdle");
spriteStop = false;
}
}
}
}