public void SetState(ConveyorSwitchComponent conveyorSwitch) { var state = conveyorSwitch.State; if (state == ConveyorState.Loose) { if (_switches.Count > 0) { return; } state = ConveyorState.Off; } State = state; foreach (var conveyor in Conveyors) { conveyor.Sync(this); } foreach (var connectedSwitch in _switches) { connectedSwitch.Sync(this); } }
public void OnPlaced(int pMoneyCost, int pIronCost) { placed = true; state = ConveyorState.Idle; moneyCost = pMoneyCost; ironCost = pIronCost; }
private void UpdateState(int position, ConveyorState state, bool isLoaderFree, bool isMechanicFree) { var conveyorVm = Conveyors[position]; conveyorVm.State = state; switch (state) { case ConveyorState.Working: if (isLoaderFree) { LoaderX = DefaultPos; LoaderY = DefaultPos; } if (isMechanicFree) { MechanicX = DefaultPos; MechanicY = DefaultPos; } break; case ConveyorState.InRepairing: MechanicX = conveyorVm.X; MechanicY = conveyorVm.Y + 150; break; case ConveyorState.AddMaterials: LoaderX = conveyorVm.X; LoaderY = conveyorVm.Y + 150; break; } Invalidate(); }
public ConveyorState PauseResume() { switch (ConveyorState) { case ConveyorState.Running: case ConveyorState.Resumed: { Pause(); ConveyorState = ConveyorState.Paused; return(ConveyorState); } case ConveyorState.Paused: { Resume(); ConveyorState = ConveyorState.Resumed; return(ConveyorState); } case ConveyorState.NotStarted: case ConveyorState.Stopped: default: return(ConveyorState); } }
private void Start() { var setState = currentState; currentState = currentState + 1; SetState(setState); }
public void SetState(ConveyorState state) { if (conveyorState == state) { return; } DisableAll(); switch (state) { case ConveyorState.Working: greenLamp.Enable(); break; case ConveyorState.Waiting: blueLamp.Enable(); break; case ConveyorState.Stopped: redLamp.SetSimpleMode(); redLamp.Enable(); break; case ConveyorState.TimeRunsOut: redLamp.SetBlinkMode(); redLamp.Enable(); break; } conveyorState = state; }
private void Start() { var setState = conveyorState; conveyorState = conveyorState + 1; SetState(setState); }
public void Stop() { foreach (var block in _blocks.Values) { block.Stop(); } ConveyorState = ConveyorState.Stopped; }
public void Run() { foreach (var block in _blocks.Values) { ThreadPool.QueueUserWorkItem(block.Run); } ConveyorState = ConveyorState.Running; }
public void link(Item item, int quantity, Vector2 output) { ConveyorInv.addItem(item, quantity); // set the conveyor to "Busy" so it doesnt suck in items until it has finished moving the items it already has state = ConveyorState.Inputting; inputDirections.Add(new Vector3(output.x, output.y)); sprites.Add(item.icon); timeElapsed = 0; firstframe = true; }
private void SetState(ConveyorComponent component, ConveyorState state) { component.State = state; if (TryComp <RecyclerComponent>(component.Owner, out var recycler)) { if (component.State != ConveyorState.Off) { _recycler.EnableRecycler(recycler); } else { _recycler.DisableRecycler(recycler); } } UpdateAppearance(component); }
public void SetState(ConveyorState state) { if (currentState == state) { return; } lampsPanel.SetState(state); if (state == ConveyorState.Working) { conveyorBelt.Run(); conveyorDoor.Open(); } else if (state == ConveyorState.Waiting || state == ConveyorState.Stopped) { conveyorBelt.Stop(); conveyorDoor.Close(); } currentState = state; }
private void HandleUpdate(Conveyor caller, ConveyorState state) { var pos = Conveyors.FindIndex(conveyor => conveyor == caller); UpdateView?.Invoke(pos, state, Loader.IsFree, Mechanic.IsFree); }
private void ItemTransfer() { ////////////////////////////////////////// /// IDLE ////////////////////////////////////////// if (state == ConveyorState.Idle) { foreach (GameObject s in spriteObjects) // { Destroy(s); // reset all the sprites ready for the next IO cycle } spriteObjects.Clear(); // inputDirections.Clear(); for (int i = 0; i < TrueArmTypes.Length; i++) { if (TrueArmTypes[i] == IOController.Input) { Tile t = Tile.Vector3ToTile(transform.position); GameObject tower = Tile.tileMap[t.x + (int)cardinalDirections[i].x, t.y + (int)cardinalDirections[i].y].GetTower(); if (tower.TryGetComponent <Mine>(out Mine m)) { if (m.inv.items.Count > 0) { // get the item and the quantity we want the conveyor to take Item item = m.inv.items[0]; int quantity = m.inv.quantity[0]; //remove the item from the towers inventory and add it to the conveyors m.inv.removeItem(item, quantity); ConveyorInv.addItem(item, quantity); // set the conveyor to "Busy" so it doesnt suck in items until it has finished moving the items it already has state = ConveyorState.Inputting; inputDirections.Add(new Vector3(-cardinalDirections[i].x, -cardinalDirections[i].y)); sprites.Add(item.icon); timeElapsed = 0; firstframe = true; } } } } } ////////////////////////////////////////// /// INPUT_CYCLE ////////////////////////////////////////// else if (state == ConveyorState.Inputting) { // onfirst frame // for each input // create a sprite // after first frame // calculate the percentage (timeElapsed/timeToTransport) // set the position of sprite to inputDirection * (1-percentage) // when percentage >= 100 // delete the sprite // move on to Looking For Output timeElapsed += Time.deltaTime; if (firstframe) { firstframe = false; for (int i = 0; i < inputDirections.Count; i++) { GameObject spriteObject = Instantiate(conveyorSprite, this.gameObject.transform); spriteObject.GetComponent <SpriteRenderer>().sprite = sprites[i]; spriteObject.transform.localPosition = new Vector3(inputDirections[i].x, 0, inputDirections[i].y); spriteObjects.Add(spriteObject); } } else if (timeElapsed > 0 && timeElapsed < timeToTransport) { for (int i = 0; i < inputDirections.Count; i++) { GameObject spriteObject = spriteObjects[i]; float percent = timeElapsed / timeToTransport; spriteObject.transform.localPosition = new Vector3(inputDirections[i].x, 0, inputDirections[i].y) * (1 - percent); spriteObject.transform.localPosition += Vector3.up; } } else if (timeElapsed >= timeToTransport) { state = ConveyorState.LookingForOutput; timeElapsed = 0; inputDirections.Clear(); // now that the inputs are complete, delete the input directions ready for the next inputs. for (int i = 0; i < TrueArmTypes.Length; i++) { if (TrueArmTypes[i] == IOController.Output || TrueArmTypes[i] == IOController.OutputToTower) { outputDirections.Add(cardinalDirections[i]); } } } } else if (state == ConveyorState.LookingForOutput) { outputDirections.Clear(); // for (int i = 0; i < TrueArmTypes.Length; i++) // { // constantly check for new outputs, otherwise when items stop, they will never start again if (TrueArmTypes[i] == IOController.Output || TrueArmTypes[i] == IOController.OutputToTower) { outputDirections.Add(cardinalDirections[i]); } } // Are we ready to output? bool readyForOutput = true; // by default we are ready for output. foreach (Vector2 direction in outputDirections) // then we loop through all the neighboring conveyors and if they are Not Idle (not ready to recieve an item) { // then we set readyForOutput to false and chech again on the next frame TODO: use a timer instead of checking every frame Tile t = Tile.Vector3ToTile(this.transform.position + new Vector3(-direction.x, 0, -direction.y)); if (t.GetTower().TryGetComponent <ConveyorManager>(out ConveyorManager conv)) { if (!(conv.state == ConveyorState.Idle)) { readyForOutput = false; } } } // when we are ready to output, proceed if (readyForOutput && outputDirections.Count > 0) { state = ConveyorState.Outputting; firstframe = true; // destroy all the sprites and clear the list foreach (GameObject spriteObject in spriteObjects) { Destroy(spriteObject); } spriteObjects.Clear(); } } ////////////////////////////////////////// /// OUTPUT_CYCLE ////////////////////////////////////////// else if (state == ConveyorState.Outputting) { // onfirst frame // for each output // create an inventory for each output direction // if an item is allowed (by item filters) to go in a direction, it will be added to the inventory headed in that direction // if an item is NOT allowed a certain direction, it will not be added to that output Inventory, and therefore will not go that direction // create a sprite // after first frame // calculate the percentage (timeElapsed/timeToTransport) // set the position of sprite to outputDirection * percentage // when percentage >= 100 // delete the sprite // move on to Looking For Output timeElapsed += Time.deltaTime; if (firstframe) { firstframe = false; // reset inventories for each output outputInventories.Clear(); for (int k = 0; k < outputDirections.Count; k++) { // in order to avoid errors spriteObjects.Add(new GameObject()); // have an empty inventory for each output - the absence of an inventory will cause errors outputInventories.Add(new Inventory()); // } List <Vector2> itemOutputDirections = new List <Vector2>(); int numOfItemsDirections = 0; for (int j = 0; j < ConveyorInv.items.Count; j++) { Item item = ConveyorInv.items[j]; for (int i = 0; i < outputDirections.Count; i++) { Vector2 outputDirection = outputDirections[i]; // if the item is in the item filter OR if the item filter is empty // add it to the itemOutputDirections (therefore allowing the item to go that direction) // NOTE: empty item filters will accept all items int outputDirectionCardinalIndex = GetCardinalDirection(outputDirection); if (itemFilters[outputDirectionCardinalIndex].Contains(item) || itemFilters[outputDirectionCardinalIndex].Count == 0) { numOfItemsDirections++; itemOutputDirections.Add(outputDirection); } } for (int k = 0; k < itemOutputDirections.Count; k++) { // if an item is meant to travel in a particular direction int temp = outputDirections.IndexOf(itemOutputDirections[k]); // it *OVERWRITES* the before code outputInventories[temp].addItem(item, ConveyorInv.quantity[j] / numOfItemsDirections); // therefore every output will have an inventory } // and output with zero items, will be an inventory of length 0 } // // the above creates an inventory for each output that respects item filters. // these inventories are then used to pass items to the next tower // also used to change the sprite of items scrolling across the conveyor for (int i = 0; i < outputDirections.Count; i++) { // if the output inventory contains an item // show that item scrolling across the conveyor if (outputInventories[i].items.Count > 0) { GameObject spriteObject = Instantiate(conveyorSprite, this.gameObject.transform); spriteObject.GetComponent <SpriteRenderer>().sprite = outputInventories[i].items[0].icon; // TODO: loop through all elements, instead of just taking the first Destroy(spriteObjects[i]); // ( show all items scrolling across the conveyor, not just the one in slot 0) spriteObjects[i] = spriteObject; } } } // move items across the conveyor in the appropriate direction else if (timeElapsed > 0 && timeElapsed < timeToTransport) { for (int i = 0; i < outputDirections.Count; i++) { if (outputInventories[i].items.Count > 0) { GameObject spriteObject = spriteObjects[i]; float percent = timeElapsed / timeToTransport; spriteObject.transform.localPosition = new Vector3(-outputDirections[i].x, 0, -outputDirections[i].y) * percent; spriteObject.transform.localPosition += Vector3.up; } } } else if (timeElapsed >= timeToTransport) { state = ConveyorState.Idle; for (int i = 0; i < outputDirections.Count; i++) { if (outputInventories[i].items.Count > 0) { GameObject spriteObject = spriteObjects[0]; Tile t = Tile.Vector3ToTile(this.transform.position + new Vector3(-outputDirections[i].x, 0, -outputDirections[i].y)); if (t.GetTower()) { // if connected to a conveyor // "Link" the conveyor so that inputs and outputs match up if (t.GetTower().TryGetComponent <ConveyorManager>(out ConveyorManager conv)) { for (int j = 0; j < outputInventories[i].items.Count;) { conv.link(outputInventories[i].items[j], outputInventories[i].quantity[j], outputDirections[i]); outputInventories[i].items.RemoveAt(j); outputInventories[i].quantity.RemoveAt(j); } spriteObjects.Remove(spriteObject); Destroy(spriteObject); } // if connected to a tower // output the conveyors inventory into the towers if (t.GetTower().TryGetComponent(out Tower tower)) { // for each item in the conveyors inventory // add that item to the towers inventory for (int j = 0; j < outputInventories[i].items.Count; j++) { tower.inv.addItem(outputInventories[i].items[j], outputInventories[i].quantity[j]); outputInventories[i].items.RemoveAt(j); outputInventories[i].quantity.RemoveAt(j); } } } } } // last thing before looping round the cycles sprites.Clear(); ConveyorInv.items.Clear(); ConveyorInv.quantity.Clear(); } } }