Very unfinished attempt at changing the coordinate system to work relative to the level and to move the sub instead of the whole level

This commit is contained in:
Regalis
2015-12-03 19:42:11 +02:00
parent 5bcdfa2b56
commit c064c5eb50
14 changed files with 278 additions and 269 deletions

View File

@@ -77,15 +77,7 @@ namespace Barotrauma
get;
private set;
}
public Vector2 Position
{
get
{
return cells==null ? Vector2.Zero : ConvertUnits.ToDisplayUnits(cells[0].body.Position);
}
}
public List<Vector2> PositionsOfInterest
{
get { return positionsOfInterest; }
@@ -712,106 +704,106 @@ namespace Barotrauma
return verticeList.ToArray();
}
public void SetPosition(Vector2 pos)
{
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 + simAmount, cell.body.Rotation);
//}
//public void SetPosition(Vector2 pos)
//{
// 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 + simAmount, cell.body.Rotation);
// //}
foreach (Body body in bodies)
{
body.SetTransform(body.Position + simAmount, body.Rotation);
}
// foreach (Body body in bodies)
// {
// body.SetTransform(body.Position + simAmount, 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);
}
}
// 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);
// }
// }
//WrappingWall.UpdateWallShift(Position, wrappingWalls);
}
// //WrappingWall.UpdateWallShift(Position, wrappingWalls);
//}
Vector2 prevVelocity;
public void Move(Vector2 amount)
{
Vector2 simVelocity = ConvertUnits.ToSimUnits(amount / (float)Physics.step);
//Vector2 prevVelocity;
//public void Move(Vector2 amount)
//{
// Vector2 simVelocity = ConvertUnits.ToSimUnits(amount / (float)Physics.step);
foreach (Body body in bodies)
{
body.LinearVelocity = simVelocity;
}
// foreach (Body body in bodies)
// {
// body.LinearVelocity = simVelocity;
// }
foreach (Character character in Character.CharacterList)
{
foreach (Limb limb in character.AnimController.Limbs)
{
if (character.AnimController.CurrentHull != null) continue;
// foreach (Character character in Character.CharacterList)
// {
// foreach (Limb limb in character.AnimController.Limbs)
// {
// if (character.AnimController.CurrentHull != null) continue;
limb.body.LinearVelocity += simVelocity;
}
}
// limb.body.LinearVelocity += simVelocity;
// }
// }
foreach (Item item in Item.ItemList)
{
if (item.body==null || item.CurrentHull != null) continue;
item.body.LinearVelocity += simVelocity;
}
// foreach (Item item in Item.ItemList)
// {
// if (item.body==null || item.CurrentHull != null) continue;
// item.body.LinearVelocity += simVelocity;
// }
AtStartPosition = Vector2.Distance(startPosition, -Position) < ExitDistance;
AtEndPosition = Vector2.Distance(endPosition, -Position) < ExitDistance;
// AtStartPosition = Vector2.Distance(startPosition, -Position) < ExitDistance;
// AtEndPosition = Vector2.Distance(endPosition, -Position) < ExitDistance;
prevVelocity = simVelocity;
// prevVelocity = simVelocity;
WrappingWall.UpdateWallShift(-Position, wrappingWalls);
}
// WrappingWall.UpdateWallShift(-Position, wrappingWalls);
//}
public static void AfterWorldStep()
{
if (loaded == null) return;
//public static void AfterWorldStep()
//{
// if (loaded == null) return;
loaded.ResetBodyVelocities();
}
// loaded.ResetBodyVelocities();
//}
private void ResetBodyVelocities()
{
if (prevVelocity == Vector2.Zero) return;
if (!MathUtils.IsValid(prevVelocity))
{
prevVelocity = Vector2.Zero;
return;
}
//private void ResetBodyVelocities()
//{
// if (prevVelocity == Vector2.Zero) return;
// if (!MathUtils.IsValid(prevVelocity))
// {
// prevVelocity = Vector2.Zero;
// return;
// }
foreach (Character character in Character.CharacterList)
{
if (character.AnimController.CurrentHull != null) continue;
// foreach (Character character in Character.CharacterList)
// {
// if (character.AnimController.CurrentHull != null) continue;
foreach (Limb limb in character.AnimController.Limbs)
{
limb.body.LinearVelocity -= prevVelocity;
}
}
// foreach (Limb limb in character.AnimController.Limbs)
// {
// limb.body.LinearVelocity -= prevVelocity;
// }
// }
foreach (Item item in Item.ItemList)
{
if (item.body == null || item.CurrentHull != null) continue;
item.body.LinearVelocity -= prevVelocity;
}
}
// foreach (Item item in Item.ItemList)
// {
// if (item.body == null || item.CurrentHull != null) continue;
// item.body.LinearVelocity -= prevVelocity;
// }
//}
public void Draw(SpriteBatch spriteBatch)
{
@@ -841,7 +833,7 @@ namespace Barotrauma
System.Diagnostics.Debug.WriteLine("avgpos: " + avgPos / cells.Count);
System.Diagnostics.Debug.WriteLine("pos: " + Position);
//System.Diagnostics.Debug.WriteLine("pos: " + Position);
}
public List<VoronoiCell> GetCells(Vector2 pos, int searchDepth = 2)
@@ -911,10 +903,10 @@ namespace Barotrauma
{
if (onlySolid && !cell.edges[i].isSolid) continue;
Vector2 start = cell.edges[i].point1 + Position;
Vector2 start = cell.edges[i].point1;
start.Y = -start.Y;
Vector2 end = cell.edges[i].point2 + Position;
Vector2 end = cell.edges[i].point2;
end.Y = -end.Y;
edges.Add(new Vector2[] { start, end });
@@ -932,7 +924,7 @@ namespace Barotrauma
foreach (VoronoiCell cell in wrappingWalls[side, n].Cells)
{
Vector2 offset = wrappingWalls[side, n].Offset + Position;
Vector2 offset = wrappingWalls[side, n].Offset;
for (int i = 0; i < cell.edges.Count; i++)
{
if (onlySolid && !cell.edges[i].isSolid) continue;

View File

@@ -35,8 +35,8 @@ namespace Barotrauma
public void Draw(SpriteBatch spriteBatch)
{
Vector2 pos = level.EndPosition;
pos.Y = -pos.Y - level.Position.Y;
Vector2 pos = Vector2.Zero;// level.EndPosition;
//pos.Y = -pos.Y - level.Position.Y;
if (GameMain.GameScreen.Cam.WorldView.Y < -pos.Y - 512) return;
@@ -46,7 +46,7 @@ namespace Barotrauma
int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width / 512.0f + 2.0f) * 512.0f);
spriteBatch.Draw(shaftTexture,
new Rectangle((int)(MathUtils.Round(pos.X, 512.0f) + level.Position.X % 512), (int)pos.Y, width, 512),
new Rectangle((int)(MathUtils.Round(pos.X, 512.0f) - Submarine.Loaded.Position.X % 512), (int)pos.Y, width, 512),
new Rectangle(0, 0, width, 256),
Color.White, 0.0f,
Vector2.Zero,
@@ -59,7 +59,7 @@ namespace Barotrauma
if (vertices == null) return;
if (vertices.Length <= 0) return;
basicEffect.World = Matrix.CreateTranslation(new Vector3(level.Position, 0.0f)) * cam.ShaderTransform
basicEffect.World = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
basicEffect.CurrentTechnique.Passes[0].Apply();
@@ -74,7 +74,7 @@ namespace Barotrauma
for (int i = 0; i < 2; i++)
{
basicEffect.World = Matrix.CreateTranslation(
new Vector3(level.Position + level.WrappingWalls[side, i].Offset, 0.0f)) *
new Vector3(-Submarine.Loaded.Position + level.WrappingWalls[side, i].Offset, 0.0f)) *
cam.ShaderTransform *
Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;

View File

@@ -69,7 +69,7 @@ namespace Barotrauma.Lights
foreach (ConvexHull convexHull in ConvexHull.list)
{
if (!camView.Intersects(convexHull.BoundingBox)) continue;
//if (!camView.Intersects(convexHull.BoundingBox)) continue;
convexHull.DrawShadows(graphics, cam, pos, shadowTransform);
}
@@ -109,7 +109,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights)
{
if (light.hullsInRange.Count == 0 || light.Color.A < 0.01f || light.Range < 1.0f) continue;
if (!MathUtils.CircleIntersectsRectangle(light.Position, light.Range, viewRect)) continue;
//if (!MathUtils.CircleIntersectsRectangle(light.Position, light.Range, viewRect)) continue;
//clear alpha to 1
ClearAlphaToOne(graphics, spriteBatch);
@@ -128,7 +128,7 @@ namespace Barotrauma.Lights
//draw the light shape
//where Alpha is 0, nothing will be written
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.MultiplyWithAlpha, null, null, null, null, cam.Transform);
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.MultiplyWithAlpha, null, null, null, null, Matrix.CreateTranslation(new Vector3(Submarine.Loaded.Position.X, -Submarine.Loaded.Position.Y, 0.0f)) * cam.Transform);
light.Draw(spriteBatch);
spriteBatch.End();
}

