Beispiel #1
0
 public Scene(List <IDrawable> drawables,
              List <IUpdateable> updateables,
              List <ILoadContent> loadables,
              List <IDog> dogs,
              IDictionary <string, Portal> portals,
              Player player,
              WalkingArea walkingArea,
              int width,
              float characterScalingMin,
              string songName)
 {
     Player    = player;
     Drawables = drawables;
     Drawables.Add(player);
     Updateables = updateables;
     Loadables   = loadables;
     Loadables.Add(player);
     Dogs                = dogs;
     Portals             = portals;
     Player              = player;
     Width               = width;
     WalkingArea         = walkingArea;
     CharacterScalingMin = characterScalingMin;
     SongName            = songName;
 }
Beispiel #2
0
    IEnumerator PlayerMovement(WalkingArea selectedTile)
    {
        StartAnimatingWalk();
        moving = true;
        var path = NavigationScript.CreatePathToTarget(pathMap.paths, selectedTile);

        while (path.Count > 0 && !died)
        {
            var nextStep = path.Last();
            path.Remove(nextStep);
            while ((transform.position - nextStep.CharacterSocket.position).sqrMagnitude > 0.003f)
            {
                if (!footAudioSource.isPlaying)
                {
                    footAudioSource.PlayOneShot(footstepsClips[Random.Range(0, footstepsClips.Length)]);
                }
                PlayerPosition     = transform.position;
                transform.position = Vector2.MoveTowards(transform.position, nextStep.CharacterSocket.position, speed * Time.deltaTime);
                yield return(null);
            }
        }

        startingTile = selectedTile;
        StopWalkingAnimation();

        pathMap = NavigationScript.CreatePathsTreeFromStart(startingTile);
        moving  = false;
    }
Beispiel #3
0
    public static void CallTileCleared(WalkingArea tile)
    {
        var handler = TileCleared;

        if (handler != null)
        {
            handler(tile);
        }
    }
Beispiel #4
0
    public virtual IEnumerator ExecuteTurn()
    {
        bool endTurn = false;

        if (nextWaypoint == null)
        {
            nextWaypoint = waypoints.First();
        }
        StartAnimatingWalk();
        StartTurn();
        var path = NavigationScript.CreatePathToTarget(map.paths, nextWaypoint);

        while (!endTurn)
        {
            int movementPoints = remainingMovementPoints;
            yield return(StartCoroutine(CheckForAttack()));

            if (!(movementPoints == remainingMovementPoints))
            {
                StartAnimatingWalk();
            }
            var nextStep = path.Last();
            if (nextStep.IsSocketOccupied)
            {
                endTurn = true;
            }
            else
            {
                path.Remove(nextStep);
                SpendMovementPoints(map.costs[nextStep]);
                transform.localScale = new Vector3(Mathf.Sign(nextStep.CharacterSocket.position.x - transform.position.x), 1, 1);
                while ((transform.position - nextStep.CharacterSocket.position).sqrMagnitude > 0.003f)
                {
                    transform.position = Vector2.MoveTowards(transform.position, nextStep.CharacterSocket.position, speed * Time.deltaTime);
                    yield return(null);
                }
                startingTile = nextStep;
            }

            if (nextWaypoint == startingTile)
            {
                nextWaypoint = nextWaypoint == waypoints.Last() ? waypoints[0] : waypoints[waypoints.IndexOf(nextWaypoint) + 1];
                map          = NavigationScript.CreatePathsTreeFromStart(startingTile);
                path         = NavigationScript.CreatePathToTarget(map.paths, nextWaypoint);
            }
            nextStep = path.Last();

            endTurn = endTurn?true:(remainingMovementPoints - nextStep.cost) < 0;
            if (endTurn)
            {
                StopWalkingAnimation();
            }
            yield return(null);
        }
    }
