Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
FArekkusu authored Nov 29, 2020
1 parent 96ce3b6 commit 91ddbea
Show file tree
Hide file tree
Showing 27 changed files with 218 additions and 142 deletions.
11 changes: 8 additions & 3 deletions Hunter/HunterGame/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class Camera
{
public const int MinZoom = 25;
public const int MaxZoom = 100;
public const int DefaultZoom = 75;
public const int DefaultZoom = 70;
public const int ZoomStep = 1;

public int ZoomLevel = DefaultZoom;
public Matrix Zoom = Matrix.CreateScale(DefaultZoom / 100f, DefaultZoom / 100f, 0);
public Matrix Zoom = CreateZoomMatrix(DefaultZoom);
public Matrix Transform;

public void Update()
Expand All @@ -28,7 +28,12 @@ public void Update()
public void UpdateZoom(int change)
{
ZoomLevel = Math.Max(MinZoom, Math.Min(MaxZoom, ZoomLevel + change));
Zoom = Matrix.CreateScale(ZoomLevel / 100f, ZoomLevel / 100f, 0);
Zoom = CreateZoomMatrix(ZoomLevel);
}

public static Matrix CreateZoomMatrix(int zoomLevel)
{
return Matrix.CreateScale(zoomLevel / 100f, zoomLevel / 100f, 0);
}

public void Follow(Vector2 target, Vector2 centerOffset)
Expand Down
50 changes: 34 additions & 16 deletions Hunter/HunterGame/GameObjects/Animals/Doe.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HunterGame.GameObjects.Boid;
using HunterGame.GameObjects.Boid.Interfaces;
using HunterGame.GameObjects.Player;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Animals
{
public class Doe : Boid
public class Doe : Boid.Boid, IWandering, IFleeing
{
public const double FleeingSpeed = 225;
public const double WanderingSpeed = 75;
public const double MaxForce = 5;
public const double TextureIndependentFearDistance = 70;
public const double TextureIndependentCalmingDistance = 100;
public const double TextureIndependentFearDistance = 125;
public const double TextureIndependentCalmingDistance = 175;

public readonly double FearDistance;
public readonly double CalmingDistance;
Expand Down Expand Up @@ -46,6 +49,8 @@ public override void Update(GameTime gameTime, WorldState worldState)
Wander(elapsedTime, worldState);
else if (State == BoidState.Fleeing)
Flee(elapsedTime, worldState);
else
throw new IncorrectBoidStateException($"Doe cannot be in state {State}");
}

public void Wander(double elapsedTime, WorldState worldState)
Expand All @@ -60,45 +65,58 @@ public void Wander(double elapsedTime, WorldState worldState)
}

var otherDoes = close.OfType<Doe>().ToList();
var closeDoesFromOtherGroup = otherDoes.Where(doe => doe.Group != Group && (doe.CenterPosition - CenterPosition).Length() <= FearDistance).ToList();

if (this == Group.Leader && !Group.IsComplete)
TryCompleteGroup(otherDoes);

var borderAvoidingForce = GetBordersAvoidingForce(FleeingSpeed, MaxForce);
var separationForce = GetSeparationForce(otherDoes, SeparationActivationDistance, WanderingSpeed, MaxForce);

var speed = WanderingSpeed;

if (this == Group.Leader)
HandleLeader(closeDoesFromOtherGroup, worldState.Random, borderAvoidingForce);
HandleLeader(worldState.Random, borderAvoidingForce);
else
HandleFollower(ref separationForce);
HandleFollower(ref speed, ref separationForce);

Acceleration += borderAvoidingForce * 2;
Acceleration += separationForce * 0.15f;

ApplyForces(WanderingSpeed * elapsedTime);
ApplyForces(speed * elapsedTime);
}

public void HandleLeader(List<Doe> closeDoesFromOtherGroup, Random random, Vector2 borderAvoidingForce)
public void TryCompleteGroup(IEnumerable<Doe> otherDoes)
{
if (!Group.IsComplete && closeDoesFromOtherGroup.Count > 0)
var closest = otherDoes.FirstOrDefault(doe => doe.Group != Group && (doe.CenterPosition - CenterPosition).Length() <= FearDistance);

if (closest != null)
{
var otherGroup = closeDoesFromOtherGroup[0].Group;
var otherGroup = closest.Group;

Group.Join(otherGroup);

if (otherGroup.Size > DoeGroup.MaximumSize)
otherGroup.SplitIntoHalves();
}
else
CheckArrival(ref WanderTarget, random, borderAvoidingForce);
}

