Initial commit
This commit is contained in:
		
							
								
								
									
										26
									
								
								scripts/Creature.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								scripts/Creature.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
public class Creature : KinematicBody2D
 | 
			
		||||
{
 | 
			
		||||
	[Export]
 | 
			
		||||
	public String creatureName = "UNKNOWN";
 | 
			
		||||
	public Health health = new Health();
 | 
			
		||||
	private Vector2 worldPosition = Vector2.Zero;
 | 
			
		||||
	private TileMap map { 
 | 
			
		||||
		get {
 | 
			
		||||
			return GetParent().GetChild<TileMap>(0);
 | 
			
		||||
		}}
 | 
			
		||||
 | 
			
		||||
	protected void Move(Vector2 movement)
 | 
			
		||||
	{
 | 
			
		||||
		// Generate new position in world and check
 | 
			
		||||
		// if location is a valid location to move to.
 | 
			
		||||
		Vector2 new_pos = worldPosition + movement;
 | 
			
		||||
		if (map.IsValidMapPosition(new_pos))
 | 
			
		||||
		{
 | 
			
		||||
			Position = map.MapToWorld(worldPosition); // Update position based on tile location
 | 
			
		||||
			worldPosition = new_pos; // Save new location
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								scripts/Enemy.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								scripts/Enemy.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
public class Enemy : Creature
 | 
			
		||||
{
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								scripts/Extenstions/TilemapExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								scripts/Extenstions/TilemapExtensions.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
 | 
			
		||||
namespace Godot
 | 
			
		||||
{
 | 
			
		||||
    public static class TilemapExtensions
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>method <c>IsValidMapPosition</c> returns a bool of whether a location in a tilemap has no colliders.</summary>
 | 
			
		||||
        public static bool IsValidMapPosition(this TileMap tileMap, Vector2 position)
 | 
			
		||||
        {
 | 
			
		||||
            int tile_id = tileMap.GetCell((int)position.x, (int)position.y);
 | 
			
		||||
            if(tile_id >= 0)
 | 
			
		||||
                return tileMap.TileSet.TileGetShapeCount(tile_id) == 0;
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								scripts/InputHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								scripts/InputHandler.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
 | 
			
		||||
/// <summary>Class <c>InputHandler</c> holds static methods
 | 
			
		||||
/// for handling player input.</summary>
 | 
			
		||||
public static class InputHandler
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    /// <summary>method <c>MovementInput</c> returns a Vector2 based on player input.</summary>
 | 
			
		||||
    public static Vector2 MovementInput()
 | 
			
		||||
	{
 | 
			
		||||
		Vector2 movement = new Vector2();
 | 
			
		||||
		if(Input.IsActionJustPressed("move_up"))
 | 
			
		||||
		{
 | 
			
		||||
			movement.y -= 1;
 | 
			
		||||
		}
 | 
			
		||||
		if(Input.IsActionJustPressed("move_down"))
 | 
			
		||||
		{
 | 
			
		||||
			movement.y += 1;
 | 
			
		||||
		}
 | 
			
		||||
		if(Input.IsActionJustPressed("move_right"))
 | 
			
		||||
		{
 | 
			
		||||
			movement.x += 1;
 | 
			
		||||
		}
 | 
			
		||||
		if(Input.IsActionJustPressed("move_left"))
 | 
			
		||||
		{
 | 
			
		||||
			movement.x -= 1;
 | 
			
		||||
		}
 | 
			
		||||
		return movement;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /// <summary>method <c>DebugKeyInput</c> returns bool when debug key is pressed in a debug build.</summary>
 | 
			
		||||
    public static bool DebugKeyInput()
 | 
			
		||||
    {
 | 
			
		||||
        if(OS.IsDebugBuild())
 | 
			
		||||
            return Input.IsActionJustPressed("debug_general");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								scripts/Interfaces/IStat.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								scripts/Interfaces/IStat.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Godot;
 | 
			
		||||
 | 
			
		||||
public delegate void StatUpdatedHandler(int newValue);
 | 
			
		||||
 | 
			
		||||
interface IStat
 | 
			
		||||
{
 | 
			
		||||
    int value {get;}
 | 
			
		||||
    int max {get;}
 | 
			
		||||
    int min {get;}
 | 
			
		||||
 | 
			
		||||
    event StatUpdatedHandler statUpdated;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								scripts/Player.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								scripts/Player.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
public class Player : Creature
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	[Signal]
 | 
			
		||||
	public delegate void UpdatePlayerGuiHandler(Player player);
 | 
			
		||||
	
 | 
			
		||||
	public override void _Ready()
 | 
			
		||||
	{
 | 
			
		||||
		// Update GUI on stat changes
 | 
			
		||||
		health.statUpdated += UpdateGui;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public override void _Process(float delta)
 | 
			
		||||
	{
 | 
			
		||||
		ProcessMovement();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	protected void ProcessMovement()
 | 
			
		||||
	{
 | 
			
		||||
		Move(InputHandler.MovementInput());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	protected void UpdateGui(int newValue = -1)
 | 
			
		||||
	{
 | 
			
		||||
		EmitSignal(nameof(UpdatePlayerGuiHandler), this);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								scripts/PlayerGui.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								scripts/PlayerGui.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
public class PlayerGui : CanvasLayer
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	ProgressBar healtbar;
 | 
			
		||||
	public override void _Ready()
 | 
			
		||||
	{
 | 
			
		||||
		healtbar = GetNode<ProgressBar>("Control/HealthBar");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void OnUpdatePlayerGui(Player player)
 | 
			
		||||
	{
 | 
			
		||||
		// Update healthbar
 | 
			
		||||
		if(healtbar != null)
 | 
			
		||||
		{
 | 
			
		||||
			healtbar.MaxValue = player.health.max;
 | 
			
		||||
			healtbar.MinValue = player.health.min;
 | 
			
		||||
			healtbar.Value = player.health.value;
 | 
			
		||||
		} else { GD.Print("WARNING: Healtbar not set!"); }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								scripts/Stats/Health.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								scripts/Stats/Health.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
public class Health : IStat
 | 
			
		||||
{
 | 
			
		||||
    public int value { get { return hp; }}
 | 
			
		||||
    public int max { get { return 100; }}
 | 
			
		||||
    public int min { get {return 0; }}
 | 
			
		||||
 | 
			
		||||
    private int hp = 100;
 | 
			
		||||
 | 
			
		||||
    public event StatUpdatedHandler statUpdated;
 | 
			
		||||
 | 
			
		||||
    public void Heal(int heal_amount)
 | 
			
		||||
    {
 | 
			
		||||
        int new_val = hp - heal_amount;
 | 
			
		||||
        if(new_val > max)
 | 
			
		||||
            hp = max;
 | 
			
		||||
        else
 | 
			
		||||
            hp = new_val;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Damage(int damange_amount)
 | 
			
		||||
    {
 | 
			
		||||
        int new_val = hp - damange_amount;
 | 
			
		||||
        if(new_val < min)
 | 
			
		||||
            hp = min;
 | 
			
		||||
        else
 | 
			
		||||
            hp = new_val;
 | 
			
		||||
        OnStatUpdated();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected void OnStatUpdated()
 | 
			
		||||
    {
 | 
			
		||||
        if(statUpdated != null)
 | 
			
		||||
            statUpdated(hp);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user