Beispiel #5
0
    protected virtual void Start()
    {
        var hit = Physics2D.Raycast(transform.position, Vector2.down, 1, LayerMask.GetMask("Ground"));

        if (hit)
        {
            //Debug.Log(hit.transform.name);
            startingTile       = hit.transform.GetComponent <WalkingArea>();
            transform.position = startingTile.CharacterSocket.position;
        }
        currentHitPoints = startingHitPoints;
        RegisterEnemy();
    }
Beispiel #6
0
    protected virtual void OnTileClicked(WalkingArea tile)
    {
        return;

        if (tile == this)
        {
            rend.color = reachableColor;
        }
        else
        {
            rend.color = Color.white;
        }
    }
Beispiel #7
0
 void OnNewTileClicked(WalkingArea tile)
 {
     if (!playersTurn || moving)
     {
         return;
     }
     if (tile != null && selectedTile == tile)
     {
         if (pathMap.costs[selectedTile] <= remainingMovementPoints)
         {
             MoveToCurrentTile();
             UseMovementPoints(pathMap.costs[selectedTile]);
         }
     }
 }
Beispiel #8
0
    public IEnumerator ExecuteTurn()
    {
        bool endTurn = false;

        StartTurn();
        startingTile.LeaveSocket(gameObject);
        while (!endTurn && !died)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                endTurn = true;
                startingTile.TakeSocket(gameObject);
                PlayerPosition = transform.position;
                playersTurn    = false;
                WalkingArea.CallTileCleared(null);
            }
            else if (Input.GetMouseButtonDown(1) && !moving && remainingMovementPoints >= ShotCost)
            {
                UseMovementPoints(ShotCost);
                Vector2 crosshairVector = ((Vector2)(crosshair.position - gunTransform.position)).normalized;
                var     shotHit         = Physics2D.Raycast(gunTransform.position, crosshairVector, float.PositiveInfinity, hitMask);
                var     trace           = new Vector3[2];
                trace[0] = new Vector3(gunTransform.position.x, gunTransform.position.y, 0);
                if (shotHit)
                {
                    trace[1] = new Vector3(shotHit.point.x, shotHit.point.y, 0);
                    bulletTrace.SetPositions(trace);
                    var enemy = shotHit.transform.GetComponent <EnemyScript>();
                    if (enemy)
                    {
                        Debug.Log("found Enemy");
                        enemy.TakeDamage(ShotDamage, crosshairVector);
                    }
                }
                else
                {
                    var missPoint = (Vector2)transform.position + crosshairVector * 25;
                    trace[1] = trace[1] = new Vector3(missPoint.x, missPoint.y, 0);
                    bulletTrace.SetPositions(trace);
                }
                weaponAudioSource.PlayOneShot(weaponClips[Random.Range(0, weaponClips.Length)]);
                StartCoroutine(DisableBulletTrace());
            }
            yield return(null);
        }
    }
Beispiel #9
0
    public static List <WalkingArea> CreatePathToTarget(Dictionary <WalkingArea, WalkingArea> pathsTree, WalkingArea target)
    {
        var         path = new List <WalkingArea> ();
        WalkingArea step = target;

        // check if a path exists
        if (pathsTree.ContainsKey(target))
        {
            path.Add(step);
            while (pathsTree.ContainsKey(step))
            {
                step = pathsTree[step];
                path.Add(step);
            }
        }
        return(path);
    }