public void HandleLeader(Random random, Vector2 borderAvoidingForce)
{
CheckArrival(ref WanderTarget, random, borderAvoidingForce);

Acceleration += GetArrivalForce(WanderTarget, WanderingSpeed, MaxForce) * 2;
}

public void HandleFollower(ref Vector2 separationForce)
public void HandleFollower(ref double speed, ref Vector2 separationForce)
{
Acceleration += GetArrivalForce(Group.Leader.CenterPosition, WanderingSpeed, MaxForce) * 0.05f;

if ((Group.Leader.CenterPosition - CenterPosition).Length() <= SeparationBoostMargin)

var distanceToLeader = (Group.Leader.CenterPosition - CenterPosition).Length();

if (distanceToLeader <= SeparationBoostMargin)
separationForce *= 10;
else if (distanceToLeader >= CalmingDistance)
speed = FleeingSpeed;
}

public void StartWandering(Random random)
Expand Down
14 changes: 7 additions & 7 deletions Hunter/HunterGame/GameObjects/Animals/DoeGroup.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System.Collections.Generic;
using System.Linq;

namespace HunterGame
namespace HunterGame.GameObjects.Animals
{
public class DoeGroup
{
public const int MinimumCompleteSize = 3;
public const int MaximumSize = 10;

public List<Doe> Members = new List<Doe>();

public Doe Leader => Members[0];

public int Size => Members.Count;
public bool IsComplete => Size >= MinimumCompleteSize && Size <= MaximumSize;

Expand All @@ -34,13 +34,13 @@ public void SplitIntoHalves()
{
var newGroup = new DoeGroup();

var left = Members.Take(Size / 2);
var moved = Members.Skip(Size / 2);
var staying = Members.Take(Size / 2);
var leaving = Members.Skip(Size / 2);

foreach (var member in moved)
foreach (var member in leaving)
newGroup.Add(member);

Members = left.ToList();
Members = staying.ToList();

newGroup.Leader.WanderTarget = newGroup.Leader.CenterPosition;
}
Expand Down
13 changes: 8 additions & 5 deletions Hunter/HunterGame/GameObjects/Animals/Hare.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System.Linq;
using HunterGame.GameObjects.Boid;
using HunterGame.GameObjects.Boid.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Animals
{
public class Hare : Boid
public class Hare : Boid.Boid, IWandering, IFleeing
{

public const double FleeingSpeed = 375;
public const double WanderingSpeed = 37.5;
public const double MaxForce = 5;
public const double TextureIndependentFearDistance = 50;
public const double TextureIndependentCalmingDistance = 100;
public const double TextureIndependentFearDistance = 75;
public const double TextureIndependentCalmingDistance = 175;

public readonly double FearDistance;
public readonly double CalmingDistance;
Expand All @@ -36,6 +37,8 @@ public override void Update(GameTime gameTime, WorldState worldState)
Wander(elapsedTime, worldState);
else if (State == BoidState.Fleeing)
Flee(elapsedTime, worldState);
else
throw new IncorrectBoidStateException($"Hare cannot be in state {State}");
}

public void Wander(double elapsedTime, WorldState worldState)
Expand Down
55 changes: 31 additions & 24 deletions Hunter/HunterGame/GameObjects/Animals/Wolf.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Geometry;
using HunterGame.GameObjects.Bases;
using HunterGame.GameObjects.Boid;
using HunterGame.GameObjects.Boid.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Animals
{
public class Wolf : Boid
public class Wolf : Boid.Boid, IWandering, IFollowing
{
public const double HuntingSpeed = 300;
public const double WanderingSpeed = 75;
public const double MaxForce = 5;
public const double TextureIndependentAgroDistance = 60;
public const double TextureIndependentCalmingDistance = 100;
public const double LifeTime = 30;
public const double StarvationTime = 25;
public const double TextureIndependentAgroDistance = 100;
public const double TextureIndependentCalmingDistance = 150;
public const double LifeTime = 40;
public const double StarvationTime = 30;

public readonly double AgroDistance;
public readonly double CalmingDistance;
Expand All @@ -38,30 +42,29 @@ public override void Update(GameTime gameTime, WorldState worldState)

LifeTimer -= elapsedTime;

if (LifeTimer <= 0)
{
IsAlive = false;
return;
}

if (State == BoidState.Wandering)
Wander(elapsedTime, worldState);
else if (State == BoidState.Following)
Follow(elapsedTime, worldState);

foreach (var creature in worldState.Creatures)
if (creature.IsAlive && !(creature is Wolf) && CollisionTester.TestCircleCircle(Circle, creature.Circle))
{
creature.IsAlive = false;
LifeTimer = LifeTime;
State = BoidState.Wandering;
}
else
throw new IncorrectBoidStateException($"Wolf cannot be in state {State}");

if (LifeTimer <= StarvationTime)
foreach (var creature in worldState.Creatures)
if (creature.IsAlive && !(creature is Wolf) && CollisionTester.TestCircleCircle(Circle, creature.Circle))
{
creature.IsAlive = false;
LifeTimer = LifeTime;
State = BoidState.Wandering;
}

if (LifeTimer <= 0)
IsAlive = false;
}

public void Wander(double elapsedTime, WorldState worldState)
{
var close = FindInRadius(worldState.Creatures, AgroDistance);
var preyFound = close.Any(creature => !(creature is Wolf));
var preyFound = FindPrey(worldState.Creatures, AgroDistance).Any();

if (preyFound && LifeTimer <= StarvationTime)
{
Expand All @@ -88,8 +91,7 @@ public void StartWandering(Random random)

public void Follow(double elapsedTime, WorldState worldState)
{
var close = FindInRadius(worldState.Creatures, CalmingDistance);
var prey = close.Where(creature => !(creature is Wolf)).ToList();
var prey = FindPrey(worldState.Creatures, CalmingDistance).ToList();

if (prey.Count == 0)
{
Expand All @@ -109,5 +111,10 @@ public void StartFollowing()
{
State = BoidState.Following;
}

public IEnumerable<Creature> FindPrey(IEnumerable<Creature> creatures, double distance)
{
return FindInRadius(creatures, distance).Where(creature => !(creature is Wolf));
}
}
}
2 changes: 1 addition & 1 deletion Hunter/HunterGame/GameObjects/Bases/CircularObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Bases
{
public abstract class CircularObject : GameObject
{
Expand Down
2 changes: 1 addition & 1 deletion Hunter/HunterGame/GameObjects/Bases/Creature.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Bases
{
public abstract class Creature : CircularObject
{
Expand Down
2 changes: 1 addition & 1 deletion Hunter/HunterGame/GameObjects/Bases/GameObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Bases
{
public abstract class GameObject
{
Expand Down
2 changes: 1 addition & 1 deletion Hunter/HunterGame/GameObjects/Bases/RectangularObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Bases
{
public abstract class RectangularObject : GameObject
{
Expand Down
13 changes: 7 additions & 6 deletions Hunter/HunterGame/GameObjects/Boid/Boid.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using HunterGame.GameObjects.Bases;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HunterGame
namespace HunterGame.GameObjects.Boid
{
public abstract class Boid : Creature
{
Expand All @@ -22,11 +23,11 @@ protected Boid(Texture2D texture, Vector2 position) : base(texture, position)
}

public virtual void Update(GameTime gameTime, WorldState worldState) {}

public IEnumerable<Creature> FindInRadius(IEnumerable<Creature> creatures, double radius)
{
foreach (var creature in creatures)
if (creature != this && (creature.CenterPosition - CenterPosition).Length() <= radius)
if (creature != this && (creature.CenterPosition - CenterPosition).Length() - creature.Texture.Width / 2.0 <= radius)
yield return creature;
}

Expand All @@ -42,17 +43,17 @@ public void CheckArrival(ref Vector2 target, Random random, Vector2 borderAvoidi

public Vector2 ChooseNewTarget(Random random, bool keepDirection = true)
{
var angle = MathHelper.ToRadians(random.Next(360));
var angle = (float)random.Next(360);

if (Velocity != Vector2.Zero && keepDirection)
{
var oldAngle = MathHelper.ToDegrees((float)Math.Atan2(Velocity.Y, Velocity.X));

angle = oldAngle + random.Next(-MaximumAngleChange, MaximumAngleChange + 1);

angle = MathHelper.ToRadians(angle);
}

angle = MathHelper.ToRadians(angle);

var direction = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));

var distance = random.Next(100, 151);
Expand Down
2 changes: 1 addition & 1 deletion Hunter/HunterGame/GameObjects/Boid/BoidState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace HunterGame
namespace HunterGame.GameObjects.Boid
{
public enum BoidState
{
Expand Down
Loading

0 comments on commit 91ddbea

Please sign in to comment.