View File

@@ -23,7 +23,6 @@ namespace Barotrauma
class Submarine : Entity
{
public static string SavePath = "Data" + System.IO.Path.DirectorySeparatorChar + "SavedSubs";
public static List<Submarine> SavedSubmarines = new List<Submarine>();
@@ -99,7 +98,7 @@ namespace Barotrauma
public Vector2 Position
{
get { return (Level.Loaded == null) ? Vector2.Zero : -Level.Loaded.Position; }
get { return subBody.Position; }
}
public Vector2 Speed
@@ -377,7 +376,9 @@ namespace Barotrauma
public void SetPosition(Vector2 position)
{
if (!MathUtils.IsValid(position)) return;
Level.Loaded.SetPosition(-position);
subBody.SetPosition(position);
//Level.Loaded.SetPosition(-position);
//prevPosition = position;
}
@@ -385,7 +386,9 @@ namespace Barotrauma
{
if (amount == Vector2.Zero || !MathUtils.IsValid(amount)) return;
Level.Loaded.Move(-amount);
subBody.SetPosition(subBody.Position + amount);
//Level.Loaded.Move(-amount);
}
public override bool FillNetworkData(Networking.NetworkEventType type, NetBuffer message, object data)

View File

@@ -71,6 +71,10 @@ namespace Barotrauma
}
}
public Vector2 Position
{
get { return ConvertUnits.ToDisplayUnits(body.Position); }
}
public Vector2 Center
{
@@ -79,7 +83,7 @@ namespace Barotrauma
public bool AtDamageDepth
{
get { return sub.Position.Y < DamageDepth; }
get { return Position.Y < DamageDepth; }
}
public SubmarineBody(Submarine sub)
@@ -187,34 +191,35 @@ namespace Barotrauma
public void Update(float deltaTime)
{
if (body.Position!=Vector2.Zero)
if (Position!=Vector2.Zero)
{
UpdateColliding();
//!!!!!!!!!!!!!!!!
//UpdateColliding();
}
Vector2 translateAmount = speed * deltaTime;
translateAmount += ConvertUnits.ToDisplayUnits(body.Position) * collisionRigidness;
//Vector2 translateAmount = speed * deltaTime;
//translateAmount += ConvertUnits.ToDisplayUnits(Position) * collisionRigidness;
if (targetPosition != Vector2.Zero && targetPosition != sub.Position)
{
float dist = Vector2.Distance(targetPosition, sub.Position);
//if (targetPosition != Vector2.Zero && targetPosition != Position)
//{
// float dist = Vector2.Distance(targetPosition, Position);
if (dist>1000.0f)
{
sub.SetPosition(targetPosition);
targetPosition = Vector2.Zero;
}
else if (dist>50.0f)
{
translateAmount += (targetPosition - sub.Position) * 0.01f;
}
}
else
{
targetPosition = Vector2.Zero;
}
// if (dist>1000.0f)
// {
// sub.SetPosition(targetPosition);
// targetPosition = Vector2.Zero;
// }
// else if (dist>50.0f)
// {
// translateAmount += (targetPosition - Position) * 0.01f;
// }
//}
//else
//{
// targetPosition = Vector2.Zero;
//}
sub.Translate(translateAmount);
//sub.Translate(translateAmount);
//-------------------------
@@ -232,14 +237,13 @@ namespace Barotrauma
ApplyForce(totalForce);
UpdateDepthDamage(deltaTime);
//UpdateDepthDamage(deltaTime);
//hullBodies[0].body.LinearVelocity = -hullBodies[0].body.Position;
//hullBody.SetTransform(Vector2.Zero , 0.0f);
body.SetTransform(Vector2.Zero, 0.0f);// .LinearVelocity = -body.Position / (float)Physics.step;
body.LinearVelocity = Vector2.Zero;
//body.SetTransform(Vector2.Zero, 0.0f);// .LinearVelocity = -body.Position / (float)Physics.step;
//body.LinearVelocity = Vector2.Zero;
}
private Vector2 CalculateBuoyancy()
@@ -264,14 +268,19 @@ namespace Barotrauma
public void ApplyForce(Vector2 force)
{
Speed += force / mass;
body.ApplyForce(force/100.0f);
}
public void SetPosition(Vector2 position)
{
body.SetTransform(ConvertUnits.ToSimUnits(position), 0.0f);
}
private void UpdateDepthDamage(float deltaTime)
{
if (sub.Position.Y > DamageDepth) return;
if (Position.Y > DamageDepth) return;
float depth = DamageDepth - sub.Position.Y;
float depth = DamageDepth - Position.Y;
depth = Math.Min(depth, 40000.0f);
// float prevTimer = depthDamageTimer;
@@ -311,6 +320,9 @@ namespace Barotrauma
private void UpdateColliding()
{
return;
if (body.Position.LengthSquared()<0.00001f) return;
Vector2 normal = Vector2.Normalize(body.Position);