Server job assigning logic, submarine movement syncing, submarine collision improvements, spawnpoints in levels

This commit is contained in:
Regalis
2015-07-08 11:37:47 +03:00
parent 3af9b8183b
commit d56f7f3f77
155 changed files with 39772 additions and 261 deletions
+61 -21
View File
@@ -51,6 +51,11 @@ namespace Subsurface
get { return ConvertUnits.ToDisplayUnits(cells[0].body.Position); }
}
public string Seed
{
get { return seed; }
}
public Level(string seed, int width, int height, int siteInterval)
{
this.seed = seed;
@@ -66,7 +71,7 @@ namespace Subsurface
{
seed = Rand.Range(0, int.MaxValue, false).ToString();
}
return new Level((string)seed, 100000, 40000, 2000);
return new Level(seed, 100000, 40000, 2000);
}
public void Generate(float minWidth)
@@ -86,7 +91,10 @@ namespace Subsurface
Voronoi voronoi = new Voronoi(1.0);
List<Vector2> sites = new List<Vector2>();
Random rand = new Random(ToolBox.SeedToInt(seed));
int aa = seed.GetHashCode();
Random rand = new Random(seed.GetHashCode());
float siteVariance = siteInterval * 0.8f;
for (int x = siteInterval / 2; x < borders.Width; x += siteInterval)
@@ -153,21 +161,29 @@ namespace Subsurface
//generate a path from the left edge of the map to right edge
Rectangle pathBorders = new Rectangle(
borders.X + (int)minWidth, borders.Y + (int)minWidth,
borders.Right - (int)minWidth, borders.Y + borders.Height - (int)minWidth);
borders.X + (int)minWidth * 2, borders.Y + (int)minWidth * 2,
borders.Right - (int)minWidth * 4, borders.Y + borders.Height - (int)minWidth * 4);
List<VoronoiCell> pathCells = GeneratePath(rand,
new Vector2((int)minWidth, rand.Next((int)minWidth, borders.Height - (int)minWidth)),
new Vector2(borders.Width - (int)minWidth, rand.Next((int)minWidth, borders.Height - (int)minWidth)),
new Vector2((int)minWidth * 2, rand.Next((int)minWidth * 2, borders.Height - (int)minWidth * 2)),
new Vector2(borders.Width - (int)minWidth * 2, rand.Next((int)minWidth * 2, borders.Height - (int)minWidth * 2)),
cells, pathBorders, minWidth);
for (int i = 0; i <3 ; i++ )
{
Vector2 position = pathCells[rand.Next((int)(pathCells.Count*0.5f), pathCells.Count - 2)].Center;
WayPoint wayPoint = new WayPoint(new Rectangle((int)position.X, (int)position.Y, 10, 10));
wayPoint.MoveWithLevel = true;
wayPoint.SpawnType = SpawnType.Enemy;
}
//generate a couple of random paths
for (int i = 0; i < rand.Next() % 3; i++)
{
pathBorders = new Rectangle(
borders.X + siteInterval * 2, borders.Y - siteInterval * 2,
borders.Right - siteInterval * 2, borders.Y + borders.Height - siteInterval * 2);
//pathBorders = new Rectangle(
//borders.X + siteInterval * 2, borders.Y - siteInterval * 2,
//borders.Right - siteInterval * 2, borders.Y + borders.Height - siteInterval * 2);
Vector2 start = pathCells[rand.Next(1, pathCells.Count - 2)].Center;
@@ -236,7 +252,6 @@ namespace Subsurface
private List<VoronoiCell> GeneratePath(Random rand, Vector2 start, Vector2 end, List<VoronoiCell> cells, Microsoft.Xna.Framework.Rectangle limits, float minWidth, float wanderAmount = 0.3f)
{
Stopwatch sw2 = new Stopwatch();
sw2.Start();
@@ -503,6 +518,13 @@ namespace Subsurface
bodyPoints[i] = ConvertUnits.ToSimUnits(bodyPoints[i]);
}
for (int i = bodyPoints.Count-1; i >0 ; i--)
{
if (Vector2.Distance(bodyPoints[i], bodyPoints[i - 1]) < 0.1f) bodyPoints.RemoveAt(i);
}
if (bodyPoints.Count < 2) continue;
Vertices bodyVertices = new Vertices(bodyPoints);
Body edgeBody = BodyFactory.CreateLoopShape(Game1.World, bodyVertices);
@@ -528,12 +550,28 @@ namespace Subsurface
public void SetPosition(Vector2 pos)
{
Vector2 amount = ConvertUnits.ToSimUnits(pos - Position);
Vector2 amount = pos - Position;
Vector2 simAmount = ConvertUnits.ToSimUnits(amount);
foreach (VoronoiCell cell in cells)
{
if (cell.body == null) continue;
cell.body.SleepingAllowed = false;
cell.body.SetTransform(cell.body.Position + amount, cell.body.Rotation);
cell.body.SetTransform(cell.body.Position + simAmount, cell.body.Rotation);
}
foreach (MapEntity mapEntity in MapEntity.mapEntityList)
{
Item item = mapEntity as Item;
if (item == null)
{
if (!mapEntity.MoveWithLevel) continue;
mapEntity.Move(amount);
}
else if (item.body != null)
{
if (item.CurrentHull != null) continue;
item.SetTransform(item.SimPosition+amount, item.body.Rotation);
}
}
}
@@ -564,25 +602,27 @@ namespace Subsurface
}
else
{
if (limb.type == LimbType.LeftFoot || limb.type == LimbType.RightFoot) continue;
limb.body.ApplyForce((simVelocity - prevVelocity) * 10.0f * limb.Mass);
//if (limb.type == LimbType.LeftFoot || limb.type == LimbType.RightFoot) continue;
//limb.body.ApplyForce((simVelocity - prevVelocity) * 10.0f * limb.Mass);
}
}
}
foreach (Item item in Item.itemList)
{
if (item.CurrentHull != null) continue;
if (item.body == null)
foreach (MapEntity mapEntity in MapEntity.mapEntityList)
{
Item item = mapEntity as Item;
if (item == null)
{
item.Move(velocity);
if (!mapEntity.MoveWithLevel) continue;
mapEntity.Move(velocity);
}
else
else if (item.body!=null)
{
if (item.CurrentHull != null) continue;
item.body.LinearVelocity += simVelocity;
}
}
prevVelocity = simVelocity;
}
+6
View File
@@ -36,6 +36,12 @@ namespace Subsurface
protected bool isHighlighted;
protected bool isSelected;
public bool MoveWithLevel
{
get;
set;
}
//the position and dimensions of the entity
protected Rectangle rect;
+30 -13
View File
@@ -148,6 +148,8 @@ namespace Subsurface
//this.mapHash = new MapHash(md5Hash);
}
base.Remove();
}
private List<Vector2> GenerateConvexHull()
@@ -410,7 +412,7 @@ namespace Subsurface
if (targetPosition != Vector2.Zero && Vector2.Distance(targetPosition, Position) > 5.0f)
{
translateAmount += (targetPosition - Position)*0.1f;
translateAmount += (targetPosition - Position) * 0.05f;
}
else
{
@@ -433,7 +435,7 @@ namespace Subsurface
//hullBodies[0].body.LinearVelocity = -hullBodies[0].body.Position;
//hullBody.SetTransform(Vector2.Zero , 0.0f);
hullBody.LinearVelocity = -hullBody.Position;
hullBody.LinearVelocity = -hullBody.Position/(float)Physics.step;
if (collidingCell == null)
{
@@ -515,13 +517,18 @@ namespace Subsurface
VoronoiCell cell = f2.Body.UserData as VoronoiCell;
if (cell==null) return true;
Vector2 normal = contact.Manifold.LocalNormal;
float impact = Vector2.Dot(ConvertUnits.ToSimUnits(speed), normal);
Vector2 normal = -contact.Manifold.LocalNormal;
Vector2 simSpeed = ConvertUnits.ToSimUnits(speed);
float impact = -Vector2.Dot(simSpeed, normal);
Vector2 u = Vector2.Dot(simSpeed, normal)*normal;
Vector2 w = simSpeed - u;
speed = ConvertUnits.ToDisplayUnits(w*f2.Body.Friction - u*0.5f);
System.Diagnostics.Debug.WriteLine("IMPACT:"+impact);
if (impact < 5.0f) return true;
collisionRigidness = 0.8f;
collidingCell = cell;
@@ -540,6 +547,9 @@ namespace Subsurface
message.Write(Position.X);
message.Write(Position.Y);
message.Write(speed.X);
message.Write(speed.Y);
}
public override void ReadNetworkData(Networking.NetworkEventType type, NetIncomingMessage message)
@@ -548,15 +558,16 @@ namespace Subsurface
if (sendingTime <= lastNetworkUpdate) return;
Vector2 newPosition = new Vector2(message.ReadFloat(), message.ReadFloat());
if (newPosition == Position) return;
if ((newPosition - Position).Length() > 500.0f)
{
System.Diagnostics.Debug.WriteLine("Submarine has moved over 500 pixels since last update");
return;
}
//Vector2 newPosition =
//if (newPosition == Position) return;
//if ((newPosition - Position).Length() > 500.0f)
//{
// System.Diagnostics.Debug.WriteLine("Submarine has moved over 500 pixels since last update");
// return;
//}
targetPosition = Position;
targetPosition = new Vector2(message.ReadFloat(), message.ReadFloat());
speed = new Vector2(message.ReadFloat(), message.ReadFloat());
lastNetworkUpdate = sendingTime;
}
@@ -809,6 +820,8 @@ namespace Subsurface
}
}
ID = 1;
loaded = this;
}
@@ -825,6 +838,10 @@ namespace Subsurface
public static void Unload()
{
if (loaded == null) return;
loaded.Remove();
loaded.Clear();
loaded = null;
}
+9 -2
View File
@@ -10,12 +10,19 @@ using System.Collections.ObjectModel;
namespace Subsurface
{
public enum SpawnType { None, Human, Enemy };
class WayPoint : MapEntity
{
public enum SpawnType { None, Human, Enemy };
private SpawnType spawnType;
public SpawnType SpawnType
{
get { return spawnType; }
set { spawnType = value; }
}
public override Vector2 SimPosition
{
get { return ConvertUnits.ToSimUnits(new Vector2(rect.X, rect.Y)); }
@@ -31,7 +38,7 @@ namespace Subsurface
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
if (!editing) return;
//if (!editing) return;
Color clr = (isSelected) ? Color.Red : Color.LightGreen;
GUI.DrawRectangle(spriteBatch, new Rectangle(rect.X, -rect.Y, rect.Width, rect.Height), clr, true);