Inheritance: MonoBehaviour
Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < 4; i++)
        {
            if (i > characters.Count - 1)
            {
                huds [i].SetActive(false);
            }
            else
            {
                huds [i].SetActive(true);

                RLCharacter  c  = characters [i];
                HudComponent hc = huds [i].GetComponent <HudComponent> ();
                hc.portrait.GetComponent <SpriteRenderer> ().sprite = GetSprite(c.name);
                for (int h = 0; h < 4; h++)
                {
                    if (c.health > h)
                    {
                        hc.hearts [h].GetComponent <SpriteRenderer> ().sprite = GetSprite("health");
                    }
                    else
                    {
                        hc.hearts [h].GetComponent <SpriteRenderer> ().sprite = GetSprite("health_empty");
                    }
                }
                for (int h = 0; h < 5; h++)
                {
                    if (c != null && c.GetComponent <ActionCounter>().actionsRemaining > h)
                    {
                        hc.potions [h].GetComponent <SpriteRenderer> ().sprite = GetSprite("action");
                    }
                    else
                    {
                        hc.potions [h].GetComponent <SpriteRenderer> ().sprite = GetSprite("action_empty");
                    }
                }
            }
        }
    }
 bool ContainsType(int x, int y, RLCharacter.RLTypes t)
 {
     for (int i = objects.Count - 1; i >= 0; i--) {
         if (objects[i].positionI.x == x && objects[i].positionI.y == y && objects [i].hasTypes.Contains (t)) {
             return true;
         }
     }
     return false;
 }
 // should add support for muliple types eventually
 // perhaps it would be better for types to nest themselves?
 // also, the map should really contain multiple layers,
 // all of which are expressed / exposed through these interfaces
 void OverlapCheck(OverlapCallback fn, RLCharacter.RLTypes ta, RLCharacter.RLTypes tb)
 {
     if (objects.Count > 0) {
         for (int i = objects.Count - 1; i >= 0; i--) {
             if (objects.Count > i && objects [i].hasTypes.Contains (ta)) {
                 for (int j = i; j >= 0; j--) {
                     if (objects.Count >= j && objects.Count >= i) {
                         if (i != j && objects.Count > j && objects [j].hasTypes.Contains (tb)
                              && objects [i].positionI.Equals (objects [j].positionI)) {
                             fn (objects [i], objects [j]);
                         }
                     }
                 }
             }
         }
     }
 }