Beispiel #10
0
    public static NavigationMap CreatePathsTreeFromStart(WalkingArea start)
    {
        var tilesChecked  = new HashSet <WalkingArea>();
        var toBeChecked   = new List <WalkingArea>();
        var previousPoint = new Dictionary <WalkingArea, WalkingArea>();
        var travelCost    = new Dictionary <WalkingArea, int>();

        travelCost.Add(start, 0);
        toBeChecked.Add(start);

        while (toBeChecked.Count > 0)
        {
            toBeChecked.Sort((x, y) => travelCost[y] - travelCost[x]);
            WalkingArea current = toBeChecked.Last();
            toBeChecked.Remove(current);
            tilesChecked.Add(current);
            foreach (var next in current.neighbours)
            {
                //Debug.Log ("checking tile " + next.transform.name);
                if (!tilesChecked.Contains(next))
                {
                    var nextCost = travelCost[current] + next.cost;
                    if (travelCost.ContainsKey(next))
                    {
                        //Debug.Log ("path to tile " + next.transform.name+ "already exists");
                        if (nextCost < travelCost[next])
                        {
                            //Debug.Log ("path to tile " + next.transform.name+ "has lower cost");
                            travelCost[next] = nextCost;
                            previousPoint.Add(next, current);
                        }
                    }
                    else
                    {
                        //Debug.Log ("path to tile " + next.transform.name+ "didn't exist, adding");
                        travelCost.Add(next, nextCost);
                        previousPoint.Add(next, current);
                    }
                    toBeChecked.Add(next);
                }
            }
        }
        var map = new NavigationMap(previousPoint, travelCost);

        return(map);
    }
Beispiel #11
0
    private void OnTileHovered(WalkingArea tile)
    {
        if (!playersTurn || moving)
        {
            return;
        }

        if (selectedTile == null || selectedTile != tile)
        {
            selectedTile = tile;
            var path = NavigationScript.CreatePathToTarget(pathMap.paths, tile);
            int remainingMovement = remainingMovementPoints;
            for (int i = path.Count - 2; i >= 0; i--)
            {
                var step = path[i];
                remainingMovement -= step.cost;

                step.rend.color = remainingMovement >= 0 ? step.reachableColor : step.unreachableColor;
            }
            UIController.Instance.SetMovementCostTextValue(pathMap.costs[selectedTile]);
        }
    }
