Example #1
0
    public void setTile(IVector2 v, byte foreground, byte background)
    {
        if (!inBounds(v))
        {
            return;
        }
        setByte(v, FOREGROUND_ID, foreground);
        setByte(v, BACKGROUND_ID, background);
        TileSpec t = TileSpecList.getTileSpec(foreground);
        byte     d = 1;

        if (t != null)
        {
            d = t.durability;
        }
        setByte(v, DURABILITY, d);
        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                IVector2 vi = v + new IVector2(x, y);
                updateTileSpec(vi);
                if (inBounds(vi))
                {
                    makeDirty(vi.x / chunkSize, vi.y / chunkSize);
                }
            }
        }
    }
Example #2
0
    public TileSpec getBackground(IVector2 v)
    {
        TileSpec t = TileSpecList.getTileSpec(getByte(v, BACKGROUND_ID));

        if (t == null)
        {
            return(TileSpecList.getTileSpec(0));
        }
        return(t);
    }
Example #3
0
    void laddercase(IVector2 currentpos, IVector2 dest, ParentedNode node, Vector2 v)
    {
        bool s = map.getForeground(currentpos) == TileSpecList.getTileSpec("Ladder");
        bool f = map.getForeground(currentpos) == TileSpecList.getTileSpec("Ladder");

        if (currentpos.x == dest.x)
        {
            ladderTile(currentpos);
            ladderTile(dest);
        }
        currentMovement.y = Mathf.Sign(v.y) * moveSpeed;
    }
Example #4
0
    void ladderTile(IVector2 t)
    {
        if (!canLadder)
        {
            return;
        }
        currentState = movementState.CLIMBING;
        TileSpec ts = map.getForeground(t);

        if (!ts.solid && ts != TileSpecList.getTileSpec(LADDERINDEX))
        {
            map.setTile(t, (byte)LADDERINDEX, (byte)1);
        }
    }
Example #5
0
    public TileSpec getRenderBackground(IVector2 v)
    {
        switch (renderMode)
        {
        case NAVMESH:
            return(TileSpecList.getTileSpec(0));

        case BACKGROUND:
            return(TileSpecList.getTileSpec(0));

        default:
            return(getBackground(v));
        }
    }
Example #6
0
    public TileSpec getRenderForeground(IVector2 v)
    {
        switch (renderMode)
        {
        case NAVMESH:
            return((!isForegroundSolid(v) && isForegroundSolid(v + Direction.getDirection(Direction.BOTTOM)))? TileSpecList.getTileSpec(0) : TileSpecList.getTileSpec(1));

        case BACKGROUND:
            return(TileSpecList.getTileSpec(0));

        default:
            return(getForeground(v));
        }
    }
Example #7
0
 public byte randomTile(int x, int y)
 {
     while (true)
     {
         int  i = (int)(Random.value * 32);
         byte b = (byte)(Random.value * 6 + 2);
         if (TileSpecList.getTileSpec(b).weight > i)
         {
             if ((int)b <= 3)
             {
                 b = (byte)(y < surfaceHeight?3:2);
             }
             return(b);
         }
     }
 }
Example #8
0
    protected override void mine(IVector2 v)
    {
        byte d = map.getByte(v, Map.DURABILITY);

        if (mineTime <= 0)
        {
            digging = false;
            TileSpec ts = TileSpecList.getTileSpec(map.getByte(v, Map.FOREGROUND_ID));
            map.setTile(v, (byte)spawnedTile, map.getByte(v, Map.BACKGROUND_ID));
            eliminate();
        }
        else
        {
            mineTime -= Time.fixedDeltaTime;
        }
    }
