public void HasSamePositionAt_NOT_Touched_Diagonal_Test() { VModel model = new VModel(); Point size = new Point(); size.X = 100; size.Y = 50; model.Map = new Map2d(size); // Insert a Wall exactly in the middle (vertical) model.Map.InsertWall(new Wall() { Start = new Point(50, 0), End = new Point(50, 50) }); // Presume that the first id's are reserved for players ModelPlayer player = new ModelPlayer(model, 0, 10); // Create Coins ModelObject target = new ModelObject(model, 10, 2); // Add objects to model model.AddModelObject(target, new Point(10, 10)); model.AddModelObject(player, new Point(20, 20)); // tight but NOT 'on' the coin (=> which through (int) is ON the coin!) bool result = target.hasSamePosition(player); Assert.AreEqual(false, result); }
public bool hasSamePositionAt(ModelObject obj, Point position) { double distance = Math.Sqrt(Math.Pow(position.X - obj.Position.X, 2) + Math.Pow(position.Y - obj.Position.Y, 2)); // old: //if (distance <= Size || distance <= obj.Size) if (distance <= (Size + obj.Size)) { return true; } return false; }
public void AddModelObject(ModelObject modelObj, Point position) { if (modelObjWithId.ContainsKey(modelObj.ID)) { throw new ArgumentException("Id already exists!"); } modelObjWithId[modelObj.ID] = modelObj; NextFreeId = modelObj.ID + 1; // TODO: if (!modelObjsOfType.ContainsKey(modelObj.GetType())) { modelObjsOfType[modelObj.GetType()] = new List<ModelObject>(); } modelObjsOfType[modelObj.GetType()].Add(modelObj); if (!modelObjsInPoint.ContainsKey(position)) { modelObjsInPoint[position] = new List<ModelObject>(); } modelObjsInPoint[position].Add(modelObj); pointOfModelObj[modelObj] = position; if (modelObj.Size > maxSize) { maxSize = modelObj.Size; } }
public bool hasSamePosition(ModelObject obj) { return hasSamePositionAt(obj, Position); }
public void HasSamePositionAt_Valid_Touched_Test() { VModel model = new VModel(); Point size = new Point(); size.X = 100; size.Y = 50; model.Map = new Map2d(size); // Insert a Wall exactly in the middle (vertical) model.Map.InsertWall(new Wall() { Start = new Point(50, 0), End = new Point(50, 50) }); // Presume that the first id's are reserved for players ModelPlayer player = new ModelPlayer(model, 0, 10); // Create Coins ModelObject target = new ModelObject(model, 10, 2); // Add objects to model model.AddModelObject(target, new Point(10, 10)); model.AddModelObject(player, new Point(10, 22)); // touches the coin bool result = target.hasSamePosition(player); Assert.AreEqual(true, result); }
public Point GetPositionOfModelObject(ModelObject obj) { if (!pointOfModelObj.ContainsKey(obj)) { Point p = new Point(); p.X = -1; p.Y = -1; return p; } return pointOfModelObj[obj]; }
public void MoveModelObject(ModelObject obj, Point toPosition) { if (obj != null) { pointOfModelObj[obj] = toPosition; } //TODO: move this to VMService ////TODO: test this //if (obj != null) //{ // Point endPosition = GameLogic.MoveModelObject(obj, toPosition); // pointOfModelObj[obj] = endPosition; //} }
private Point ProcessWay(ModelObject obj, Point toPosition, bool checkForWalls, bool interact) { // walk through every point from actual position to toPosition // determine linear equation getValueOfAxis getMainMovingAxis = getValueOfX; isDirectionPositive determineDirection; calcNextPoint nextPoint; CalcPointParams nextPointParams; if (toPosition.X == obj.Position.X) { // movement is parallel to vertical axis y // set determine direction delegate determineDirection = isParallelToYDirectionPositive; // set calcNextPoint delegate nextPoint = calcParallelToY; // set getMainMovingAxis to getValueOfY getMainMovingAxis = getValueOfY; //set parameters CalcParallelToAxisParams parameters = new CalcParallelToAxisParams(); parameters.ConstValue = toPosition.X; nextPointParams = parameters; } else if (toPosition.Y == obj.Position.Y) { // movement is parallel to horizontal axis x // set determine direction delegate determineDirection = isKXDDirectionPositive; // set calcNextPoint delegate nextPoint = calcParallelToX; //set parameters CalcParallelToAxisParams parameters = new CalcParallelToAxisParams(); parameters.ConstValue = toPosition.Y; nextPointParams = parameters; } else { double k = (toPosition.X - obj.Position.X) / (toPosition.Y - obj.Position.Y); double d = obj.Position.Y - (k * obj.Position.X); // set determine direction delegate determineDirection = isKXDDirectionPositive; // set calcNextPoint delegate nextPoint = calcNextKXDPoint; //set parameters CalcKXDPointParams parameters = new CalcKXDPointParams(); parameters.d = d; parameters.k = k; nextPointParams = parameters; } // determine direction dir int dir = 0; if (determineDirection(obj.Position, toPosition)) { dir = 1; } else { dir = -1; } // compute every position between actual position and toPosition Point actualPosition = obj.Position; try { // x = obj.Position.X is not correct!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! bool direction = determineDirection(actualPosition, toPosition); for (int x = getMainMovingAxis(obj.Position) + dir; (dir == 1 && direction) || (dir == -1 && !direction); x += dir) { Point position = nextPoint(x, nextPointParams); if (computePosition(obj, position, checkForWalls, interact)) { actualPosition = position; } else { return actualPosition; } direction = determineDirection(actualPosition, toPosition); } } catch (Exception e) { Log.ErrorFormat(e.Message); } return actualPosition; }
public ICollection<ModelObject> GetCloseModelObjects(ModelObject obj) { // compute a square around the obj. with 'size-of-biggest-object' + 'own-size' int minX = Math.Max(obj.Position.X - obj.Size - maxSize, 0); int maxX = Math.Min(obj.Position.X + obj.Size + maxSize, Map.GetSize().X); int minY = Math.Max(obj.Position.Y - obj.Size - maxSize, 0); int maxY = Math.Min(obj.Position.Y + obj.Size + maxSize, Map.GetSize().Y); List<ModelObject> returnVals = new List<ModelObject>(); for (int i = minX; i <= maxX; i++) { for (int j = minY; j <= maxY; j++) { Point p = new Point(); p.X = i; p.Y = j; if (modelObjsInPoint.ContainsKey(p)) { // add all that are not the same object returnVals.AddRange(modelObjsInPoint[p].Where(x => !x.Equals(obj))); } } } return returnVals; }
internal ICollection<ModelObject> playerMeetsCoinAction(ModelObject obj1, ModelObject obj2) { ModelPlayer player = null; ModelCoin coin = null; if (obj1 is ModelPlayer) player = obj1 as ModelPlayer; else if (obj1 is ModelCoin) coin = obj1 as ModelCoin; if (obj2 is ModelPlayer) player = obj2 as ModelPlayer; else if (obj2 is ModelCoin) coin = obj2 as ModelCoin; List<ModelObject> returnVal = new List<ModelObject>(); // Why does this method return something? // This seems for me to be an errro if (player == null || coin == null) { returnVal.Add(obj1); returnVal.Add(obj2); } else { // count one up in the statistic of the player if (pointsOfPlayers.ContainsKey(player)) { pointsOfPlayers[player] += coin.Points; } // remove coin from model CoinRemoved(coin.Position, player.ID); player.Model.RemoveModelObject(coin.ID); returnVal.Add(player); } return returnVal; }
private ICollection<ModelObject> playersMeetAction(ModelObject obj1, ModelObject obj2) { List<ModelObject> returnVal = new List<ModelObject>(); returnVal.Add(obj1); returnVal.Add(obj2); return returnVal; }
internal bool computePosition(ModelObject obj, Point position, bool checkForWalls, bool interact) { if (checkForWalls && checkIfObjectIntersectsWallAt(obj, position)) { return false; } if (interact) { // TODO: Might it be, that closeObjs returns also the same object itself? ICollection<ModelObject> closeObjs = obj.Model.GetCloseModelObjects(obj); foreach (ModelObject o in closeObjs) { // Check if they somehow intersect - but shouldn't both calls be the same? if (o.hasSamePositionAt(obj, position) || obj.hasSamePositionAt(o, position)) { Tuple<Type, Type> got = new Tuple<Type, Type>(obj.GetType(), o.GetType()); if (actionsOnObjectsMeet.ContainsKey(got)) { actionsOnObjectsMeet[got](obj, o); } } } } return true; }
internal bool checkIfObjectIntersectsWallAt(ModelObject obj, Point atPosition) { foreach (Wall w in obj.Model.Map.GetWalls()) { // If is a non-vertical line if (w.Start.X != w.End.X) { // solve equation k*x+d=ym +|- sqrt(r^2-(x-xm)^2) // get k and d double k = 0; if (w.Start.Y != w.End.Y) { k = (w.Start.X - w.End.X) / (w.Start.Y - w.End.Y); } double d = w.Start.Y - (k * w.Start.X); int ym = atPosition.Y; int xm = atPosition.X; int r = obj.Size; double preSqrt = (-xm * xm * k * k) + 2 * xm * ym * k - 2 * xm * d * k - ym * ym + 2 * ym * d - d * d + k * k * r * r + r * r; if (preSqrt >= 0) { double sqrt = Math.Sqrt(preSqrt); double[] x = new double[2]; x[0] = (-sqrt + xm + ym * k - d * k) / (k * k + 1); x[1] = (sqrt + xm + ym * k - d * k) / (k * k + 1); //is at least one point on line for (int i = 0; i < x.Length; i++) { Point p = new Point(); p.X = (int)Math.Round(x[i]); p.Y = (int)Math.Round(k * x[i] + d); if (isPointOnLine(p, w)) { return true; } } } } else { double preSqrt = Math.Pow(obj.Size, 2) - Math.Pow((w.Start.X - atPosition.X), 2); if (preSqrt >= 0) { double[] y = new double[2]; y[0] = atPosition.Y + Math.Sqrt(preSqrt); y[1] = atPosition.Y - Math.Sqrt(preSqrt); for (int i = 0; i < y.Length; i++) { Point p = new Point(); p.X = w.Start.X; p.Y = (int)Math.Round(y[i]); if (isPointOnLine(p, w)) { return true; } } } } } return false; }
public void Teleport(ModelObject obj, Point toPosition) { if (toPosition.X < 0) { toPosition.X = 0; } if (toPosition.Y < 0) { toPosition.Y = 0; } if (toPosition.X > obj.Model.Map.GetWidth()) { toPosition.X = obj.Model.Map.GetWidth(); } if (toPosition.Y > obj.Model.Map.GetHeight()) { toPosition.Y = obj.Model.Map.GetHeight(); } obj.Model.MoveModelObject(obj.ID, toPosition); }
public void MoveModelObject(ModelObject obj, Point toPosition) { Point endPoint = ProcessWay(obj, toPosition, CheckForWallsWhileMoving, true); obj.Model.MoveModelObject(obj.ID, endPoint); }
public Point MayMoveTo(ModelObject obj, Point toPosition) { return ProcessWay(obj, toPosition, true, false); }