Esempio n. 4
0
    void GenLevel()
    {
        currentLevel++;
        // clear the map and all current objects
        foreach (RLCharacter c in objects)
        {
            Destroy(c.gameObject);
        }
        objects.Clear();

        // rebuild the map
        // build the map
        map = new RL.Map(14, 12);

        // build the walls based on the underlying tiles
        for (int x = 0; x < map.sx; x++)
        {
            for (int y = 0; y < map.sy; y++)
            {
                /*
                 * assuming there is a sprite in the middle, here are the potential fills
                 * ...
                 * .x.
                 * ...
                 *
                 * ...
                 * xxx
                 * ...
                 *
                 * .x.
                 * .x.
                 * .x.
                 *
                 * oxi // note, need to do two for each, depending on the direction of internal / external
                 * oxx
                 * ooo
                 *
                 * ...
                 * .xx
                 * .x.
                 *
                 * .x.
                 * xx.
                 * ...
                 *
                 * what a pain in the butt. I am not going to f**k with this for right now, because it makes tons of assumptions
                 * about the map structure and room layout and stuff
                 */

                Sprite toInsert    = tl;
                bool   needsInsert = false;
                if (!map.IsOpenTile(x, y))
                {
                    toInsert    = t;
                    needsInsert = true;
                }
                if (needsInsert)
                {
                    GameObject s = (GameObject)Instantiate(spriteHolder, new Vector3(x, y, 0), Quaternion.identity);
                    s.GetComponent <SpriteRenderer> ().sprite = toInsert;
                    s.AddComponent <RLCharacter> ();
                    objects.Add(s.GetComponent <RLCharacter>());
                }
            }
        }

        GameObject playerGO = (GameObject)Instantiate(spriteHolder, new Vector3(4, 3, 0), Quaternion.identity);

        player = playerGO.AddComponent <RLCharacter> ();
//		player = playerGO.GetComponent<RLCharacter> ();
        player.GetComponent <SpriteRenderer> ().sprite = playerR;
        player.name = "player";
        objects.Add(player);
        currentFacing = Facing.RIGHT;

        // add a random number of monsters
        int nMonsters = currentLevel;

        for (int i = 0; i < nMonsters; i++)
        {
            GameObject m = (GameObject)Instantiate(monster, new Vector3(Random.Range(1, map.sx - 1), Random.Range(1, map.sy - 1)), Quaternion.identity);
            m.GetComponent <RLCharacter> ().AddType(RLCharacter.RLTypes.MONSTER);
            objects.Add(m.GetComponent <RLCharacter> ());
        }

        // add some downward facing stairs
        GameObject stair = (GameObject)Instantiate(spriteHolder, new Vector3(Random.Range(1, map.sx - 1), Random.Range(1, map.sy - 1)), Quaternion.identity);

        stair.GetComponent <SpriteRenderer> ().sprite = stairsDown;
        stair.AddComponent <RLCharacter> ();
        stair.GetComponent <RLCharacter> ().AddType(RLCharacter.RLTypes.STAIRS_DOWN);
        objects.Add(stair.GetComponent <RLCharacter> ());
    }