Example #9
0
    public Vector2[] GetLadderNeighbors()
    {
        List <Vector2> neighbors     = new List <Vector2>();
        List <Vector2> goodNeighbors = new List <Vector2>();
        IVector2       pos           = new IVector2(location.x, location.y);

        neighbors.Add(new IVector2(location.x, location.y + 1));

        foreach (IVector2 v in neighbors)
        {
            byte     b = m.getByte(v, Map.FOREGROUND_ID);
            TileSpec t = TileSpecList.getTileSpec(b);
            if (m.unnavigable(v) && m.ladderable(v))
            {
                goodNeighbors.Add(v);
            }
        }

        return(goodNeighbors.ToArray());
    }
Example #10
0
    protected virtual void mine(IVector2 v)
    {
        if (!canMine)
        {
            return;
        }
        byte d = map.getByte(v, Map.DURABILITY);

        if (d == 0)
        {
            digging = false;
            TileSpec ts = TileSpecList.getTileSpec(map.getByte(v, Map.FOREGROUND_ID));
            InventroyManager.instance.addToInventory(ts.resource);
            map.setTile(v, 0, map.getByte(v, Map.BACKGROUND_ID));
        }
        else
        {
            map.setByte(v, Map.DURABILITY, (byte)(d - 1));
            digTimer = digTime;
        }
        // This element type should be determined by the element being mined
    }
Example #11
0
    public Vector2[] GetDigNeighbors()
    {
        List <Vector2> neighbors     = new List <Vector2>();
        List <Vector2> goodNeighbors = new List <Vector2>();
        IVector2       pos           = new IVector2(location.x, location.y);

        neighbors.Add(new IVector2(location.x - 1, location.y));
        neighbors.Add(new IVector2(location.x + 1, location.y));
        neighbors.Add(new IVector2(location.x, location.y - 1));
        neighbors.Add(new IVector2(location.x, location.y + 1));

        foreach (IVector2 v in neighbors)
        {
            byte     b = m.getByte(v, Map.FOREGROUND_ID);
            TileSpec t = TileSpecList.getTileSpec(b);
            if (t.diggable && t.solid)
            {
                goodNeighbors.Add(v);
            }
        }

        return(goodNeighbors.ToArray());
    }
Example #12
0
    //Coroutine which calculates the path to the destination.
    public IEnumerator getPath()
    {
        Vector2 lastDest = position;
        float   waitTime = .1f;

reset:
        while (true)
        {
            //This routine happens every second.
            yield return(new WaitForSeconds(waitTime));

            //If the destination hasn't changed...
            if (lastDest == destination && !rePath)
            {
                pathing  = false;
                waitTime = Mathf.Clamp(waitTime + .01f, .01f, .5f);
                //...Go back to start of the loop.
                goto reset;
            }
            pathing = true;
            rePath  = false;

            if (!TileSpecList.getTileSpec(map.getByte(destination, Map.FOREGROUND_ID)).diggable)
            {
                lastDest = destination;
                path     = new Route();
                goto reset;
            }
            waitTime = .1f;
            Vector2 start = position;
            Vector2 end   = new IVector2(destination.x, destination.y);

            //Create a list of leaves
            List <ParentedNode> leaves = new List <ParentedNode>();

            //Create a list of branches (used leaves)
            List <ParentedNode> branches = new List <ParentedNode>();

            //If the character has to dig
            Route digRoute = new Route();

            //Add current position to the leaves
            leaves.Add(new ParentedNode(null, position, 0));
            int count = 0;

            //While there are still leaves, and the destination hasn't changed.
            while (leaves.Count > 0)
            {
                ParentedNode current = getSmallestLeaf(leaves);

                leaves.Remove(current);
                branches.Add(current);

                //If it found the path...
                if (current.location == end)
                {
                    //...Create a new route based on that last node.
                    //ParentedNode p = new ParentedNode(current.parent,end,0);
                    Route r = new Route(current);
                    setPath(r);
                    if (r.locations.Count > 0)
                    {
                        lastDest = new IVector2(r.locations[r.locations.Count - 1].x, r.locations[r.locations.Count - 1].y);
                    }
                    //Break out
                    goto reset;
                }

                //Add new leaves, both open neighbors and ones where you dig.
                addToLeaves(current, current.GetNeighbors(), branches, leaves, start, end, 0, ParentedNode.Type.WALK);
                if (canMine)
                {
                    addToLeaves(current, current.GetDigNeighbors(), branches, leaves, start, end, 1, ParentedNode.Type.DIG);
                }
                if (canLadder)
                {
                    addToLeaves(current, current.GetLadderNeighbors(), branches, leaves, start, end, 3, ParentedNode.Type.LADDER);
                }

                count++;

                if (rePath)
                {
                    goto reset;
                }
                //Only do 20 cycles per frame
                if (count % pathsPerFrame == 0)
                {
                    yield return(new WaitForSeconds(1));

                    if (count > 10000)
                    {
                        destination = lastDest;
                        goto reset;
                    }
                }
            }

            //If it goes through and cant find anything,
            Debug.Log("Path not found");
            waitTime += .01f;
        }
    }
 public bool onLadder()
 {
     return(map.getForeground(position) == TileSpecList.getTileSpec("Ladder"));
 }
