public override (bool, Vector) CanMove(WorldScene worldScene) { var direction = GetDirection(); if (direction == null) { return(false, null); } var nextPosition = new OffsetVector(Position, direction); foreach (var entity in worldScene.GetEntities()) { if (entity == this || !(entity is CollisionEntity collisionEntity)) { continue; } if (nextPosition.X < entity.Position.X + entity.Width && nextPosition.X + this.Width > entity.Position.X && nextPosition.Y < entity.Position.Y + entity.Height && nextPosition.Y + this.Height > entity.Position.Y) { OnCollision(collisionEntity); collisionEntity.OnCollision(this); return(false, null); } } return(true, direction); }
/// <summary> /// Function chosing the next action to be performed /// </summary> /// <returns>Returns one Action</returns> public override CellAction ChooseNextAction() { CellAction cellAction; SurroundingView surroundings = this.Cell.Sense(); // Get offset to the closest enemy IList<ICell> allCells = surroundings.GetAllCells(); // Remove friendly cells allCells = GetEnemyCells(this.Cell.GetTeam(), allCells); // Get closest cell ICell closestCell = GetClosestCell(allCells); if (closestCell == null) cellAction = new CellAction(GetRandomAction()); else { // Get vector to this cell IOffsetVector offsetVector = new OffsetVector(this.Cell.Position, closestCell.Position); AvailableActions action; if (Math.Abs(offsetVector.X) <= 1 && Math.Abs(offsetVector.Y) <= 1) action = AvailableActions.ATTACK; else action = this.Cell.GetRelativeMovment(offsetVector); cellAction = new CellAction(action, offsetVector); } return cellAction; }
public void Assignment_X_ModifiesOffset_AndNotOriginal() { float originalX = 10; float originalY = 20; float offsetX = 30; float offsetY = 40; float newOffsetX = 50; float expected = originalX + newOffsetX; var original = Vector.Create(originalX, originalY); var offset = Vector.Create(offsetX, offsetY); var source = new OffsetVector(original, offset); source.X = newOffsetX; Assert.AreEqual(source.Offset.X, newOffsetX); Assert.AreEqual(source.X, expected); Assert.AreEqual(source.Base.X, originalX); }
public void OffsetVector_NestedOffset() { float originalX = 10; float originalY = 20; float offsetX = 30; float offsetY = 40; float expectedX = originalX + originalX + offsetX; float expectedY = originalY + originalY + offsetY; var original = Vector.Create(originalX, originalY); var offset = Vector.Create(offsetX, offsetY); OffsetVector nestedOffset = new OffsetVector(original, offset); OffsetVector source = new OffsetVector(original, nestedOffset); Assert.AreEqual(source.X, expectedX); Assert.AreEqual(source.Y, expectedY); }
public void Assignment_Y_ModifiesScale_AndNotOriginal() { float originalX = 10; float originalY = 20; float offsetX = 30; float offsetY = 40; float newOffsetY = 50; float expected = originalY + newOffsetY; var original = Vector.Create(originalX, originalY); var offset = Vector.Create(offsetX, offsetY); var source = new OffsetVector(original, offset); source.Y = newOffsetY; Assert.AreEqual(source.Offset.Y, newOffsetY); Assert.AreEqual(source.Y, expected); Assert.AreEqual(source.Base.Y, originalY); }
public void Constructor_SharedVector_SharedVector() { float originalX = 10; float originalY = 20; float offsetX = 30; float offsetY = 40; float expectedX = originalX + offsetX; float expectedY = originalY + offsetY; var original = Vector.Create(originalX, originalY); var offset = Vector.Create(offsetX, offsetY); var source = new OffsetVector(original, offset); Assert.AreEqual(source.Offset.X, offset.X); Assert.AreEqual(source.Offset.Y, offset.Y); Assert.AreEqual(source.Base.X, originalX); Assert.AreEqual(source.Base.Y, originalY); Assert.AreEqual(source.X, expectedX); Assert.AreEqual(source.Y, expectedY); }
/// <summary> /// Creates a new OffsetVector based on the specified input text. /// </summary> /// <param name="input">The input text to get a match for. For example, "a + (1, 2, -3.222)".</param> /// <returns>Returns the new OffsetVector, or null if a no matches were found for the specified input.</returns> public static OffsetVector Create(string input) { const string pattern = @"^(?<vectorName>\w+)\s*\+\s*\(?(?<offsetX>[+-]?((\d+(\.\d+)?)))[, ]\s*(?<offsetY>[+-]?((\d+(\.\d+)?)))[, ]\s*(?<offsetZ>[+-]?((\d+(\.\d+)?)))\s*\)?\s*$"; Regex regex = new Regex(pattern); MatchCollection matches = regex.Matches(input); if (matches.Count == 0) { return(null); } OffsetVector offsetVector = new OffsetVector(); offsetVector.vectorName = RegexHelper.GetValue <string>(matches, "vectorName"); offsetVector.offsetX = RegexHelper.GetValue <double>(matches, "offsetX"); offsetVector.offsetY = RegexHelper.GetValue <double>(matches, "offsetY"); offsetVector.offsetZ = RegexHelper.GetValue <double>(matches, "offsetZ"); return(offsetVector); }
public void CameraFocus(Player player) { var v = player.Position; float count = 1; for (int i = 0; i < this.players.Length; i++) { if (i == player._joystickID) { continue; } if (this.players[i] == null) { continue; } v = new OffsetVector(v, this.players[i].Position); count++; } v = new ScalingVector(v, 1 / count, 1 / count); GameWindow.Singleton.Canvas.GetCamera().Follow(v); }
private void ExecuteCommandLine(string text) { SuperVector superVector = SuperVector.Create(text); if (superVector != null) { CreateOrUpdateSuperVector(superVector); return; } SetColor setColor = SetColor.Create(text); if (setColor != null) { ChangeVectorColor(setColor.vectorName, setColor.hexCode); return; } SetNamedColor setNamedColor = SetNamedColor.Create(text); if (setNamedColor != null) { string hexCode; if (colorLookups.ContainsKey(setNamedColor.colorName)) { hexCode = colorLookups[setNamedColor.colorName]; ChangeVectorColor(setNamedColor.vectorName, hexCode); } else { Debug.LogError($"\"{setNamedColor.colorName}\" needs to be defined first!!!"); } return; } DefineColor defineColor = DefineColor.Create(text); if (defineColor != null) { DefineColorName(defineColor.colorName, defineColor.hexCode); return; } MoveVector moveVector = MoveVector.Create(text); if (moveVector != null) { MoveVectorTo(moveVector.vector1, moveVector.vector2); return; } SumVectors sumVectors = SumVectors.Create(text); if (sumVectors != null) { SumAllVectors(sumVectors.newVectorName, sumVectors.vector1, sumVectors.vector2, sumVectors.vector3, sumVectors.vector4, sumVectors.vector5); return; } NegativeVector negativeVector = NegativeVector.Create(text); if (negativeVector != null) { MakeVectorNegative(negativeVector.vectorName); return; } AssignNegativeVector assignNegativeVector = AssignNegativeVector.Create(text); if (assignNegativeVector != null) { CreateNegativeVector(assignNegativeVector.existingVectorName, assignNegativeVector.newVectorName); return; } VectorCommand vectorCommand = VectorCommand.Create(text); if (vectorCommand != null) { HandleVectorCommand(vectorCommand.command, vectorCommand.vectorName); return; } OffsetVector offsetVector = OffsetVector.Create(text); if (offsetVector != null) { OffsetVectorBy(offsetVector.vectorName, offsetVector.offsetX, offsetVector.offsetY, offsetVector.offsetZ); return; } }