Пример #1
0
        public void AddConnection(WayPoint w)
        {
            // WP adden
            connectedPoints.Add(w);

            // Distanz speichern
            float distance = (float)Math.Sqrt(Math.Pow(w.Location.Position.X - Location.Position.X, 2) + Math.Pow(w.Location.Position.Y - Location.Position.Y, 2));
            distances.Add(w.ID, distance);
        }
Пример #2
0
 public static PathNode FindePath(WayPoint start, WayPoint target)
 {
     if (!callculated)
     {
         if (savedPaths.ContainsKey(start.ID) && savedPaths[start.ID].ContainsKey(target.ID))
             return savedPaths[start.ID][target.ID].Clone();
         return _findePath(start, target);
     }
     else
         return savedPaths[start.ID][target.ID].Clone();
 }
Пример #3
0
        // Tue die Action
        public override void CalculateAction(Enemy e)
        {
            // ist player in sichweite
            if (e.DistanceLessThan(_player, e.SightiningDistance))
            {
                // Kann er den playe sehen oder ist dieser von einem objekt bedeckt / 20x20 ist ca. ein schuss
                if (GameManager.PointSeePoint(e.LocationBehavior.Position, _player.LocationBehavior.Position, new Vector2(20, 20)))
                {
                    // kann spieler sehen
                    _sawPlayer = true;
                    _sawPlayerAt = _player.LocationBehavior.Position;

                    // wenn er in angriffreichweite ist angreifen
                    if (e.DistanceLessThan(_player, e.SightiningDistance))
                    {
                        _lookAtPlayer = true;
                        _attack = true;
                        _lookAtNextNode = false;
                        _walk = false;

                        return;
                    }
                    else
                    {
                        // Kann er zum spieler laufen ohne an eine wand zu stoßen?
                        if (GameManager.PointSeePoint(e.LocationBehavior.Position, _player.LocationBehavior.Position, e.LocationBehavior.Size))
                        {
                            _lookAtPlayer = true;
                            _attack = false;
                            _lookAtNextNode = false;
                            _walk = true;

                            return;
                        }
                    }
                }
            }
            else
            {
                _lookAtPlayer = false;
                _attack = false;
            }

            // ## Sieht er den player und kann schießen wird abgebrochen

            // wenn er den spiele sehen konnte aber jetzt nicht mehr
            if (_sawPlayer)
            {
                // sieht player nicht mehr
                _sawPlayer = false;

                // Suche den nähsten WP und schaue ob er auf der route lag
                WayPoint _newNearest = Karte.SearchNearest(_sawPlayerAt);

                if (_newNearest != null)
                {

                    // nearest in path
                    PathNode nearestInPath = null;

                    // wenn der nähste wegpunkt im pfad ist, diesen als neues ziel setzten
                    if (_currentPath.IsWaypointInPath(_newNearest.ID, out nearestInPath))
                    {
                        _currentPath = nearestInPath;
                    }
                    else
                    {
                        // ansonsten den aktuellen pfad löschen und neuen berechnen
                        _nearest = _newNearest;
                        _currentPath = null;
                    }

                }
            }

            //

            // WP des Players
            WayPoint playerWayPoint = Main.MainObject.GameManager.GameState.Player.NearestWayPoint;

            // falls playerwegpunkt neu ist pfad neu berechnen und auch neuen nearest suchen
            if (playerWayPoint != null && _lastPlayerWaypoint != playerWayPoint)
            {
                _nearest = null;
                _currentPath = null;
            }

            // Falls es kein Wegpunkt gibt, der am nähesten bei mir ist
            if (_nearest == null)
                _nearest = Karte.SearchNearest(e.LocationBehavior.Position);

            // Sollte es kein Wegpunkt in meiner Nähe geben abbrechen
            if (_nearest == null)
                return;

            // wenn es kein pfad gigt, pfad zu player berechnen
            if (_currentPath == null && playerWayPoint != null)
            {
                _lastPlayerWaypoint = playerWayPoint;
                _currentPath = PathFinder.FindePath(_nearest, playerWayPoint);
            }

            // wenn _currentPath == null stehen bleiben )
            if (_currentPath != null)
            {
                // Steht enemy über wp => next wp
                if (Vector2.Distance(_currentPath.WayPoint.Location.Position, e.LocationBehavior.Position) < 10)
                {
                    // wenn es einen nächste wp gibt gehe zu dem ansonsten laufen=false /stehen bleiben
                    if (_currentPath.NextNode == null)
                    {
                        _lookAtPlayer = false;
                        _attack = false;
                        _lookAtNextNode = false;
                        _walk = false;
                    }
                    else
                    {
                        _currentPath = _currentPath.NextNode;

                        _lookAtPlayer = false;
                        _attack = false;
                        _lookAtNextNode = true;
                        _walk = true;
                    }
                }
                else
                {
                    // checken ob enemy festhängt - hängt fest wenn er halb so schnell wie normal läuft
                    if (_walkedDistance < e.Speed / 2)
                    {
                        searchNextVisibleNode(e);
                    }

                    _lookAtPlayer = false;
                    _attack = false;
                    _lookAtNextNode = true;
                    _walk = true;
                }
            }
            else
            {
                _lookAtPlayer = false;
                _attack = false;
                _lookAtNextNode = false;
                _walk = false;
            }
        }
