// this is called at least once after connectedPorts of any of the inputs or outputs got changed.
 // there is no guarentee on the frequency of these calls.
 public virtual void OnConnectionChange()
 {
     connectedInputCount  = connectedInputs.GetCount();
     firstConnectedInput  = connectedInputs.GetFirst();
     firstConnectedOutput = connectedOutputs.GetFirst();
     connectedOutputCount = connectedOutputs.GetCount();
 }
Exemple #2
0
 public Port(AbstractPlacedItem item, int x, int z, EnumDirection direction) {
     Assert.IsTrue(item != null);
     this.me = item;
     portCenter = new Vector3(x, 0f, z);
     this.direction = direction;
     output = null;
 }
Exemple #3
0
 // this function is no longer used, because we know about shapes now and do not rely on directions that much.
 public static EnumDirection GetDirectionTo(AbstractPlacedItem origin, AbstractPlacedItem destination)
 {
     if (Mathf.RoundToInt(origin.transform.position.x) ==
         Mathf.RoundToInt(destination.transform.position.x))
     {
         if (origin.transform.position.z < destination.transform.position.z)
         {
             return(EnumDirection.North);
         }
         else
         {
             return(EnumDirection.South);
         }
     }
     else
     {
         if (origin.transform.position.x < destination.transform.position.x)
         {
             return(EnumDirection.East);
         }
         else
         {
             return(EnumDirection.West);
         }
     }
 }
 public static bool Buy(AbstractPlacedItem item)
 {
     if (CanAfford(item))
     {
         cash -= item.cost;
         return(true);
     }
     return(false);
 }
    void DoFixedUpdatedAfterCollisionStay()
    {
        var originalLocation = new Location(transform.position);
        var targetPosition   = target.position;

        targetPosition.y = transform.position.y;
        if ((transform.position - targetPosition).magnitude > 1)
        {
            targetPosition   = currentHolder.transform.position;
            targetPosition.y = transform.position.y;
        }
        var destinationPosition = Vector3.MoveTowards(transform.position, targetPosition, speed);

        if (originalLocation != new Location(destinationPosition))
        {
            var targetItem = Grid.GetItem(target);
            if (targetItem != null)
            {
                currentHolderOutputIndex = targetItem.AddProduct(this);
                if (currentHolderOutputIndex < 0)
                {
                    targetItem.RemoveProduct(this);
                    return;
                }
                currentHolder.RemoveProduct(this);
                var machine = targetItem as Machine;
                if (machine != null)
                {
                    if (machine.Consume(this))
                    {
                        Destroy(gameObject);
                    }
                    return;
                }
                currentHolder = targetItem;
            }
        }

        transform.position = destinationPosition;
    }
Exemple #6
0
    public static IEnumerable <Type> GetSurrounding <Type>(AbstractPlacedItem item, bool includeCenterSquare = false)
        where Type : AbstractPlacedItem
    {
        var surroundings = new HashSet <Type>();

        int x, z;

        if (GetCoords(item.transform.position, out x, out z))
        {
            if (includeCenterSquare && GetItem(x, z) == item && item is Type)
            {
                surroundings.Add((Type)item);
            }

            var blocks = item.rotatedBlocks;
            for (int iBlock = 0; iBlock < blocks.Length; ++iBlock)
            {
                int blockX = x + Mathf.RoundToInt(blocks[iBlock].x);
                int blockZ = z + Mathf.RoundToInt(blocks[iBlock].z);

                for (int checkX = blockX - 1; checkX <= blockX + 1; checkX++)
                {
                    for (int checkZ = blockZ - 1; checkZ <= blockZ + 1; checkZ++)
                    {
                        if (checkX != blockX ^ checkZ != blockZ)
                        {
                            var checkItem = GetItem(checkX, checkZ);
                            if (checkItem != null && checkItem != item && checkItem is Type)
                            {
                                surroundings.Add((Type)checkItem);
                            }
                        }
                    }
                }
            }
        }

        return(surroundings);
    }
