예제 #1
0
        public bool IsTouchingGround(Vector3 pos, out RaycastResults res)
        {
            int iters = 20;

            res = null;
            for (int i = -1; i < iters; i++)
            {
                float   p         = i / (iters - 1.0f);
                float   xDiff     = Mathf.Sin(p * 2 * Mathf.PI);
                float   yDiff     = Mathf.Cos(p * 2 * Mathf.PI);
                Vector3 curOffset = (new Vector3(xDiff, 0, yDiff)).normalized * feetWidth;
                if (i == -1) // start with no offset (-1 is just a dumb hack to do this)
                {
                    curOffset = new Vector3(0, 0, 0);
                }

                if (PhysicsUtils.RayCast(pos + curOffset, new Vector3(0, -1, 0), heightBelowHead + footDepth, out res))
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #2
0
        public void UpdatePathing()
        {
            MovingEntity body = GetComponent <MovingEntity>();

            if (pathingTarget == null)
            {
                body.desiredMove = Vector3.zero;
            }
            if (PhysicsUtils.millis() - lastPathfind > 1000.0 / pathfindsPerSecond)
            {
                LVector3       myPos           = LVector3.FromUnityVector3(transform.position);
                RaycastResults blockStandingOn = body.BlockStandingOn();
                // if we are using shift and standing over an empty block, but our feet are on a neighboring block, use that neighboring block for pathfinding instead
                if (blockStandingOn != null)
                {
                    myPos = blockStandingOn.hitBlock + new LVector3(0, blocksHeight, 0);
                }

                ////// new stuff
                // find position on ground below player and path to that instead
                LVector3 playerPos    = LVector3.FromUnityVector3(pathingTarget.transform.position);
                int      goDownAmount = 10; // don't get into infinite loop, give up eventually
                while (playerPos.BlockV == BlockValue.Air && goDownAmount > 0)
                {
                    playerPos = playerPos - new LVector3(0, 1, 0);
                    goDownAmount--;
                }
                playerPos += new LVector3(0, 2, 0);
                //////


                bool iShouldJump = true;


                //// new stuff

                /*
                 * bool pathingSuccess = false;
                 * PathingSpreadNode resPath = BlocksPathing.Pathfind(World.mainWorld, myPos, playerPos, 1, 1, blocksHeight, 1, out pathingSuccess);
                 * iShouldJump = true;
                 * if (resPath != null)
                 * {
                 *  curPath = (new PathingResult(resPath)).GetPathNode();
                 * }
                 */
                /////

                //PhysicsUtils.Pathfind(blocksHeight, ref curPath, out iShouldJump, myPos, playerPos, 200);

                if (iShouldJump)
                {
                    body.jumping = true;
                }
                lastPathfind = PhysicsUtils.millis();
            }

            body.desiredMove = Vector3.zero;
            Vector3 targetPos = transform.position;

            if (curPath != null)
            {
                LVector3 myPos           = LVector3.FromUnityVector3(transform.position);
                LVector3 myPosBeforeJump = myPos - new LVector3(0, 1, 0);
                if (curPath.prevNode != null && (myPos == curPath.prevNode.pos || myPosBeforeJump == curPath.prevNode.pos))
                {
                }
                else if (curPath.nextNode != null && (myPos == curPath.nextNode.pos || myPosBeforeJump == curPath.nextNode.pos))
                {
                    curPath = curPath.nextNode;
                }
                else if (myPos == curPath.pos || myPosBeforeJump == curPath.pos)
                {
                    if (curPath.nextNode != null)
                    {
                        curPath = curPath.nextNode;
                    }
                }

                LVector3 targetBlock = curPath.pos;
                if (targetBlock.y == LVector3.FromUnityVector3(transform.position).y)
                {
                    body.usingShift = true;
                }
                else
                {
                    body.usingShift = false;
                }
                if (targetBlock.y > myPos.y)
                {
                    body.jumping = true;
                }
                else
                {
                    body.jumping = false;
                }
                targetPos = targetBlock.BlockCentertoUnityVector3();
                body.SetAbsoluteDesiredMove((targetPos - transform.position).normalized);
            }
        }
예제 #3
0
        public void UpdateOld()
        {
            MovingEntity body = GetComponent <MovingEntity>();

            if (PhysicsUtils.millis() - lastPathfind > 1000.0 / pathfindsPerSecond)
            {
                LVector3       myPos           = LVector3.FromUnityVector3(transform.position);
                RaycastResults blockStandingOn = body.BlockStandingOn();
                // if we are using shift and standing over an empty block, but our feet are on a neighboring block, use that neighboring block for pathfinding instead
                if (blockStandingOn != null)
                {
                    myPos = blockStandingOn.hitBlock + new LVector3(0, blocksHeight, 0);
                }
                LVector3 playerPos = LVector3.FromUnityVector3(FindObjectOfType <BlocksPlayer>().transform.position);
                bool     iShouldJump;
                PhysicsUtils.Pathfind(blocksHeight, ref curPath, out iShouldJump, myPos, playerPos, 200);

                if (iShouldJump)
                {
                    body.jumping = true;
                }
                lastPathfind = PhysicsUtils.millis();
            }

            body.desiredMove = Vector3.zero;
            Vector3 targetPos = transform.position;

            if (curPath != null)
            {
                LVector3 myPos           = LVector3.FromUnityVector3(transform.position);
                LVector3 myPosBeforeJump = myPos - new LVector3(0, 1, 0);
                if (curPath.prevNode != null && (myPos == curPath.prevNode.pos || myPosBeforeJump == curPath.prevNode.pos))
                {
                }
                else if (curPath.nextNode != null && (myPos == curPath.nextNode.pos || myPosBeforeJump == curPath.nextNode.pos))
                {
                    curPath = curPath.nextNode;
                }
                else if (myPos == curPath.pos || myPosBeforeJump == curPath.pos)
                {
                    if (curPath.nextNode != null)
                    {
                        curPath = curPath.nextNode;
                    }
                }
                else if (myPos != curPath.pos && myPosBeforeJump != curPath.pos && curPath.prevNode != null)
                {
                    //curPath = curPath.prevNode;
                }

                /*
                 * while (myPos != curPath.pos && myPosBeforeJump != curPath.pos)
                 * {
                 *  if (curPath.nextNode == null)
                 *  {
                 *      break;
                 *  }
                 *  else
                 *  {
                 *      curPath = curPath.nextNode;
                 *  }
                 * }
                 */

                //if (myPos == curPath.pos || myPosBeforeJump == curPath.pos)
                // {
                LVector3 targetBlock = curPath.pos;
                if (targetBlock.y == LVector3.FromUnityVector3(transform.position).y)
                {
                    body.usingShift = true;
                }
                else
                {
                    body.usingShift = false;
                }
                if (curPath.nextNode != null)
                {
                    //targetBlock = curPath.nextNode.pos;
                }
                if (targetBlock.y > myPos.y)
                {
                    body.jumping = true;
                }
                else
                {
                    body.jumping = false;
                }
                targetPos = targetBlock.BlockCentertoUnityVector3();
                body.SetAbsoluteDesiredMove((targetPos - transform.position).normalized);
                //}
                //else
                //{
                // }
            }

            /*
             * if (curPath != null && curPath.nextNode != null)
             * {
             *  //PathNode next = curPath;
             *  //while (Vector3.Distance(next.pos.BlockCentertoUnityVector3(), transform.position) < 1.0f && next.nextNode != null)
             *  //{
             *  //    next = next.nextNode;
             *  //}
             *  targetPos = curPath.nextNode.pos.BlockCentertoUnityVector3();
             *  //LVector3 playerPos = LVector3.FromUnityVector3(FindObjectOfType<BlocksPlayer>().transform.position);
             *  //Debug.Log("going to " + curPath.nextNode.pos.BlockCentertoUnityVector3() + " player position is " + FindObjectOfType<BlocksPlayer>().transform.position + " player block pos is " + playerPos);
             *  //body.SetAbsoluteDesiredMove((curPath.nextNode.pos.BlockCentertoUnityVector3() - transform.position));
             *  //body.jumping = true;
             * }
             * else if (curPath != null)
             * {
             *  //Debug.Log("going to " + curPath.nextNode.pos.BlockCentertoUnityVector3() + " player position is " + FindObjectOfType<BlocksPlayer>().transform.position + " player block pos is " + playerPos);
             *  // body.SetAbsoluteDesiredMove((curPath.pos.BlockCentertoUnityVector3() - transform.position));
             *  targetPos = curPath.pos.BlockCentertoUnityVector3();
             *  //body.jumping = true;
             * }
             * LVector3 targetBlock = LVector3.FromUnityVector3(targetPos);
             * LVector3 myBlock = LVector3.FromUnityVector3(transform.position - new Vector3(0, body.heightBelowHead / 2.0f, 0));
             * if (targetBlock.y > myBlock.y)
             * {
             *  body.jumping = true;
             * }
             * else
             * {
             *  body.jumping = false;
             * }
             * body.SetAbsoluteDesiredMove((targetPos - transform.position)+Random.insideUnitSphere*0.1f);
             */
            /*
             * Vector3 curOff = Random.insideUnitCircle;
             * offset += curOff * 0.1f;
             * if (offset.magnitude > 1.0f)
             * {
             *  offset = offset.normalized * 1.0f;
             * }
             * if (Random.value < 0.01f)
             * {
             *  body.jumping = true;
             * }
             * else
             * {
             *  body.jumping = false;
             * }
             * body.desiredMove = offset;
             */
        }
예제 #4
0
        // Update is called once per frame
        void Update()
        {
            if (movingEntity == null)
            {
                movingEntity = GetComponent <MovingEntity>();
            }
            if (playerPulling != null)
            {
                transform.position += movingEntity.GetVel() * Time.deltaTime + movingEntity.desiredMove * Time.deltaTime;
                //movingEntity.SetVel(movingEntity.GetVel() * 0.99f);
            }


            timeSinceSpawned += Time.deltaTime;

            if (blockName != "")
            {
                try
                {
                    blockId = BlockUtils.StringToBlockId(blockName);
                    if (blockStack != null)
                    {
                        blockStack.block = blockId;
                    }
                    blockName = "";
                }
                catch
                {
                }
            }

            if (movingEntity != null && movingEntity.IsTouchingGround())
            {
                movingEntity.desiredMove *= 0.9f;
            }

            if (playerThrowing != null)
            {
                movingEntity.speed = 5.0f;
                if (movingEntity.IsTouchingGround())
                {
                    //movingEntity.desiredMove *= 0.9f;
                }
                if (Time.time - timeThrown > 3.3f)
                {
                    playerThrowing = null;
                }
            }
            if (blockStack == null)
            {
                blockStack = new BlockStack(blockId, 1);
            }
            if (blockStack != null)
            {
                if (blockStack.count <= 0)
                {
                    blockStack = null;
                }
                else
                {
                    blockId = blockStack.block;
                    if (transform.GetComponentInChildren <UnityEngine.UI.Text>() != null)
                    {
                        if (blockStack.count > 1)
                        {
                            transform.GetComponentInChildren <UnityEngine.UI.Text>().text = blockStack.count + "";
                        }
                        else
                        {
                            transform.GetComponentInChildren <UnityEngine.UI.Text>().text = "";
                        }
                    }
                }
            }

            if (playerPulling != null || !pullable)
            {
                if (movingEntity != null)
                {
                    movingEntity.enabled = false;
                }
            }
            if (playerPulling == null && pullable)
            {
                if (movingEntity != null)
                {
                    movingEntity.enabled = true;
                }
            }


            if (playerPulling == null && movingEntity != null && movingEntity.enabled)
            {
                if (movingEntity.IsTouchingGround() && informStuffBelow.Do())
                {
                    RaycastResults blockStandingOn = movingEntity.BlockStandingOn();
                    //Debug.Log("checking below entity, hit block " + BlockValue.IdToBlockName(blockStandingOn.hitBlock.Block));

                    Blocks.BlockOrItem customBlock;
                    if (World.mainWorld.customBlocks.ContainsKey(blockStandingOn.hitBlock.Block, out customBlock))
                    {
                        //Debug.Log("calling block stack above on it");
                        using (BlockData hitBlockData = World.mainWorld.GetBlockData(blockStandingOn.hitBlock))
                        {
                            BlockStack myStack = Stack;
                            BlockStack resStack;
                            customBlock.BlockStackAbove(hitBlockData, myStack, transform.position, out resStack);

                            if (resStack == null || resStack.count <= 0 || resStack.block == BlockValue.Air)
                            {
                                Destroy(transform.gameObject);
                            }
                            else
                            {
                                Stack = resStack;
                            }
                        }
                    }
                }
            }
            if (GetComponent <UnityEngine.UI.Outline>() != null)
            {
                GetComponent <UnityEngine.UI.Outline>().enabled = selected;
            }

            if (GetComponentInChildren <DurabilityBar>() != null)
            {
                DurabilityBar bar = GetComponentInChildren <DurabilityBar>();
                bar.enabled = blockStack != null && blockStack.maxDurability != 0;
                if (bar.enabled)
                {
                    bar.durability = blockStack.durability / (float)blockStack.maxDurability;
                }
            }

            UpdateTexture();

            if (pullable)
            {
                transform.rotation *= Quaternion.Euler(0, 12.0f * Time.deltaTime, 0);
            }
        }
예제 #5
0
        public void Update()
        {
            this.typeOfThingDoing = this.thingDoing.typeOfThing;
            if (world == null)
            {
                world = World.mainWorld;

                if (world == null) // not initialized yet, wait
                {
                    return;
                }
            }

            if (updateAction.Do())
            {
                //Debug.Log("updating action");
                float totalWeight = 0.0f;
                for (int i = 0; i < actionWeights.Length; i++)
                {
                    actionWeights[i] = actionBaseWeights[i] + GetWeight((TypeOfThingDoing)i);
                    if (isImportant[i])
                    {
                        actionWeights[i] = zeroOneThing(actionWeights[i], importantMaxValues[i]);
                    }
                    totalWeight += actionWeights[i];
                }
                if (totalWeight <= 0.0f)
                {
                    totalWeight = 1.0f;
                }

                // randomly sample proportional to weights
                float randVal     = Random.value;
                float cumulSum    = 0.0f;
                int   chosenThing = 0;
                for (int i = 0; i < (int)TypeOfThingDoing.MaxValue; i++)
                {
                    cumulSum += actionWeights[i] / totalWeight;
                    if (randVal <= cumulSum)
                    {
                        chosenThing = i;
                        break;
                    }
                }

                int   curAct       = (int)thingDoing.typeOfThing;
                float curWeight    = actionWeights[curAct];
                float chosenWeight = actionWeights[chosenThing];

                float diff = curWeight - chosenWeight;
                // if current is much more likely, don't change
                if (diff > 1.0f)
                {
                }
                // otherwise, change with diff pr (if diff < 0 this means always change)
                else/* if (diff < 0)
                     * {
                     * TypeOfThingDoing chosenThingDoing = (TypeOfThingDoing)chosenThing;
                     * thingDoing = UpdateBehavior(chosenThingDoing);
                     * if (thingDoing.typeOfThing == TypeOfThingDoing.GettingFood)
                     * {
                     *  OnSearchForFood(out lookingForBlock, out blockLookingFor);
                     * }
                     * }
                     * else if (Random.value < diff)*/
                {
                    TypeOfThingDoing chosenThingDoing = (TypeOfThingDoing)chosenThing;
                    ThingDoing       prevThingDoing   = thingDoing;
                    thingDoing = UpdateBehavior(chosenThingDoing);
                    if (thingDoing != prevThingDoing)
                    {
                        iNeedToPathfindAgain = true;
                    }
                    if (thingDoing.typeOfThing == TypeOfThingDoing.GettingFood)
                    {
                        OnSearchForFood(out lookingForBlock, out blockLookingFor);
                    }
                }
            }


            if (thingDoing.typeOfThing == TypeOfThingDoing.GettingFood || thingDoing.typeOfThing == TypeOfThingDoing.RunningAway || thingDoing.typeOfThing == TypeOfThingDoing.Gathering || thingDoing.typeOfThing == TypeOfThingDoing.GoingTo)
            {
                UpdatePathing();
            }
            else if (thingDoing.typeOfThing == TypeOfThingDoing.Socializing)
            {
                thingDoing.typeOfThing = TypeOfThingDoing.Wandering;
            }
            else if (thingDoing.typeOfThing == TypeOfThingDoing.Standing)
            {
                GetComponent <MovingEntity>().desiredMove = Vector3.zero;
            }
            else if (thingDoing.typeOfThing == TypeOfThingDoing.Wandering)
            {
                if (updateWander.Do())
                {
                    //int randVal = Random.Range(0, 5);
                    Vector3[]    options     = new Vector3[] { Vector3.zero, Vector3.forward, Vector3.right, Vector3.left, Vector3.back };
                    Vector3      desiredMove = options[Random.Range(0, options.Length)];
                    MovingEntity me          = GetComponent <MovingEntity>();
                    me.desiredMove = Vector3.zero;
                    me.jumping     = false;
                    RaycastResults blockStandingOn = me.BlockStandingOn();
                    if (blockStandingOn != null && desiredMove != Vector3.zero)
                    {
                        LVector3 myPos        = blockStandingOn.hitBlock + new LVector3(0, 1, 0);
                        LVector3 nextPos      = myPos + new LVector3((long)desiredMove.x, (long)desiredMove.y, (long)desiredMove.z);
                        LVector3 belowNextPos = nextPos + new LVector3(0, -1, 0);
                        LVector3 aboveNextPos = nextPos + new LVector3(0, 1, 0);

                        bool needsToJump  = false;
                        bool okayToMove   = false;
                        bool needsToShift = false;
                        // we need to hop up
                        if (nextPos.Block != Example.Air)
                        {
                            // we can hop up
                            if (aboveNextPos.Block == Example.Air)
                            {
                                okayToMove  = true;
                                needsToJump = true;
                            }
                            // we can't hop up
                            else
                            {
                            }
                        }
                        // we won't run into anything and can walk there
                        else
                        {
                            // going down
                            if (belowNextPos.Block == Example.Air)
                            {
                                LVector3 belowBelowNextPos = belowNextPos + new LVector3(0, -1, 0);
                                // too far to fall, don't do it
                                if (belowBelowNextPos.Block == Example.Air)
                                {
                                }
                                // down stairs, that's fine
                                else
                                {
                                    okayToMove = true;
                                }
                            }
                            // just horizontal, that's fine
                            else
                            {
                                okayToMove = true;
                            }
                        }


                        me.jumping    = needsToJump;
                        me.usingShift = needsToShift;
                        if (okayToMove)
                        {
                            me.desiredMove = desiredMove;
                        }
                    }
                }
            }
        }
예제 #6
0
        public bool RayCastWithWidth(Vector3 origin, Vector3 direction, float width, float maxMag, out RaycastResults hitResults)
        {
            int iters = 20;

            hitResults = null;
            for (int i = -1; i < iters; i++)
            {
                float   p         = i / (iters - 1.0f);
                float   xDiff     = Mathf.Sin(p * 2 * Mathf.PI);
                float   yDiff     = Mathf.Cos(p * 2 * Mathf.PI);
                Vector3 curOffset = (new Vector3(xDiff, 0, yDiff)).normalized * width;
                if (i == -1) // start with no offset (-1 is just a dumb hack to do this)
                {
                    curOffset = new Vector3(0, 0, 0);
                }

                if (PhysicsUtils.RayCast(origin + curOffset, direction, maxMag, out hitResults))
                {
                    hitResults.hitPos -= curOffset;
                    return(true);
                }
            }
            return(false);
        }