OutOfBody/Scripts/Ghost.cs

188 lines
5.2 KiB
C#

using Godot;
using System;
public partial class Ghost : StaticBody2D
{
//I just tweaked the player code, cause it works.
private Area2D probe; // probe to find the tile in the in the direction of the ghost's movement
private AnimatedSprite2D sprite; // animated sprite of ghost
private AudioStreamPlayer GhostMove;//some audio tracks
private AudioStreamPlayer GhostCage;
private Vector2 spriteMovement; // 2d vector dictating sprite's independant movement
private bool canMove = true; // if next step can be taken
private bool blocked; // if the player should noy move
private int pause; //timer for when processing needs to occur
private int idle = 99; // timer for idling based actions (animations mostly) starts high enough to skip certain animations on startup
public bool activated; //if activated by player
public bool facing; //if player is facing
public bool moving; //probably redundant but it works
public float storedSpin; //what the actual rotation of the object is used mostly for the sprite
private void _on_probe_body_entered(Node2D body)
{
if (body.IsInGroup("Wall")) // if probe detects walls
{
blocked = true; // stops from moving
canMove = false;
activated = false; //stops the constant movement from reoccuring until the player reactivates it
}
if (body.IsInGroup("Obsticals") && !body.IsInGroup("Cage") && !body.IsInGroup("Lazer")) // if probe detects an obstical that isn't a cage or lazer
{
blocked = true; //stops from moving
canMove = false;
activated = false; //stops the constant movement from reoccuring until the player reactivates it
}
if (body == GetNode<StaticBody2D>("/root/OutofBody/Chloek")) // if probe detects the player somehow
{
blocked = true; //stops from moving
canMove = false;
activated = false; //stops the constant movement from reoccuring until the player reactivates it
}
}
private void _on_probe_body_exited(Node2D body)
{
if (body.IsInGroup("Wall")) //if probe detects walls
{
blocked = false; // allow moving
}
if (body.IsInGroup("Obsticals") && !body.IsInGroup("Lazer")) //if probe detects a non-lazer obstical
{
blocked = false; // allow moving
}
if (body == GetNode<StaticBody2D>("/root/OutofBody/Chloek")) //if probe detects player
{
blocked = false; // allow moving
}
}
private void _on_ghost_area_area_entered(Area2D area)
{
if (area == GetNode<Area2D>("/root/OutofBody/Chloek/Probe"))
{
facing = true; //the player is facing the ghost
}
}
private void _on_ghost_area_area_exited(Area2D area)
{
if (area == GetNode<Area2D>("/root/OutofBody/Chloek/Probe"))
{
facing = false; //the player is no longer facing the ghost
}
}
public override void _Ready() // on startup
{
// setting starting coditions for variables
probe = GetNode<Area2D>("Probe");
sprite = GetNode<AnimatedSprite2D>("GhostSprite");
GhostMove = GetNode<AudioStreamPlayer>("GhostMove");
GhostCage = GetNode<AudioStreamPlayer>("GhostCage");
spriteMovement = Position;
}
public override void _PhysicsProcess(double delta) // updates multipul times a frame so movment happens propperly
{
//finished tasks become functions
if (pause == 0)
{
move();
}
else {pause--;}
animations();
}
public void animations()
{
idle++;
if (sprite.GlobalPosition != Position) //if intending to move
{
if (idle > 0)
{
idle = 0;
}
}
//all time based animations here
switch (idle)
{
case 0: sprite.Play("Moving"); break;
case 1: sprite.Play("Stopping"); break;
case 60: sprite.Play("Pre-Idle"); break;
case 100: sprite.Play("Idle"); break;
}
if (idle >= 60)
{
sprite.Rotation = 0;
}
else if (idle == 0)
{
sprite.Rotation = storedSpin;
}
spriteMovement = spriteMovement.MoveToward(Position, 10f); // sprite moves towards ghost's position
sprite.GlobalPosition = spriteMovement; //seems redundant
}
public void move()
{
if (canMove == true && blocked == false && facing == true || moving == true && blocked == false && canMove == true) //if ghost can move
{
GhostMove.Play();
spriteMovement = Position; //sprite snaps to previous position
Position = probe.GlobalPosition; //ghost position changes to probe's position
moving = true; //allows constant movement
}
else
{
//disables constant movement
canMove = false;
activated = false;
moving = false;
}
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("Action"))
{
//actions are handled by the ghost not the player
activated = true;
canMove = true;
}
if(activated == false)
{
//all rotations are stored as radians not degrees
if (@event.IsActionPressed("Up"))
{
probe.Position = new Vector2(0,-20);
storedSpin = 4.712f; //sprite flips to face up
pause = 3;
}
else if (@event.IsActionPressed("Down"))
{
probe.Position = new Vector2(0,20);
storedSpin = 1.5708f; //sprite flips to face down
pause = 3;
}
else if (@event.IsActionPressed("Left"))
{
probe.Position = new Vector2(-20,0);
storedSpin = 3.1416f; //sprite flips to face left
pause = 3;
}
else if (@event.IsActionPressed("Right"))
{
probe.Position = new Vector2(20,0);
storedSpin = 0; //sprite flips to face right
pause = 3;
}
}
}
}