Exemple #7
0
    public static bool RemoveItem(AbstractPlacedItem itemToRemove)
    {
        int x, z;

        if (GetCoords(itemToRemove.transform.position, out x, out z))
        {
            var blocks = itemToRemove.rotatedBlocks;
            // first, check if all slots are actually belonging to that item,
            for (int i = 0; i < blocks.Length; ++i)
            {
                int blockX = x + Mathf.RoundToInt(blocks[i].x);
                int blockZ = z + Mathf.RoundToInt(blocks[i].z);
                if (items[blockX, blockZ] != itemToRemove)
                {
                    Assert.IsTrue(false);
                    return(false);
                }
            }

            // then clear them, if we are still here.
            for (int i = 0; i < blocks.Length; ++i)
            {
                int blockX = x + Mathf.RoundToInt(blocks[i].x);
                int blockZ = z + Mathf.RoundToInt(blocks[i].z);
                items[blockX, blockZ] = null;
            }
            MoneyManager.Refund(itemToRemove);
            for (int i = 0; i < itemToRemove.products.Count; i++)
            {
                GameObject.Destroy(itemToRemove.products[i].gameObject);
            }
            itemToRemove.products.Clear();
            //Network.RemoveFromWorld(itemToRemove); // reset port connections.
            // MonoBehaviour.print("Grid added " + itemToPlace + " @ " + x + ", " + z);
            return(true);
        }
        return(false);
    }
Exemple #8
0
    public static bool PlaceItem(AbstractPlacedItem itemToPlace)
    {
        int x, z;

        if (GetCoords(itemToPlace.transform.position, out x, out z))
        {
            var blocks = itemToPlace.rotatedBlocks;
            // first, check if all slots are empty
            for (int i = 0; i < blocks.Length; ++i)
            {
                int blockX = x + Mathf.RoundToInt(blocks[i].x);
                int blockZ = z + Mathf.RoundToInt(blocks[i].z);
                if (items[blockX, blockZ] != null)
                {
                    Assert.IsTrue(false);
                    return(false);
                }
            }
            // then set them to this item, if we are still here.
            for (int i = 0; i < blocks.Length; ++i)
            {
                int blockX = x + Mathf.RoundToInt(blocks[i].x);
                int blockZ = z + Mathf.RoundToInt(blocks[i].z);
                items[blockX, blockZ] = itemToPlace;
            }

            // MonoBehaviour.print("Grid added " + itemToPlace + " @ " + x + ", " + z + " + " + String.Join("; ", System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(blocks, b => b.ToString()))));

            //Network.AddToWorld(itemToPlace); // establish port connections

            foreach (var item in Grid.GetSurrounding <AbstractPlacedItem>(itemToPlace))
            {
                item.OnConnectionChange();
            }
            return(true);
        }
        return(false);
    }
    protected void Update()
    {
        if (thingToPlace != null)
        {
            if (Input.GetButtonDown("Rotate"))
            {
                var from = thingToPlace.transform.rotation.eulerAngles;
                thingToPlace.transform.rotation =
                    Quaternion.Euler(from.x, from.y + 90, from.z);
            }
            if (Input.GetMouseButtonDown(1))
            {
                Destroy(thingToPlace.gameObject);
                thingToPlace = null;
            }
        }
        if (Input.GetMouseButtonDown(0))
        {
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                var hit = GetMouseHit();
                if (hit != null)
                {
                    var itemAtLocation = Grid.GetItem(hit.Value.point);
                    if (thingToPlace != null && itemAtLocation == null)
                    {
                        if (MoneyManager.Buy(thingToPlace))
                        {
                            if (MoneyManager.CanAfford(thingToPlace))
                            {
                                var thingToReallyPlace = Instantiate(FloorPlacementController.thingToPlace);
                                Grid.PlaceItem(thingToReallyPlace);
                                thingToReallyPlace.OnConnectionChange();
                                onPlaceSound.Play();
                            }
                            else
                            {
                                Destroy(thingToPlace);
                                thingToPlace = null;
                            }
                        }
                        else
                        {
                            Destroy(thingToPlace);
                            thingToPlace = null;
                        }
                    }
                    else if (thingToPlace == null && itemAtLocation != null)
                    {
                        itemAtLocation.OnClick();
                    }
                    if (itemAtLocation != null)
                    {
                        foreach (var item in Grid.items)
                        {
                            if (item != null)
                            {
                                item.OnConnectionChange();
                            }
                        }
                    }
                }
            }
        }

        if (thingToPlace != null)
        {
            thingToPlace.OnConnectionChange();
        }
    }
 public static void Refund(AbstractPlacedItem item)
 {
     cash += item.cost;
 }
 public static bool CanAfford(AbstractPlacedItem item)
 {
     return(item.cost <= cash);
 }