Beispiel #12
0
    // Use this for initialization
    void Awake()
    {
        died = false;
        WalkingArea.TileClicked += OnNewTileClicked;
        PlayerAttacked          += OnPlayerAttacked;
        groundMask = LayerMask.GetMask("Ground", "Ladders");
        hitMask    = LayerMask.GetMask("Ground", "Obstacles", "Enemies");
        var hit = Physics2D.Raycast(transform.position, Vector2.down, 1, groundMask);

        if (hit)
        {
            Debug.Log(hit.transform.name);
            startingTile       = hit.transform.GetComponent <WalkingArea>();
            transform.position = startingTile.CharacterSocket.position;
        }
        var hp = PlayerPrefs.GetInt("hp");

        playerHealth = hp == 0 ? startingPlayerHealth : hp;
        UIController.Instance.SetPlayerHealthTextValue(playerHealth);
        camera                   = Camera.main;
        PlayerPosition           = transform.position;
        WalkingArea.TileHovered += OnTileHovered;
        WalkingArea.TileCleared += OnTileCleared;
    }
        public static Scene ParseSceneXml(string sceneXmlPath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(sceneXmlPath);
            XmlNode                      baseNode            = doc.DocumentElement;
            XmlNodeList                  nodes               = baseNode.ChildNodes;
            int                          sceneWidth          = int.Parse(baseNode.Attributes["width"].Value);
            List <Tileset>               tilesets            = new List <Tileset>();
            List <IDrawable>             drawables           = new List <IDrawable>();
            List <IUpdateable>           updateables         = new List <IUpdateable>();
            List <IDog>                  dogs                = new List <IDog>();
            List <ILoadContent>          loadables           = new List <ILoadContent>();
            IDictionary <string, Portal> portals             = new Dictionary <string, Portal>();
            WalkingArea                  walkingArea         = null;
            Player                       player              = null;
            float                        characterScalingMin = 1.0F;

            XmlNode propertiesNode = GetChildNode(baseNode, "properties");
            string  songName       = GetPropertyValue(propertiesNode, "song");

            // Get tileset first to be used when loading dogs
            foreach (XmlNode node in nodes)
            {
                if (node.Name == "tileset")
                {
                    string tilesetXmlPathRelative = node.Attributes["source"].Value;
                    string tilesetXmlPath         = Path.Combine(Path.GetDirectoryName(sceneXmlPath),
                                                                 Path.GetDirectoryName(tilesetXmlPathRelative),
                                                                 Path.GetFileName(tilesetXmlPathRelative));
                    int     tilesetFirstGid = int.Parse(node.Attributes["firstgid"].Value);
                    Tileset tileset         = TilesetParser.ParseTilesetXml(tilesetXmlPath, tilesetFirstGid);
                    tilesets.Add(tileset);
                    loadables.Add(tileset);
                }
            }

            foreach (XmlNode node in nodes)
            {
                if (node.Name == "imagelayer")
                {
                    if (node.Attributes["name"]?.InnerText == "background")
                    {
                        Debug.Assert(node.ChildNodes.Count == 1, "More than one background layer in scene");
                        ImageLayer background = ParseImageNode(node.ChildNodes[0], sceneXmlPath, 0);
                        drawables.Add(background);
                        loadables.Add(background);
                    }
                    else if (node.Attributes["name"]?.InnerText == "foreground")
                    {
                        Debug.Assert(node.ChildNodes.Count == 1, "More than one foreground layer in scene");
                        ImageLayer foreground = ParseImageNode(node.ChildNodes[0], sceneXmlPath, 1000);
                        drawables.Add(foreground);
                        loadables.Add(foreground);
                    }
                }
                else if (node.Name == "objectgroup" && node.Attributes["name"]?.InnerText == "dogs")
                {
                    foreach (XmlNode dogNode in node.ChildNodes)
                    {
                        IDog dog = ParseDogNode(dogNode, tilesets, sceneWidth);
                        drawables.Add(dog);
                        dogs.Add(dog);
                    }
                }
                else if (node.Name == "objectgroup" && node.Attributes["name"]?.InnerText == "player")
                {
                    Debug.Assert(node.ChildNodes.Count == 1);
                    XmlNode playerNode = node.ChildNodes[0];
                    int     x          = (int)Math.Round(float.Parse(playerNode.Attributes["x"].Value));
                    int     y          = (int)Math.Round(float.Parse(playerNode.Attributes["y"].Value));
                    y -= (int)Math.Round(float.Parse(playerNode.Attributes["height"].Value)); // Compensate for Tiled's coordinate system
                    string name = playerNode.Attributes["name"]?.InnerText;
                    player = new Player(x, y, name ?? "Felixia");
                }
                else if (node.Name == "objectgroup" && node.Attributes["name"]?.InnerText == "walking")
                {
                    walkingArea = new WalkingArea(ParsePolygonXml(node.ChildNodes[0]), sceneWidth);
                    drawables.Add(walkingArea);
                }
                else if (node.Name == "objectgroup" && node.Attributes["name"]?.InnerText == "portals")
                {
                    foreach (XmlNode portalNode in node.ChildNodes)
                    {
                        Portal portal = ParsePortalNode(portalNode, sceneWidth);
                        portals[portal.Name] = portal;
                        drawables.Add(portal);
                    }
                }
                else if (node.Name == "properties")
                {
                    characterScalingMin = float.Parse(GetPropertyValueOrDefault(node, "scaling_min", "1.0"));
                }
                if (player == null)
                {
                    player = new Player("Felixia");
                }
            }
            return(new Scene(drawables,
                             updateables,
                             loadables,
                             dogs,
                             portals,
                             player,
                             walkingArea,
                             sceneWidth,
                             characterScalingMin,
                             songName));
        }
Beispiel #14
0
 private void OnTileCleared(WalkingArea tile)
 {
     selectedTile = null;
 }
Beispiel #15
0
 private void ClearTileRender(WalkingArea tile)
 {
     rend.color = Color.white;
 }