public void Fire(float charge) { //Debug.Break(); if (Time.time - lastFireTime > cooldownTime) { if (!firing) // Prevent calling this in multiple consecutive frames { firing = true; //Debug.Log("Fire!"); // Reset variables chargeStartTime = -1.0f; maxCharge = false; maxChargeStartTime = -1.0f; // Fire a rowball if loaded if (loaded) { Vector3 aiming3 = new Vector3(ship.aiming.x, ship.aiming.y); Vector3 spawnPosition = position + aiming3; Rowball r = ObjectManager.AddRowball(spawnPosition, Color.white); r.level = (int)(charge * (float)(r.frames.Length - 1)); r.AddImpulse(charge * ship.aiming * firepower); r.shooter = ship; } // Apply recoil to the ship Vector2 recoilVector = charge * recoil * -ship.aiming; ship.AddImpulse(recoilVector); // Start firing animation StartCoroutine(FireRowballAnimation()); } } }
//Collision public void ProcessCollision(Collider2D other) { if (!totaled) { //Process rowball if (other.gameObject.layer == module.rowballLayer) { Rowball rowball = other.GetComponent <Rowball>(); RowballState state = GetRowballState(rowball); if (state == RowballState.Kill) { //Process totaled = true; effector.enabled = false; bounds.enabled = false; collisionBounds.gameObject.SetActive(false); //Drop rowball if carrying one Vector2 relativeVelocity = rowball.RelativeVelocity(velocity); if (hasRowball) { Rowball r = SpawnRowball(); r.AddImpulse(relativeVelocity * module.rowballDropFactor); } //Break closest point float minDistance = size * 2; //Arbitrary number, should always be larger Point targetPoint = null; foreach (Vertex v in edge) { Point p = vertexPoint(v); float distance; if (p != null) { distance = Vector2.Distance(p.position, rowball.position); if (distance < minDistance) { minDistance = distance; targetPoint = p; } } } center.RemoveLink(targetPoint); targetPoint.RemoveLinks(); //Random destruction and force for (int i = 0; i < center.linkCount; ++i) { if (Random.Range(0f, 1f) > (1 - module.frameworkDestroyChance)) { center.RemoveLink(i); //Framework } } foreach (Point p in points) { if (p != null) { if (p != center && Random.Range(0f, 1f) > (1 - module.edgeDestroyChance)) { p.RemoveLinks(); //Edge } //Force (no else, in order to process dummy links) p.AddImpulse(relativeVelocity * Random.Range(module.minDestroyForce, module.maxDestroyForce)); } } } } //Ramming (stealing) if (other.gameObject.layer == module.pointLayer) { Point p = other.GetComponent <Point>(); if (p.handler != null && p.handler.GetComponent <ShipRevamped>() != null) { ShipRevamped s = p.handler.GetComponent <ShipRevamped>(); Vector2 relativeVelocity = center.velocity - s.center.velocity; //Check if relative velocity is above threshold and stealer is moving faster if (relativeVelocity.magnitude > module.stealSpeed && s.center.velocity.magnitude > center.velocity.magnitude) { Pass(s); } } } } }
public void ProcessCollision(Collider2D other) { if (!totaled) { //Process rowball if (other.gameObject.layer == module.rowballLayer) { Rowball rowball = other.GetComponent <Rowball>(); RowballState state = GetRowballState(rowball); // float comparator = 2 * shipModule.volleyWidth - 1; for (float x = -1; x <= 1; x += 0.3f) { for (float y = -1; y <= 1; y += 0.3f) { Vector2 v = new Vector2(x, y).normalized; float d0 = Vector2.Dot(aiming.normalized, v); float d1 = Vector2.Dot(rowball.velocity.normalized, v); Debug.DrawRay(position, -v * 32, (d0 < comparator) ? Color.green : Color.red, 4); if (d1 < comparator) { Debug.DrawRay(position, v * 16, Color.blue, 4); } } } //if (volley && rowball.shooter != null && rowball.level > 1) //{ // float dotProduct = Vector2.Dot(rowball.velocity.normalized, aiming.normalized); // if (dotProduct < comparator) // { // rowball.velocity = (new Vector2(-1 * shipModule.volleySpeed * rowball.velocity.x, -1 * shipModule.volleySpeed * rowball.velocity.y)); // rowball.shooter = this.gameObject; //Update shooter on volley // InterfaceManager.CreateTextEffect( // "VOLLEY", // position + new Vector2(Random.Range(size, size * 2), Random.Range(size, size * 2)), // color, 0.3f //Magic number // ); // PlayerManager.GetPlayer(this).volleys++; // //Hardcoded for now // return; // } //} if (state == RowballState.Kill) { if (!rowball.clearedShot && rowball.shooter == this) { //Debug.Log("Rowball hasn't cleared shot!"); return; } // Only allow kills when game is in active state (not in ready/end state) if (GameManager.instance.roundState != GameManager.RoundState.PLAYING) { return; } //Process Debug.Log("Player totaled"); this.totaled = true; effector.enabled = false; bounds.enabled = false; collisionBounds.gameObject.SetActive(false); //Drop rowball if carrying one Vector2 relativeVelocity = rowball.RelativeVelocity(velocity); if (hasRowball) { Rowball r = ObjectManager.AddRowball(this.position, Color.white);; r.AddImpulse(relativeVelocity * module.rowballDropFactor); hasRowball = false; } //Break closest point float minDistance = size * 2; //Arbitrary number, should always be larger Point targetPoint = null; foreach (Vertex v in edge) { Point p = vertexPoint(v); float distance; if (p != null) { distance = Vector2.Distance(p.position, rowball.position); if (distance < minDistance) { minDistance = distance; targetPoint = p; } } } if (targetPoint != null) { center.RemoveLink(targetPoint); targetPoint.RemoveLinks(); } //Random destruction and force for (int i = 0; i < center.linkCount; ++i) { if (Random.Range(0f, 1f) > (1 - module.frameworkDestroyChance)) { center.RemoveLink(i); //Framework } } foreach (Point p in points) { if (p != null && p != center) { if (Random.Range(0f, 1f) > (1 - module.edgeDestroyChance)) { p.RemoveLinks(); //Edge } //Force (no else, in order to process dummy links) Vector2 explosionDirection = (relativeVelocity.normalized + (p.position - position).normalized) * 0.5f; p.AddImpulse(explosionDirection * Random.Range(module.minDestroyForce, module.maxDestroyForce)); } } //Event Debug.Log("Player killed"); Observer.OnKill.Invoke(this, rowball); } else {//if(state==RowballState.Pickup){ if (!hasRowball && Time.time - cannon.lastFireTime > cannon.cooldownTime) { //Process pickup //InitSpritePosition(rowball.position); hasRowball = true; Destroy(rowball.gameObject); } else if (rowball.shooter == null || rowball.clearedShot) { rowball.velocity = new Vector2(this.velocity.x * 2, this.velocity.y * 2); //Magic numbers } } } //Ramming (force-fire and stealing) if (other.gameObject.layer == module.pointLayer) { Point p = other.GetComponent <Point>(); if (p.handler != null && p.handler.GetComponent <ShipRevamped>() != null) { ShipRevamped s = p.handler.GetComponent <ShipRevamped>(); Vector2 relativeVelocity = center.velocity - s.center.velocity; //Check if relative velocity is above threshold and stealer is moving faster if (hasRowball && relativeVelocity.magnitude > module.stealSpeed && s.center.velocity.magnitude > center.velocity.magnitude) { string stealString = "BUMP"; if (!s.hasRowball) { Pass(s); stealString = "STEAL"; PlayerManager.GetPlayer(this).steals++; } //else this.cannon.Fire(0.1f); InterfaceManager.CreateTextEffect( stealString, position + new Vector2(Random.Range(size, size * 2), Random.Range(size, size * 2)), s.color, 0.3f //Magic number ); } //Ramming if (s.center.velocity.magnitude > center.velocity.magnitude && Vector2.Dot(relativeVelocity.normalized, (s.center.position - center.position).normalized) > 0 ) { AddImpulseAtPoint(-relativeVelocity * module.ramMultiplier, p.position, module.ramScale); } } } } }