void Interact() { Collider2D hitComponent = Physics2D.OverlapCircle(transform.position, interactRadius, shipComponentsMasks); if (hitComponent == null) { return; } ShipComponent shipComponent = hitComponent.GetComponent <ShipComponent>(); if (shipComponent == null) { Debug.LogWarning($"Player interacted with object {hitComponent.name} but it did not have a ShipComponent attached to it."); return; } shipComponent.Interact(); BreakableComponent breakable = shipComponent.GetComponent <BreakableComponent>(); if (breakable) { progressBar.gameObject.SetActive(true); } focus = shipComponent; }
private static void InspectBreakableComponent(BreakableComponent component) { InspectReportComponentBase(component); Console.WriteLine($"CanBreak: {component.CanBreak}"); Console.WriteLine($"BreakTo: {component.BreakTo}"); }
public void Interact() { _previousState = _currentState; _currentState = Mouse.GetState(); if (_focus != null) { float distance = Vector2.Distance(_position, _focus.GetPosition()); if (distance > interactRad) { // If player is too far from the Interactable Object (ie. ShipComponent) // then cancel current action and reset focus to a null value to prevent it from being interacted again from a distance _focus.Cancel(); _focus = null; _fixProgress.SetActive(false); return; } BreakableComponent b = _focus as BreakableComponent; if (_currentState.LeftButton == ButtonState.Released && _previousState.LeftButton == ButtonState.Pressed) { if (b != null) { if (b.IsBroken()) { Item item = _inventory.items.Find(i => i.itemType == b.GetRequiredItem()); if (item == null) { return; } _fixProgress.SetActive(true); } } _focus.Interact(); } if (b != null) { _fixProgress.UpdateProgress(b.GetFixProgress()); } } }
void UpdatePlayer() { if (Input.GetButtonDown("Fire1")) { Interact(); } // If Player is interacting with a ShipComponent object if (focus) { BreakableComponent breakable = focus.GetComponent <BreakableComponent>(); if (breakable) { float progress = breakable.GetProgress(); progressBar.SetProgress(progress); } // Check if it's too far away and cancel current progress if (Vector2.Distance(focus.transform.position, transform.position) > 1f) { if (breakable) { progressBar.SetProgress(0f); progressBar.gameObject.SetActive(false); } focus.Cancel(); focus = null; } } if (!o2Active) { o2Current -= o2Multiplier * Time.deltaTime; } o2Bar.SetProgress(o2Current / o2TotalCapacity); }
private void GenerateShipObjects() { TiledMapObject[] objects = _tiledMap.GetLayer <TiledMapObjectLayer>("Breakables").Objects; // Loops through Objects created in Tiled foreach (TiledMapObject obj in objects) { // Default asset for this object. Defined by Custom Property called 'sprite' in Tiled if (!obj.Properties.TryGetValue("sprite", out string assetName)) { continue; } // Load sprite with given asset path/name Sprite sprite = new Sprite(_game.Content.Load <Texture2D>(assetName)); // Some sprites have a broken representation of them (ie Hull and O2 Vent). // So we should attempt to load a broken representation of this asset if at specified in Tiled Custom Property 'broken_sprite' // The BreakableComponent class should also cater for and skip 'null' BrokenSprites where applicable Sprite brokenSprite = null; if (obj.Properties.TryGetValue("broken_sprite", out string brokenAssetName)) { brokenSprite = new Sprite(_game.Content.Load <Texture2D>(brokenAssetName)); } string type = string.Empty; // If no valid Custom Property called 'item' is set for the required item, default it to screwdriver. // This is to ensure that the gameplay can flow as expected. if (!obj.Properties.TryGetValue("item", out type)) { type = "screwdriver"; } // Parse 'type' to Enum of 'ItemType'. Assumes a valid entry. ItemType item = (ItemType)Enum.Parse(typeof(ItemType), type, true); Vector2 pos = Vector2.Zero; pos.X = obj.Position.X + sprite.Origin.X; pos.Y = obj.Position.Y + sprite.Origin.Y; BreakableComponent b = new BreakableComponent(sprite, pos, item, obj.Name, brokenSprite); switch (obj.Type) { case "O2Component": b.OnComponentActivated += TurnOffO2; b.OnComponentFixed += TurnOnO2; break; case "Medbay": Medbay m = new Medbay(sprite, pos, item, obj.Name, brokenSprite); m.OnComponentInteracted += HealPlayer; b = m; break; case "Shield": b.OnComponentActivated += TurnOffShield; b.OnComponentFixed += TurnOnShield; break; case "GravityGenerator": b.OnComponentActivated += TurnOffGravity; b.OnComponentFixed += TurnOnGravity; break; case "Hull": Hull h = new Hull(sprite, pos, item, obj.Name, brokenSprite); b = h; break; } if (b != null) { _entities.Add(b); } } }
/// <inheritdoc/> public override bool Break(BreakableComponent breakTo) { if (Rows.Count == 0) { return(true); } if (Height < Rows[0].Height && !Rows[0].CanBreak) { return(false); } TableBase tableTo = breakTo as TableBase; if (tableTo == null) { return(true); } // find the break row index int breakRowIndex = 0; int breakRowIndexAdd = 0; bool rowBroken = false; float rowsHeight = 0; while (breakRowIndex < Rows.Count) { rowsHeight += Rows[breakRowIndex].Height; if (rowsHeight > Height) { float breakRowHeight = Rows[breakRowIndex].Height - (rowsHeight - Height); if (CanBreakRow(breakRowIndex, breakRowHeight)) { BreakRow(tableTo, breakRowIndex, breakRowHeight, rowsHeight - Height); breakRowIndexAdd = 1; rowBroken = true; } break; } breakRowIndex++; } // get the span list List <Rectangle> spans = GetSpanList(); // break the spans foreach (Rectangle span in spans) { if (span.Top < breakRowIndex + breakRowIndexAdd && span.Bottom > breakRowIndex) { TableCell cell = this[span.Left, span.Top]; TableCell cellTo = tableTo[span.Left, span.Top]; // update cell spans cell.RowSpan = breakRowIndex + breakRowIndexAdd - span.Top; cellTo.RowSpan = span.Bottom - breakRowIndex; // break the cell if (!rowBroken && !cell.Break(cellTo)) { cell.Text = ""; } // set the top span cell to the correct place tableTo[span.Left, span.Top] = new TableCell(); tableTo[span.Left, breakRowIndex] = cellTo; } } // remove unused rows from source (this table) while (breakRowIndex + breakRowIndexAdd < Rows.Count) { this.Rows.RemoveAt(Rows.Count - 1); } // remove unused rows from copy (tableTo) for (int i = 0; i < breakRowIndex; i++) { tableTo.Rows.RemoveAt(0); } return(true); }
/// <inheritdoc/> public override bool Break(BreakableComponent breakTo) { if (Rows.Count == 0) { return(true); } if (Height < Rows[0].Height) { return(false); } TableBase tableTo = breakTo as TableBase; if (tableTo == null) { return(true); } // get the span list List <Rectangle> spans = GetSpanList(); // find the break row index int breakRowIndex = 0; float rowsHeight = 0; while (breakRowIndex < Rows.Count && rowsHeight + Rows[breakRowIndex].Height < Height) { rowsHeight += Rows[breakRowIndex].Height; breakRowIndex++; } // break the spans foreach (Rectangle span in spans) { if (span.Top < breakRowIndex && span.Bottom > breakRowIndex) { TableCell cell = this[span.Left, span.Top]; TableCell cellTo = tableTo[span.Left, span.Top]; // update cell spans cell.RowSpan = breakRowIndex - span.Top; cellTo.RowSpan = span.Bottom - breakRowIndex; // break the cell if (!cell.Break(cellTo)) { cell.Text = ""; } tableTo[span.Left, span.Top] = new TableCell(); tableTo[span.Left, breakRowIndex] = cellTo; } } // delete last rows until all rows fit while (breakRowIndex < Rows.Count) { Rows.RemoveAt(Rows.Count - 1); } // delete first rows of the breakTo for (int i = 0; i < breakRowIndex; i++) { tableTo.Rows.RemoveAt(0); } return(true); }