📄 vehicle.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace AI_Life
{
public struct Obstacles
{
public PointF Location;
public int Size;
public Obstacles(PointF location, int size)
{
Location = location;
Size = size;
}
}
enum SpecialPaths
{
AI
}
enum SB
{
None,
Seek,
Flee,
Arrive,
Pursuit,
Evade,
Wander,
PathFollowing,
Cohesion,
Alignment,
Separation,
CF,
FCAS,
FCS,
CS,
CA,
CAS
}
//represents a single vehicle
class Vehicle
{
private static Vehicle[] allCars;
//=========================================================//
//general variables
public static bool enableSpecialPath = false;
private bool targetChanged;
private float mass;
private int max_speed, max_force, vehicleNo;
private Vector2 currentPosition, velocity, acceleration, heading, steerForce, targetPosition;
private Brush brushBack, brushWhite, brushCar;
private Pen whitePen;
private Graphics g;
private static Random random;
private Size clientSize;
private SB SB;
public static Obstacles[] obstacles = new Obstacles[10]; //not used
//=========================================================//
//=========================================================//
//wander specific variables
private Vector2 wanderTarget;
public static float WanderRadius = 40, WanderDistance = 80;
public static int WanderJitter = 10;
//=========================================================//
//=========================================================//
//arrive specific variables
public static int ArriveRadius = 100;
//=========================================================//
//=========================================================//
//pathfollowing specific varibles
Point[] specialPathPoints, pathPoints, sP1, sP2;
int currentPathPoint, maxSpecialPathPoints, maxPathPoints;
//=========================================================//
//cohesion specific variables
public static int CohesionRadius = 100;
//=========================================================//
//=========================================================//
//flee specific variables
public static int FOV = 100; //field of view
//=========================================================//
public static bool weightedSum = true;
//the constructor
public Vehicle(Graphics GraphicsObject, Size ClientSize, SB SteeringBehavior, int VehicleNumber)
{
//=========================================================//
//general initialization
g = GraphicsObject;
clientSize = ClientSize;
random = new Random();
mass = random.Next(20, 80);
max_force = 20;
vehicleNo = VehicleNumber;
max_speed = 10;
if (mainForm.identicalVehicles)
{
velocity = new Vector2(5, 5);
}
else
{
velocity = new Vector2(random.Next(0, 10), random.Next(0, 10));
}
acceleration = new Vector2(0, 0);
heading = Vector2.Normalize(velocity);
currentPosition = new Vector2(random.Next(clientSize.Width), random.Next(clientSize.Height));
steerForce = new Vector2(0);
targetPosition = SBC.targetPosition;
brushBack = Brushes.Black;
brushWhite = Brushes.White;
brushCar = Brushes.Blue;
whitePen = Pens.White;
targetChanged = false;
SB = SteeringBehavior;
//=========================================================//
//=========================================================//
//wander specific variables
wanderTarget = new Vector2(0);
//=========================================================//
//=========================================================//
//=========================================================//
//pathfollowing s
maxSpecialPathPoints = 40;
maxPathPoints = 10;
currentPathPoint = 0;
specialPathPoints = new Point[maxSpecialPathPoints];
pathPoints = new Point[maxPathPoints];
sP1 = new Point[22];
sP2 = new Point[18];
NewPath();
}
//=========================================================//
#region Properties
public Vector2 Velocity
{
get
{
return velocity;
}
}
public Vector2 CurrentPosition
{
get
{
return currentPosition;
}
}
public SB SteeringBehaviour
{
get { return SB; }
set { SB = value; }
}
public bool TargetChanged
{
get { return targetChanged; }
set { targetChanged = value; }
}
public int MaxPathPoints
{
get { return maxSpecialPathPoints; }
set { maxSpecialPathPoints = value; }
}
public Vector2 TargetPosition
{
get { return targetPosition; }
set { targetPosition = value; }
}
public int MaxForce
{
get { return max_force; }
set { max_force = value; }
}
public int MaxSpeed
{
get { return max_speed; }
set { max_speed = value; }
}
public float Mass
{
get { return mass; }
set { mass = value; }
}
#endregion
//=========================================================//
private void DrawToBuffer()
{
PointF top = new PointF(heading.X * 14 + currentPosition.X, heading.Y * 14 + currentPosition.Y);
PointF left = new PointF(heading.Y * (-5) + currentPosition.X, heading.X * 5 + currentPosition.Y);
PointF right = new PointF(heading.Y * 5 + currentPosition.X, heading.X * (-5) + currentPosition.Y);
switch (random.Next(1, 4))
{
case 0:
brushCar = Brushes.Black;
break;
case 1:
brushCar = Brushes.Red;
break;
case 2:
brushCar = Brushes.White;
break;
case 3:
brushCar = Brushes.Blue;
break;
case 4:
brushCar = Brushes.LightGreen;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -