Skip to content

Commit

Permalink
add-basic-enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
BBQGiraffe committed Jul 22, 2021
1 parent 119efa8 commit 1e7b61b
Show file tree
Hide file tree
Showing 34 changed files with 729 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,4 @@ FodyWeavers.xsd
*.PAL
*.exe
*.pdb
bin/
3 changes: 1 addition & 2 deletions AnimatedWall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public class AnimatedWall : Tile
int startSprite;
public AnimatedWall(int start, int count)
{
anim.current = new Animation(start, count, 1000 / count);
anim.index = start;
anim.LoadAnimation(new Animation(start, count, 1000 / count));
}
public override void Create()
{
Expand Down
53 changes: 43 additions & 10 deletions AnimationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ public sealed class AnimationFrame

public sealed class Animation
{
static int animCount = 0;
public int id;
public int duration; //duration in ms
public List<AnimationFrame> frames = new List<AnimationFrame>();
public bool loop;

public Animation(int start, int count, int duration, bool loop = true)
public Action onEnd;

public Animation(int start, int count, int duration, bool loop = true, Action onEnd = null)
{
for (int i = start; i < start + count; i++)
{
Expand All @@ -24,6 +28,11 @@ public Animation(int start, int count, int duration, bool loop = true)
frames.Add(anim);
}

id = animCount;
animCount++;

this.onEnd = onEnd;

this.duration = duration;
this.loop = loop;
}
Expand All @@ -33,33 +42,57 @@ public sealed class AnimationHandler
{

public int index;
public Animation current;
int frameIndex = 0;
Animation current;
float timer;
int currentID = -1;


//returns false if asked to play the same animation or if the current animation is not completed
public bool LoadAnimation(Animation anim)
{

if(currentID == anim.id){return false;}

if(currentID > -1)
{
//return if current anim is not done
if(frameIndex < current.frames.Count-1){return false;}
}


current = anim;
currentID = anim.id;
index = current.frames[0].index;
timer = 0;
frameIndex = 0;
return true;
}

public void Update()
{
timer += Time.dt * 1000;
if (timer > current.duration)
{
timer = 0;
int end = current.frames[current.frames.Count - 1].index;
int start = current.frames[0].index;

index++;
if (index > end)
frameIndex++;
if (frameIndex >= current.frames.Count)
{
if (current.loop)
{
index = current.frames[0].index;
frameIndex = 0;
}
else
{
index = end;
frameIndex = current.frames.Count-1;
}
}
current.onEnd?.Invoke();
timer = 0;
}


}
index = current.frames[frameIndex].index;
}
}
}
6 changes: 5 additions & 1 deletion Direction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ namespace Nitemare3D
public enum Direction //never eat soggy waffles
{
North,
NorthEast,
East,
SouthEast,
South,
West
SouthWest,
West,
NorthWest
}
}
162 changes: 162 additions & 0 deletions DirectionalGuard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
namespace Nitemare3D
{


public class DirectionalGuard : Entity, ISprite
{
public GuardType type;
byte angle; //8 possible angles;

public int spriteIndex {get; set;}
public Vec2 spritePosition{get;set;} = new Vec2();
public bool visible{get;set;} = true;
public float yOffset{get;set;}

Vec2 velocity = new Vec2();
Direction direction;
float speed = 1f;





public DirectionalGuard(GuardType type, Direction dir)
{
switch (type)
{
case GuardType.HumanGreen:
texOffset = 482;
break;
case GuardType.HumanBlue:
texOffset = 434;
break;
case GuardType.Witch:
break;
case GuardType.Robot:
break;
case GuardType.Penelope:
break;
case GuardType.DRHammerstein:
break;
}
this.direction = dir;
Game.player.AddSprite(this);
}

int texOffset;

GuardState state = GuardState.patrol;
/*
Nitemare 3D has certain tiles used to guide the patrol state of certain guards,
pretty clever I think
*/


Vec2 MoveInCompassDirection()
{
switch (direction)
{
case Direction.NorthWest:
return new Vec2(-1, -1);
case Direction.North:
return new Vec2(0, -1);
case Direction.NorthEast:
return new Vec2(1, -1);
case Direction.East:
return new Vec2(1, 0);
case Direction.SouthEast:
return new Vec2(1, 1);
case Direction.South:
return new Vec2(0, 1);
case Direction.SouthWest:
return new Vec2(-1, 1);
case Direction.West:
return new Vec2(-1, 0);
default:
return new Vec2();
}

}
void UpdatePatrol()
{
var x = MathF.Round(position.X);
var y = MathF.Round(position.Y);
var tile = Level.tilemap[(int)x, (int)y];

//never eat soggy waffles
switch(tile.type)
{
case WallType.turningpointN:
direction = Direction.North;
break;
case WallType.turningpointNE:
direction = Direction.NorthEast;
break;
case WallType.turningpointNW:
direction = Direction.NorthWest;
break;
case WallType.turningpointE:
direction = Direction.East;
break;
case WallType.turningpointS:
direction = Direction.South;
break;
case WallType.turningpointSE:
direction = Direction.SouthEast;
break;
case WallType.turningpointSW:
direction = Direction.SouthWest;
break;
case WallType.turningpointW:
direction = Direction.West;
break;


}
velocity = MoveInCompassDirection();
}
void UpdateGuard()
{
switch (state)
{
case GuardState.idle:
break;
case GuardState.chasing:
break;
case GuardState.attacking:
break;
case GuardState.dead:
break;
case GuardState.patrol:
UpdatePatrol();
break;
}
}



void HandleAnimation()
{

}

public override void Update()
{
//todo calculate direction from velocity
HandleAnimation();
UpdateGuard();



position += velocity * speed * Time.dt;


spritePosition = position;

spriteIndex = texOffset + (int)direction * 4;


}
}
}
3 changes: 2 additions & 1 deletion DumbObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DumbObject : Entity, ISprite
public bool visible{get;set;} = true;
public float yOffset{get;set;}
public bool raised = false;
public DumbObject(int index, bool raised = false)
public DumbObject(int index, bool raised = false, bool hasCollision = true)
{
spriteIndex = index;
Game.player.AddSprite(this);
Expand All @@ -18,6 +18,7 @@ public DumbObject(int index, bool raised = false)
{
yOffset = 64 + Img.current.entries[index].height;
}
this.hasCollision = hasCollision;
}


Expand Down
6 changes: 6 additions & 0 deletions Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Nitemare3D
public class Entity
{
public Vec2 position = new Vec2();
public int id;
static int entCount;
public virtual void Update()
{

Expand All @@ -15,6 +17,8 @@ public virtual void Start()

}

public bool hasCollision = true;


public static List<Entity> entities = new List<Entity>();
static List<Entity> entityQueue = new List<Entity>();
Expand Down Expand Up @@ -63,6 +67,8 @@ public static void UpdateEntites()
}
foreach (var entity in entityQueue)
{
entity.id = entCount;
entCount++;
entity.Start();
entities.Add(entity);
}
Expand Down
2 changes: 1 addition & 1 deletion Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Game : Scene
MidiConsts.MIDI_E1M1,
MidiConsts.MIDI_INTERMISSION
};

public const string gameTitle = "Nitemare 3D 0.46";



Expand Down
Loading

0 comments on commit 1e7b61b

Please sign in to comment.