Example #14
0
 public bool unnavigable(IVector2 v)
 {
     return(getForeground(v) != TileSpecList.getTileSpec("Ladder") &&
            (isForegroundSolid(v) || !isForegroundSolid(v + Direction.getDirection(Direction.BOTTOM))));
 }
Example #15
0
 public bool ladderable(IVector2 v)
 {
     return(!isForegroundSolid(v) && getBackground(v) != TileSpecList.getTileSpec(0));
 }
Example #16
0
    //****************************************************************************//

    void walkcase(IVector2 currentpos, IVector2 dest, ParentedNode node, Vector2 v)
    {
        if ((v.y > .25f && Mathf.Abs(v.x) < .1f) || (map.getForeground(currentpos) == TileSpecList.getTileSpec(LADDERINDEX) && canLadder))
        {
            laddercase(currentpos, dest, node, v);
            return;
        }
        currentState = movementState.WALKING;
        if (v.y > .25f && (Mathf.Abs(v.x) > .1f && cc.velocity.x == 0))        // || (lastPosition - currentpos).magnitude == 0){
        {
            Debug.Log("Jumping");
            jump();
            currentState = movementState.JUMPING;
        }
    }
Example #17
0
    // Update is called once per frame
    private void FixedUpdate()
    {
        base.FixedUpdate();
        if (pPhysics.onWall)
        {
            targetSpeed  = 0;
            currentSpeed = 0;
        }
        targetSpeed = speed * Input.GetAxisRaw("Horizontal");

        float climb = Input.GetAxisRaw("Vertical");

        currentSpeed = IncrementSpeed(currentSpeed, targetSpeed, acceleration);

        moveAmount.x = currentSpeed;

        if (pPhysics.climbing)
        {
            moveAmount.y = climb;
            if (!pPhysics.onLadder())             //|| Mathf.Abs(currentSpeed) > .25){
            {
                pPhysics.ungrab();
                if (Input.GetAxisRaw("Jump") > .5)
                {
                    moveAmount.y = jumpHeight;
                }
            }
        }
        else if (pPhysics.onGround)
        {
            moveAmount.y = 0;
            if (Input.GetAxisRaw("Jump") > .5)
            {
                moveAmount.y = jumpHeight;
            }
        }
        else
        {
            moveAmount.y -= gravity * Time.fixedDeltaTime;
            //Debug.Log (moveAmount);
            if (moveAmount.y <= -fallDeathSpeed)
            {
                Debug.Log(moveAmount);
                reset();
            }
        }

        if (Mathf.Abs(climb) > .1f)
        {
            pPhysics.grab();
        }

        pPhysics.move(moveAmount * Time.fixedDeltaTime);
        currentTile = map.getForeground((Vector2)this.transform.position);
        if (currentTile == TileSpecList.getTileSpec("Die"))
        {
            reset();
        }

        if (Input.GetKey(KeyCode.Home))
        {
            reset();
        }
    }