Esempio n. 5
0
    // Update is called once per frame
    void PlayerUpdate()
    {
        bool shouldTransition = false;

        // should stop the player from making moves into enemies
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            player.SetPosition(player.positionI.x, player.positionI.y - 1);
            currentFacing = Facing.DOWN;
            player.GetComponent <SpriteRenderer> ().sprite = playerD;
            shouldTransition = true;
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            player.SetPosition(player.positionI.x, player.positionI.y + 1);
            currentFacing = Facing.UP;
            player.GetComponent <SpriteRenderer> ().sprite = playerU;
            shouldTransition = true;
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            player.SetPosition(player.positionI.x + 1, player.positionI.y);
            currentFacing = Facing.RIGHT;
            player.GetComponent <SpriteRenderer> ().sprite = playerR;
            shouldTransition = true;
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            player.SetPosition(player.positionI.x - 1, player.positionI.y);
            currentFacing = Facing.LEFT;
            player.GetComponent <SpriteRenderer> ().sprite = playerL;
            shouldTransition = true;
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (currentBullets <= 0)
            {
                return;
            }
            currentBullets--;
            bulletDisplay [currentBullets].gameObject.SetActive(false);
            // fire current weapon
            GameObject  bulletGO = (GameObject)Instantiate(spriteHolder, new Vector3(player.positionI.x, player.positionI.y, 0), Quaternion.identity);
            RLCharacter bullet   = bulletGO.AddComponent <RLCharacter> ();
            bullet.direction = currentFacing;
            // move the bullet out of the player
            if (bullet.direction == Facing.LEFT || bullet.direction == Facing.RIGHT)
            {
                bullet.GetComponent <SpriteRenderer> ().sprite = bulletH;
            }
            else
            {
                bullet.GetComponent <SpriteRenderer> ().sprite = bulletV;
            }
            bullet.name = "bullet";
            bullet.AddType(RLCharacter.RLTypes.BULLET);
            bullet.GetComponent <SpriteRenderer> ().color = Color.grey;
            objects.Add(bullet);
            // push the player away from the facing direction if possible
            Facing opposite = oppositeDirection(currentFacing);
            if (map.IsOpenTile(player.positionI.x + RL.Map.nDir [(int)opposite, 0], player.positionI.y + RL.Map.nDir [(int)opposite, 1]) &&
                !ContainsType(player.positionI.x + RL.Map.nDir [(int)opposite, 0], player.positionI.y + RL.Map.nDir [(int)opposite, 1], RLCharacter.RLTypes.MONSTER))
            {
                player.SetPosition(player.positionI.x + RL.Map.nDir [(int)opposite, 0], player.positionI.y + RL.Map.nDir [(int)opposite, 1]);
            }
            shouldTransition = true;
        }
        // check to see if the player is on the stairs, and if so, regen the level
        if (ContainsType(player.positionI.x, player.positionI.y, RLCharacter.RLTypes.STAIRS_DOWN))
        {
            GenLevel();
        }
        else if (shouldTransition)
        {
            fsm.PerformTransition(FsmTransitionId.Complete);
        }
    }
    void GenLevel()
    {
        currentLevel++;
        // clear the map and all current objects
        foreach (RLCharacter c in objects) {
            Destroy (c.gameObject);
        }
        objects.Clear ();

        // rebuild the map
        // build the map
        map = new RL.Map (14, 12);

        // build the walls based on the underlying tiles
        for (int x = 0; x < map.sx; x++) {
            for (int y = 0; y < map.sy; y++) {
                /*
                 * assuming there is a sprite in the middle, here are the potential fills
                 * ...
                 * .x.
                 * ...
                 *
                 * ...
                 * xxx
                 * ...
                 *
                 * .x.
                 * .x.
                 * .x.
                 *
                 * oxi // note, need to do two for each, depending on the direction of internal / external
                 * oxx
                 * ooo
                 *
                 * ...
                 * .xx
                 * .x.
                 *
                 * .x.
                 * xx.
                 * ...
                 *
                 * what a pain in the butt. I am not going to f**k with this for right now, because it makes tons of assumptions
                 * about the map structure and room layout and stuff
                */

                Sprite toInsert = tl;
                bool needsInsert = false;
                if (!map.IsOpenTile (x, y)) {
                    toInsert = t;
                    needsInsert = true;
                }
                if (needsInsert) {
                    GameObject s = (GameObject)Instantiate (spriteHolder, new Vector3 (x, y, 0), Quaternion.identity);
                    s.GetComponent<SpriteRenderer> ().sprite = toInsert;
                    s.AddComponent<RLCharacter> ();
                    objects.Add (s.GetComponent<RLCharacter>());
                }
            }
        }

        GameObject playerGO = (GameObject)Instantiate (spriteHolder, new Vector3 (4, 3, 0), Quaternion.identity);
        player = playerGO.AddComponent<RLCharacter> ();
        //		player = playerGO.GetComponent<RLCharacter> ();
        player.GetComponent<SpriteRenderer> ().sprite = playerR;
        player.name = "player";
        objects.Add (player);
        currentFacing = Facing.RIGHT;

        // add a random number of monsters
        int nMonsters = currentLevel;
        for (int i = 0; i < nMonsters; i++) {
            GameObject m = (GameObject)Instantiate (monster, new Vector3 (Random.Range (1, map.sx - 1), Random.Range (1, map.sy - 1)), Quaternion.identity);
            m.GetComponent<RLCharacter> ().AddType (RLCharacter.RLTypes.MONSTER);
            objects.Add (m.GetComponent<RLCharacter> ());
        }

        // add some downward facing stairs
        GameObject stair = (GameObject)Instantiate (spriteHolder, new Vector3 (Random.Range (1, map.sx - 1), Random.Range (1, map.sy - 1)), Quaternion.identity);
        stair.GetComponent<SpriteRenderer> ().sprite = stairsDown;
        stair.AddComponent<RLCharacter> ();
        stair.GetComponent<RLCharacter> ().AddType (RLCharacter.RLTypes.STAIRS_DOWN);
        objects.Add (stair.GetComponent<RLCharacter> ());
    }