OutOfBody/Scripts/Chloek.cs

166 lines
4.5 KiB
C#
Raw Permalink Normal View History

2024-01-02 20:10:53 -05:00
using Godot;
using System;
public partial class Chloek : StaticBody2D
{
private Area2D probe; // probe to find the tile in the in the direction of the player's movement
private AnimatedSprite2D sprite; // animated sprite of player
public Vector2 spriteMovement; // 2d vector dictating sprite's independant movement
private AudioStreamPlayer Interact;
private AudioStreamPlayer Reset;
private AudioStreamPlayer Snore;
private bool canMove = true; // if the player can move
private bool blocked; // if the player shouldn't move
private int pause; //timer for when processing needs to occur
private int idle; // timer for idling based actions (animations mostly)
private int snoreTimer; //timer for snoring
private Vector2 movement; //last movement direction
private void _on_probe_body_entered(Node2D body) //a bit slow to respond, but it does it's job most of the time
{
if (body.IsInGroup("Wall")) // if probe detects walls
{
blocked = true; // stops from moving
}
if (body.IsInGroup("Obsticals") && !body.IsInGroup("Lazer")) // if probe detects an obstical that isnt a lazer
{
blocked = true; //stops from moving;
}
// previous two can probably be lumped into one if statement
}
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 an obstical that isnt a lazer
{
blocked = false; // allow moving
}
}
private void _on_chloek_area_body_entered(Node2D body)
{
//this "bounces" the player back if they enter a lazer
//I only need this becuase Lazers break normal collision checks
if (body.IsInGroup("Lazer"))
{
pause = 0;
canMove = true;
probe.Position = movement * -1;
}
}
public override void _Ready() // on startup
{
// setting starting coditions for variables
probe = GetNode<Area2D>("Probe");
sprite = GetNode<AnimatedSprite2D>("ChloekSprite");
Interact = GetNode<AudioStreamPlayer>("Interact");
Reset = GetNode<AudioStreamPlayer>("Reset");
Snore = GetNode<AudioStreamPlayer>("Snore");
spriteMovement = Position;
}
public override void _PhysicsProcess(double delta) // can update multipul times a frame so movment happens propperly
{
//finished tasks become functions
if (pause == 0)
{
move();
animations();
}
else {pause--;}
}
public void animations()
{
idle++;
if (sprite.GlobalPosition != Position) //if intending to move
{
if (idle > 0)
{
idle = 0;
}
}
switch (idle) //switch case for all time based animations
{
case 0: sprite.Play("Walk"); break;
case 1: sprite.Play("Idle"); sprite.Stop(); break;
case 1200: sprite.Play("Idle"); break;
case 1900: sprite.Play("Eepy"); break;
}
if(idle >= 1900)
{
if(snoreTimer > 400)
{
Snore.Play();
snoreTimer = 0;
}
else {snoreTimer++;}
}
else {snoreTimer = 0;}
spriteMovement = spriteMovement.MoveToward(Position, 1f); // sprite lags behind player position slightly
sprite.GlobalPosition = spriteMovement; //seems redundant but is nessesary
}
public void move()
{
if (canMove == true && blocked == false) //if player can move and isnt walking into something
{
spriteMovement = Position; //sprite snaps to previous position
Position = probe.GlobalPosition; //player position changes to probe's position
canMove = false; //prevents the player from moving every chance they get
}
else
{
canMove = false; //prevents the player from walking as soon as they aren't blocked
}
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("Reset"))
{
Reset.Play();
}
if(pause == 0) //pauses delay movement enough so that you can't walk into walls
{
if (@event.IsActionPressed("Up"))
{
movement = new Vector2(0,-20);
canMove = true;
pause = 3;
}
else if (@event.IsActionPressed("Down"))
{
movement = new Vector2(0,20);
canMove = true;
pause = 3;
}
else if (@event.IsActionPressed("Left"))
{
movement = new Vector2(-20,0);
sprite.FlipH = true; //sprite flips to face left
canMove = true;
pause = 3;
}
else if (@event.IsActionPressed("Right"))
{
movement = new Vector2(20,0);
sprite.FlipH = false; //sprite flips to face right
canMove = true;
pause = 3;
}
probe.Position = movement; //i could do this without movement, but i need it so that other sections can use it.
}
}
}