Пример #4
0
        private static PathNode _findePath(WayPoint start, WayPoint target)
        {
            // Listen init
            openList = new List<PathNode>();
            closedList = new List<PathNode>();

            allNodesTemp = CloneNodeDic();

            // Start einfügen
            PathNode startNode = new PathNode(0, start);
            openList.Add(startNode);

            // solange openlist nicht leer ist
            while (openList.Count > 0)
            {
                //next Node aus openlist holen
                PathNode next = openList[0];
                openList.RemoveAt(0);

                // Nachbarn einfügen
                expandNode(next);

                // Node in closeliste einfügn
                closedList.Add(next);

                // prüfen ob ich schon fertig bin
                if (next.ID == target.ID)
                    break;
            }

            // pfad bauen
            buildPath(ref startNode, target.ID);

            return startNode;
        }
Пример #5
0
 public PathNode(float distance, WayPoint waypoint)
 {
     Distance = distance;
     WayPoint = waypoint;
     ID = waypoint.ID;
 }
Пример #6
0
        private bool ReadMapFile(string mapFile)
        {
            Debug.WriteLine("Lade Map " + mapFile + "..");

            // prüfen ob config.ini existiert
            if (!File.Exists(Configuration.Get("mapDir") + mapFile))
            {
                Debug.WriteLine("MapFile " + mapFile + " nicht vorhanden");
                return false;
            }

            // Map File öffnen
            TextReader tr = new StreamReader(Configuration.Get("mapDir") + mapFile);

            // Debug
            int rectCount = 0;

            // Alle lines einlesen, bei ',' trennen und diese bei [0]=R in Rechtecke einteilen
            string input;
            while ((input = tr.ReadLine()) != null)
            {
                // falls erstes zeichen eine # ist, dann ist die zeile ein kommenatar
                if (input.Length < 1 || input.Substring(0, 1).Equals("#"))
                {
                    continue;
                }

                string[] split = input.Split(new char[] { ',' });

                // Map Rechteck
                if (split[0].Equals("R") && split.Length == 5)
                {
                    rectCount++;

                    // Variablen parsen
                    int x = int.Parse(split[1]);
                    int y = int.Parse(split[2]);
                    int w = int.Parse(split[3]);
                    int h = int.Parse(split[4]);

                    // SO erstellen
                    StaticObject so = new StaticObject(new MapLocation(new Rectangle(x, y, w, h)), new SimpleRenderer(Color.White), false);

                    // SO dem QT zufügen
                    QuadTreeWalkable.Add(so);
                }

                // Playerstart
                if (split[0].Equals("X") && split.Length == 3)
                {
                    // Variablen parsen
                    int x = int.Parse(split[1]);
                    int y = int.Parse(split[2]);

                    // SO erstellen
                    PlayerStart = new Vector2(x, y);

                    Debug.WriteLine("Playerstart: " + x + "/" + +y);
                }

                // SpawnPoints
                if (split[0].Equals("S") && split.Length == 3)
                {
                    // Variablen parsen
                    int x = int.Parse(split[1]);
                    int y = int.Parse(split[2]);

                    // SP erstellen
                    SpawnPoint sp = new SpawnPoint(new MapLocation(new Vector2(x, y)), EEnemyType.E3, 1);

                    // SP adden
                    Main.MainObject.GameManager.GameState.QuadTreeSpawnPoints.Add(sp);
                }

                // Objekte etc
                if (split[0].Equals("M") && split.Length == 6)
                {
                    // Variablen parsen
                    string img = split[1];
                    int x = int.Parse(split[2]);
                    int y = int.Parse(split[3]);
                    int faktor = int.Parse(split[4]);
                    int winkel = int.Parse(split[5]);

                    // Vars für SO
                    string renderer = "S_Map_" + img;

                    float rotation = (float)((Math.PI * 2) / 360 * winkel);

                    // SP erstellen
                    MapLocation m = new MapLocation(new Vector2(x, y));
                    m.Rotation = rotation;
                    IRenderBehavior r = LoadedRenderer.Get(renderer);
                    StaticObject so = new StaticObject(m, r);
                    so.LocationSizing();

                    // SP adden
                    Main.MainObject.GameManager.GameState.QuadTreeStaticObjects.Add(so);
                }

                // Wegpunkte W,48,1700,4538
                if (split[0].Equals("W") && split.Length == 4)
                {
                    // Variablen parsen
                    int id = int.Parse(split[1]);
                    int x = int.Parse(split[2]);
                    int y = int.Parse(split[3]);

                    // WP
                    WayPoint w = new WayPoint(x, y);
                    w.ID = id;

                    // WP adden
                    Main.MainObject.GameManager.GameState.Karte.WayPoints.Add(id, w);
                    Main.MainObject.GameManager.GameState.Karte.QuadTreeWayPoints.Add(w);
                }

                // Wegpunkte C,11,10
                if (split[0].Equals("C") && split.Length == 3)
                {
                    // Variablen parsen
                    int srcId = int.Parse(split[1]);
                    int desId = int.Parse(split[2]);

                    // Connection in BEIDE richtung
                    WayPoints[srcId].AddConnection(WayPoints[desId]);
                    WayPoints[desId].AddConnection(WayPoints[srcId]);
                }
            }

            // File schließen
            tr.Close();

            //Debug
            Debug.WriteLine(rectCount + " Rects geladen");

            return true;
        }