public void Update(float speed, Whereabouts previous) { var last = PathToWalk.Last(); if (Math.Abs(last.Location.X - previous.Location.X) >= 2 || Math.Abs(last.Location.Y - previous.Location.Y) >= 2) { //bugfix... var p = new Point((previous.Location.X + last.Location.X)/2, (previous.Location.Y + last.Location.Y)/2); PathToWalk[PathToWalk.Count - 1] = new Whereabouts(last.Floor, p, last.Direction) {Fraction = previous.Fraction}; } if (PathToWalk.Count >= 2) { var w = PathToWalk[0]; var fraction = w.Fraction + speed; while (fraction >= 1) { if(PathToWalk.Count>=2) PathToWalk.RemoveAt(0); w = PathToWalk[0]; fraction -= 1; } w.Fraction = fraction; PathToWalk[0] = w; } if (Next != null) Next.Update(speed, PathToWalk.First()); }
public void Restart(PlayingField playingField, int length, Whereabouts? whereabouts = null) { Speed = InitialSpeed; Restart(playingField, whereabouts.GetValueOrDefault(PlayingField.PlayerWhereaboutsStart)); DirectionTaker = null; SerpentStatus = SerpentStatus.Alive; while (length-- > 0) AddTail(); }
public void AddPathToWalk(Whereabouts w) { if (PathToWalk.Exists(e => e.Location == w.Location)) return; w.Fraction = 0; PathToWalk.Add(w); if (Next != null) Next.AddPathToWalk(PathToWalk[0]); }
public Direction WayHome(Whereabouts whereabouts, bool canTurnAround) { if (getDistance(whereabouts.Floor, whereabouts.Location, Direction.None) <= 2) return Direction.None; // is home! var backward = whereabouts.Direction.Turn(RelativeDirection.Backward); var bestDirection = backward; var bestDistance = int.MaxValue; foreach (var direction in Direction.AllDirections) if (canTurnAround || direction != backward) testDistance(whereabouts.Floor, whereabouts.Location, ref bestDirection, ref bestDistance, direction); return bestDirection; }
public static PlayingFieldSquare[,,] Create( IList<string[]> fields, int width, int height, ref Whereabouts playerWhereaboutsStart, ref Whereabouts enemyWhereaboutsStart) { var result = new PlayingFieldSquare[fields.Count, height, width]; var builder = new PlayingFieldBuilder(result); for (var i = 0; i < fields.Count; i++) builder.ConstructOneFloor( i, fields[i], ref playerWhereaboutsStart, ref enemyWhereaboutsStart); return result; }
public Egg( IVEffect effect, IVDrawable sphere, Texture2D eggSkin, Vector4 diffuseColor, Matrix world, Whereabouts whereabouts, float timeToHatch) : base(effect) { _sphere = sphere; _eggSkin = eggSkin; _diffuseColor = diffuseColor; _world = world * Matrix.Translation(0, -0.2f, 0); Whereabouts = whereabouts; _timeToHatch = timeToHatch; }
public PlayingField(LarvContent lContent, Texture2D texture, int level) : base(lContent.TextureEffect) { _texture = texture; var pfInfo = lContent.PlayingFieldInfos[level]; TheField = pfInfo.PlayingField; Floors = pfInfo.Floors; Height = pfInfo.Height; Width = pfInfo.WIdth; PlayerWhereaboutsStart = pfInfo.PlayerSerpentStart; EnemyWhereaboutsStart = pfInfo.EnemySerpentStart; MiddleX = Width/2f; MiddleY = Height/2f; var verts = new List<VertexPositionNormalTexture> { new VertexPositionNormalTexture(new Vector3(-1, 0, -1), Vector3.Up, new Vector2(0, 0)), new VertexPositionNormalTexture(new Vector3(Width + 1, 0, -1), Vector3.Up, new Vector2(Width*0.25f, 0)), new VertexPositionNormalTexture(new Vector3(-1, 0, Height + 1), Vector3.Up, new Vector2(0, Height*0.25f)), new VertexPositionNormalTexture(new Vector3(Width + 1, 0, -1), Vector3.Up, new Vector2(Width*0.25f, 0)), new VertexPositionNormalTexture(new Vector3(Width + 1, 0, Height + 1), Vector3.Up, new Vector2(Width*0.25f, Height*0.25f)), new VertexPositionNormalTexture(new Vector3(-1, 0, Height + 1), Vector3.Up, new Vector2(0, Height*0.25f)) }; var vertsShadow = new List<VertexPositionNormalTexture>(); for (var floor = 0; floor < Floors; floor++) for (var y = 0; y < Height; y++ ) for (var x = 0; x < Width; x++) { var sq = TheField[floor, y, x]; if ((!sq.IsNone && floor != 0) || sq.IsSlope) contructSquare(verts, vertsShadow, floor, new Point(x,y), sq.Corners); } VertexBuffer = Buffer.Vertex.New(lContent.GraphicsDevice, verts.ToArray()); if(vertsShadow.Any()) VertexBufferShadow = Buffer.Vertex.New(lContent.GraphicsDevice, vertsShadow.ToArray()); VertexInputLayout = VertexInputLayout.FromBuffer(0, VertexBuffer); }
public SerpentTailSegment(PlayingField pf, Whereabouts w) { _pf = pf; PathToWalk = new List<Whereabouts> { w }; }
public void ConstructOneFloor( int floor, string[] field, ref Whereabouts playerWhereaboutsStart, ref Whereabouts enemyWhereaboutsStart) { _floor = floor; var height = field.Length; var width = field[0].Length; var expectedHeight = _field.GetUpperBound(1) + 1; var expectedWidth = _field.GetUpperBound(2) + 1; if (expectedHeight != height || expectedWidth != width) throw new Exception(); while (true) { var mustRunAgain = false; for (var y = 0; y < height; y++) for (var x = 0; x < width; x++) { if (!_field[floor, y, x].IsNone) continue; switch (field[y][x]) { case ' ': _field[floor, y, x] = new PlayingFieldSquare(); break; case 'A': playerWhereaboutsStart = calcWhereabouts(field, 'a', floor, x, y); goto case 'X'; case 'B': enemyWhereaboutsStart = calcWhereabouts(field, 'b', floor, x, y); goto case 'X'; case 'N': _field[floor, y, x] = PlayingFieldSquare.CreateFlat(0, DirectionValue.North); break; case 'S': _field[floor, y, x] = PlayingFieldSquare.CreateFlat(0, DirectionValue.South); break; case 'E': _field[floor, y, x] = PlayingFieldSquare.CreateFlat(0, DirectionValue.East); break; case 'W': _field[floor, y, x] = PlayingFieldSquare.CreateFlat(0, DirectionValue.West); break; case 'X': case 'a': case 'b': _field[floor, y, x] = PlayingFieldSquare.CreateFlat(0); break; case 'D': _field[floor, y, x] = createSlopeSquare(x, y, true); mustRunAgain |= _field[floor, y, x].IsNone; break; case 'U': _field[floor, y, x] = createSlopeSquare(x, y, false); mustRunAgain |= _field[floor, y, x].IsNone; break; } } if (!mustRunAgain) return; } }
public int GetDistance(Whereabouts whereabouts) { return Distance[whereabouts.Floor, whereabouts.Location.Y, whereabouts.Location.X]; }
private float getElevation( Whereabouts whereabouts) { var p1 = whereabouts.Location; var p2 = whereabouts.NextLocation; var floor1 = whereabouts.Floor; var floor2 = floor1; var square1 = fieldValue(ref floor1, p1); var square2 = fieldValue(ref floor2, p2); return square1.IsNone ? 0 : MathUtil.Lerp(floor1*4 + square1.Elevation, floor2*4 + square2.Elevation, whereabouts.Fraction)/3; }
public float GetElevation( Whereabouts whereabouts) { whereabouts.Fraction -= 0.5f; whereabouts.Realign(); var x = getElevation(whereabouts); whereabouts.Location = whereabouts.NextLocation; return Math.Max(x, getElevation(whereabouts)); }
public PlayingFieldSquare FieldValue(Whereabouts whereabouts) { return FieldValue(whereabouts.Floor, whereabouts.Location); }
public void Restart(PlayingField playingField, Whereabouts whereabouts) { PlayingField = playingField; _whereabouts = whereabouts; HeadDirection = _whereabouts.Direction; _tail = new SerpentTailSegment(PlayingField, _whereabouts); _serpentLength = 1; _ascendToHeaven = 0; _layingEgg = -1; _pendingEatenSegments = 6; }
public void SetPosition(Whereabouts whereabouts, PlayingField playingField) { SetPosition(whereabouts.GetPosition(playingField), whereabouts.Direction); }
public int LocationDistanceSquared(Whereabouts w) { var x = Location.X - w.Location.X; var y = Location.Y - w.Location.Y; return x*x + y*y; }
public PathFinder(PlayingField pf, Whereabouts home) { PlayingField = pf; Distance = new int[pf.TheField.GetUpperBound(0)+1, pf.TheField.GetUpperBound(1)+1, pf.TheField.GetUpperBound(2)+1]; Explore(home.Floor, home.Location, 1, Direction.None); }