/* * public Vector3 SpaceToWorld(int X, int Y) { * Vector3 vec = new Vector3(X*cellSize + cellSize/2, 0, Y*cellSize + cellSize/2); * return vec; * } * * public Point2D WorldToSpace(Vector2 pos3D) { * int posX, posY; * posX = (int)(pos3D.X / cellSize); * posY = (int)(pos3D.Y / cellSize); * return new Point2D(posX, posY); * } * * public Point2D WorldToSpace(Vector3 pos3D) { * int posX, posY; * posX = (int)(pos3D.X / cellSize) ; * posY = (int)(pos3D.Z / cellSize) ; * return new Point2D(posX, posY); * } * * public int WorldToSpaceId(Vector3 pos3D) { * int posX, posY; * posX = (int)(pos3D.X / cellSize); * posY = (int)(pos3D.Z / cellSize); * int id = posX + posY * width; * if (id < 0 || id >= buildingSpace.Length) return 0; * return id; * } * * public Vector2 GetNearestPos(Vector3 pos3D) { * int posX, posY; * posX = (int)( pos3D.X / cellSize) ; * posY = (int)( pos3D.Z / cellSize) ; * return new Vector2(posX, posY); * } * * public Vector3 GetNearestRealPos(Vector3 pos3D) { * int posX, posY; * posX = (int)(pos3D.X / cellSize); * posY = (int)(pos3D.Z / cellSize); * if (posX < 0) posX = 0; * if (posY < 0) posY = 0; * if (posX >= width) posX = width-1; * if (posY >= height) posY = height-1; * return new Vector3(cellSize/2+ posX*cellSize,0, cellSize/2+ posY*cellSize); * }*/ public bool TestWithDiagonals(fint pX, fint pY) { int x = (pX / (fint)2).ToInt(); int y = (pY / (fint)2).ToInt(); int id = x + y * Width; if (x < 0 || y < 0 || x >= Width || y >= Height) { return(false); } if (staticSpace[id] == 0) { return(false); } fint modx = pX % fint.one; fint mody = pY % fint.one; byte bitmask = pathfinding[id]; if ((bitmask & MapSpace.DirectionDiagonalBlock[0]) == 0) { if (mody < modx) { return(false); } } else if ((bitmask & MapSpace.DirectionDiagonalBlock[1]) == 0) { if (mody > modx) { return(false); } } if ((bitmask & MapSpace.DirectionDiagonalBlock[2]) == 0) { if (mody > ((fint)1 - modx)) { return(false); } } else if ((bitmask & MapSpace.DirectionDiagonalBlock[3]) == 0) { if (mody < ((fint)1 - modx)) { return(false); } } return(true); }
public void Update() { parent.Velocity.X = fint.zero; parent.Velocity.Y = fint.zero; radiusNear = false; radiusTouch = false; //------------------------------------------------------------------------------------ unitsNearBy.Clear(); parent.proximityToken.FindNeighbors(new Point3D(parent.Position.X.ToInt(), 0, parent.Position.Y.ToInt()), NeighborCheckRadius, unitsNearBy); DisperseUpdate(); var dist = (parent.Position - dest).sqrMagnitude; int reachDist = 1; if (parent.Orders.Count != 0 && parent.Orders.Peek().type == OrderType.MOVE) { reachDist = 10; } if (active) { fint xxx = fint.zero, yyy = fint.zero; //------------------------------------------------------------------------------------ FVector2 cur2D = BackgroundGame.MapSpace.WorldToSpace(parent.Position); int id = cur2D.X.ToInt() + cur2D.Y.ToInt() * BackgroundGame.MapSpace.Width; byte direction = flowfield == null ? (byte)255 : ((id >= 0 && id < flowfield.Length) ? flowfield.Get(id) : (byte)0); //if (direction == 255 || dist<(fint)((reachDist+1) * (reachDist+1))) { parent.Rotation = FVector2.Angle(dest, parent.Position); /*} else { * if (direction != 0 && direction != 1) { * if (direction >= 100) direction -= 100; * * int x = -MapSpace.Direction[direction - 2, 0]; * int y = -MapSpace.Direction[direction - 2, 1]; * parent.Rotation = FMath.Atan2((fint)y, (fint)x); * } * }*/ xxx = FMath.Cos(parent.Rotation) * (fint)Speed; yyy = FMath.Sin(parent.Rotation) * (fint)Speed; //------------------------------------------------------------------------------------ if (!BackgroundGame.MapSpace.TestWithDiagonals(parent.Position.X + parent.Velocity.X + xxx, parent.Position.Y + parent.Velocity.Y)) { xxx = fint.zero; } if (!BackgroundGame.MapSpace.TestWithDiagonals(parent.Position.X + parent.Velocity.X, parent.Position.Y + parent.Velocity.Y + yyy)) { yyy = fint.zero; } if (!BackgroundGame.MapSpace.TestWithDiagonals(parent.Position.X + parent.Velocity.X + xxx, parent.Position.Y + parent.Velocity.Y + yyy)) { xxx = fint.zero; yyy = fint.zero; } parent.Velocity.X += xxx; parent.Velocity.Y += yyy; } parent.Velocity.X = GameUtils.Clamp(parent.Velocity.X, -Speed, Speed); parent.Velocity.Y = GameUtils.Clamp(parent.Velocity.Y, -Speed, Speed); parent.Position += parent.Velocity; //if (!active) if (active) { dist = (parent.Position - dest).sqrMagnitude; if (dist <= (fint)(reachDist * reachDist)) { //parent.Position = dest; active = false; reached = true; parent.Moving = false; } } }
public void DisperseUpdate() { FVector2 offset = FVector2.Zero; int pushCount = 0; radiusNear = unitsNearBy.Count > 0; foreach (InteractiveObject unit in unitsNearBy) { if (unit.id == parent.id || unit.Moving != parent.Moving) { continue; } fint off_avoidanceX = (parent.Position.X - unit.Position.X); fint off_avoidanceY = (parent.Position.Y - unit.Position.Y); fint distance = (off_avoidanceX * off_avoidanceX) + (off_avoidanceY * off_avoidanceY); if (distance != fint.zero && distance >= (fint)(Radius * Radius)) { continue; } //fint dist = FMath.Sqrt(distance); FVector2 vec = new FVector2(off_avoidanceX, off_avoidanceY); vec.Normalize(); /*if (distance==0) { * off_avoidanceX = 1; * off_avoidanceY = 1; * }*/ //distance=Radius*Radius; //double dist = Math.Sqrt(distance)/Point2D.DIVIDER; //double dist= ((Radius - Math.Sqrt(distance))/Radius); offset.X += vec.X; offset.Y += vec.Y; pushCount++; } if (pushCount != 0) { radiusTouch = true; fint xxx = offset.X / (fint)pushCount; fint yyy = offset.Y / (fint)pushCount; //xxx = GameUtils.Clamp(xxx, -Radius, Radius); //yyy = GameUtils.Clamp(yyy, -Radius, Radius); if (!BackgroundGame.MapSpace.TestWithDiagonals(parent.Position.X + parent.Velocity.X + xxx, parent.Position.Y + parent.Velocity.Y)) { xxx = fint.zero; } if (!BackgroundGame.MapSpace.TestWithDiagonals(parent.Position.X + parent.Velocity.X, parent.Position.Y + parent.Velocity.Y + yyy)) { yyy = fint.zero; } if (!BackgroundGame.MapSpace.TestWithDiagonals(parent.Position.X + parent.Velocity.X + xxx, parent.Position.Y + parent.Velocity.Y + yyy)) { xxx = fint.zero; yyy = fint.zero; } parent.Velocity.X += xxx; parent.Velocity.Y += yyy; } }
public static fint Clamp(fint value, int min, int max) { value = (value > (fint)max) ? (fint)max : value; value = (value < (fint)min) ? (fint)min : value; return(value); }
public static fint Clamp(fint value, fint min, fint max) { value = (value > max) ? max : value; value = (value < min) ? min : value; return(value); }