private void CheckCollision() { /* * if (CheckCollisions) * { * foreach (Collider other in GameWorld.Instance.Colliders) * { * if (other != this) * { * if (CollisionBox.Intersects(other.CollisionBox)) * { * gameObject.OnCollisionStay(other); * * if (!otherColliders.Contains(other)) * { * otherColliders.Add(other); * gameObject.OnCollisionEnter(other); * } * * } * else if (otherColliders.Contains(other)) * { * otherColliders.Remove(other); * gameObject.OnCollisionExit(other); * } * } * } * } */ if (CheckCircleCollisions) { foreach (Collider other in GameWorld.Instance.CircleColliders) { if (other != this) { if (circleCollisionBox.Intersects(other.circleCollisionBox)) { gameObject.OnCollisionStay(other); if (!otherColliders.Contains(other)) { otherColliders.Add(other); gameObject.OnCollisionEnter(other); } } else if (otherColliders.Contains(other)) { otherColliders.Remove(other); gameObject.OnCollisionExit(other); } } } } }
public override void Update() { if (!isLoaded) { LoadContent(GameWorld.Instance.Content); } KeyboardState keyState = Keyboard.GetState(); MouseState mouse = Mouse.GetState(); if (activated) { activationTime += GameWorld.Instance.deltaTime; if (activationTime > 2) { activated = false; activationTime = 0; foreach (GameObject go in GameWorld.gameObjects) { if (go.Tag == "Deflect") { GameWorld.objectsToRemove.Add(go); if (GameWorld.Instance.client != null) { GameWorld.Instance.client.SendRemoval("Deflect", gameObject.Id); } } } } } if (keyState.IsKeyDown(key) && canShoot) { canShoot = false; activated = true; effect = new GameObject(); effect.AddComponent(new SpriteRenderer(effect, "Deflect", 1)); effect.AddComponent(new Collider(effect, true, true)); effect.Tag = "Deflect"; effect.Id = gameObject.Id; GameWorld.newObjects.Add(effect); if (GameWorld.Instance.client != null) { GameWorld.Instance.client.SendProjectile("Deflect,Create", new Vector2(gameObject.transform.position.X, gameObject.transform.position.Y), Vector2.Zero); } } if (activated) { float radius = (gameObject.GetComponent("Collider") as Collider).CircleCollisionBox.Radius * 2F; radius = radius - (gameObject.GetComponent("Collider") as Collider).CircleCollisionBox.Radius; effect.transform.position = new Vector2(gameObject.transform.position.X - radius, gameObject.transform.position.Y - radius); if (GameWorld.Instance.client != null) { GameWorld.Instance.client.SendProjectile("Deflect,Update", effect.transform.position, Vector2.Zero); } foreach (GameObject go in GameWorld.gameObjects) { if (abilities.Exists(x => x == go.Tag) && gameObject.Id != go.Id) { Circle playerCircle = new Circle(); playerCircle.Center = new Vector2(gameObject.transform.position.X + 16, gameObject.transform.position.Y + 16); playerCircle.Radius = (gameObject.GetComponent("Collider") as Collider).CircleCollisionBox.Radius * 2F; if (playerCircle.Intersects((go.GetComponent("Collider") as Collider).CircleCollisionBox)) { //SetVector(gameObject, go); } } } } }
public void OnCollisionEnter(Collider other) { if (gameObject.Tag == "Map") { objectsToRemove.Add(other.gameObject); } foreach (GameObject go in GameWorld.gameObjects) { if (other.gameObject.Tag == "Chain" && go.Tag == "Chain" && other.gameObject.Id == GameWorld.Instance.player.Id && go.Id == GameWorld.Instance.player.Id && gameObject.Tag == "Pillar" && !Projectile.chainActivated) { (go.GetComponent("Projectile") as Projectile).chainTarget = gameObject; Projectile.chainActivated = true; break; } } if (gameObject.Tag == "Pillar") { foreach (GameObject go in GameWorld.gameObjects) { if (go.Tag != "Map" && go.Tag != "Pillar" && go.Tag != "Lava" && go.Tag != "LavaSpot" && go.Tag != "Spellshield" && go.Tag != "Deflect" && go.Tag != "Chain") { Circle playerCircle = new Circle(); playerCircle.Center = new Vector2(gameObject.transform.position.X + 16, gameObject.transform.position.Y + 16); playerCircle.Radius = (gameObject.GetComponent("Collider") as Collider).CircleCollisionBox.Radius * 1.2F; if (playerCircle.Intersects((go.GetComponent("Collider") as Collider).CircleCollisionBox)) { Physics physicsSpell = (go.GetComponent("Physics") as Physics); Physics physicsPlayer = (gameObject.GetComponent("Physics") as Physics); float L = Vector2.Distance(gameObject.transform.position, go.transform.position); float ex = (gameObject.transform.position.X - go.transform.position.X) / L; float ey = (gameObject.transform.position.Y - go.transform.position.Y) / L; float ox = -1 * ey; float oy = ex; double e1x = (0 * ex + 0 * ey) * ex; double e1y = (0 * ex + 0 * ey) * ey; double e2x = (physicsSpell.Velocity.X * ex + physicsSpell.Velocity.Y * ey) * ex; double e2y = (physicsSpell.Velocity.X * ex + physicsSpell.Velocity.Y * ey) * ey; double o1x = (0 * ox + 0 * oy) * ox; double o1y = (0 * ox + 0 * oy) * oy; double o2x = (physicsSpell.Velocity.X * ox + physicsSpell.Velocity.Y * oy) * ox; double o2y = (physicsSpell.Velocity.X * ox + physicsSpell.Velocity.Y * oy) * oy; int playerMass = 10; int spellMass = 1; double vxs = (playerMass * e1x + spellMass * e2x) / (playerMass + spellMass); double vys = (playerMass * e1y + spellMass * e2y) / (playerMass + spellMass); //Velocity Ball 1 after Collision double vx1 = -e1x + 2 * vxs + o1x; double vy1 = -e1y + 2 * vys + o1y; //Velocity Ball 2 after Collision double vx2 = -e2x + 2 * vxs + o2x; double vy2 = -e2y + 2 * vys + o2y; Vector2 test = new Vector2((float)vx2, (float)vy2); (go.GetComponent("Physics") as Physics).Velocity = test; go.transform.position += test; if (abilities.Exists(x => x == go.Tag) || go.Tag.Contains("Nova")) { test.Normalize(); (go.GetComponent("Projectile") as Projectile).TestVector = test * go.ProjectileSpeed; } } } } } }
public void Update() { if (gameObject.Tag.Contains("Firewave")) { foreach (GameObject go in GameWorld.gameObjects) { if (go.Tag == "Player" || go.Tag == "Dummy") { if (intersects((go.GetComponent("Collider") as Collider).CircleCollisionBox, (gameObject.GetComponent("Collider") as Collider).CollisionBox)) { FirewavePush(go, go.Id); } } } } if (gameObject.Id == GameWorld.Instance.player.Id) { foreach (GameObject other in GameWorld.gameObjects) { if (other.Tag == "Deflect" && other.Id != gameObject.Id) { GameObject temp = new GameObject(); foreach (GameObject go in GameWorld.gameObjects) { if (go.Tag == "Enemy" && go.Id == other.Id) { temp = go; } if (temp.Tag == "Enemy") { Circle playerCircle = new Circle(); playerCircle.Center = new Vector2(temp.transform.position.X + 16, temp.transform.position.Y + 16); playerCircle.Radius = (temp.GetComponent("Collider") as Collider).CircleCollisionBox.Radius * 2F; if (playerCircle.Intersects((gameObject.GetComponent("Collider") as Collider).CircleCollisionBox)) { Deflect.SetVector(temp, gameObject); temp = new GameObject(); } } } } } KeyboardState keyState = Keyboard.GetState(); if (gameObject.Tag.Contains("Boomerang")) { if (Vector2.Distance(originalPos, gameObject.transform.position) > 100) { boomerangReturn = true; } else { (gameObject.GetComponent("Physics") as Physics).Acceleration += testVector / 10 * projectileSpeed; } if (boomerangReturn) { boomerangTimer += GameWorld.Instance.deltaTime; Vector2 playerPos = GameWorld.Instance.player.transform.position; Vector2 boomReturn = (gameObject.GetComponent("Physics") as Physics).GetVector((new Vector2(playerPos.X + 16, playerPos.Y + 16)), new Vector2(gameObject.transform.position.X + 16, gameObject.transform.position.Y + 16)); boomReturn.Normalize(); (gameObject.GetComponent("Physics") as Physics).Acceleration += boomReturn / 10 * projectileSpeed; } if (boomerangTimer >= 5) { if (gameObject.Tag == "Boomerang") { //GameWorld.objectsToRemove.Add(gameObject); } } } if (gameObject.Tag == "DeathMeteor") { (gameObject.GetComponent("Physics") as Physics).Acceleration += meteorVector / 10; abilityTimer += 0.001f; } if (gameObject.Tag == "GravityWell") { (gameObject.GetComponent("Physics") as Physics).Acceleration += (TestVector / 4) * projectileSpeed; foreach (GameObject go in GameWorld.gameObjects) { Vector2 pull = (gameObject.GetComponent("Physics") as Physics).GetVector(gameObject.transform.position, go.transform.position); pull.Normalize(); if (go.Tag == "Dummy" || go.Tag == "Enemy") { if (Vector2.Distance(gameObject.transform.position, go.transform.position) < 300) { (go.GetComponent("Physics") as Physics).Acceleration += pull / 10; if (GameWorld.Instance.client != null && go.Tag == "Enemy") { GameWorld.Instance.client.SendEnemyAcceleration(go.Id, pull / 10); } } } } } if (gameObject.Tag == "DeathMine") { mineTimer += GameWorld.Instance.deltaTime; if (mineTimer > mineActivationTime) { deathMineActivated = true; (gameObject.GetComponent("SpriteRenderer") as SpriteRenderer).Color = Color.Red; Color color = Color.Red; if (GameWorld.Instance.client != null) { GameWorld.Instance.client.SendColor(gameObject.Id, gameObject.Tag, color.R, color.G, color.B, color.A); } } } if ((gameObject.Tag.Contains("Fireball") || gameObject.Tag.Contains("Drain") || gameObject.Tag.Contains("Chain") || gameObject.Tag.Contains("Nova")) && !gameObject.Tag.Contains("Firewave")) { if (gameObject.Tag.Contains("Drain") || gameObject.Tag.Contains("Chain")) { (gameObject.GetComponent("Physics") as Physics).Acceleration += (testVector / 2.5F) * projectileSpeed; } else { (gameObject.GetComponent("Physics") as Physics).Acceleration += (testVector / 2) * projectileSpeed; } if (distanceTravelled > travelDistance) { if (gameObject.Tag.Contains("Chain") && !chainActivated) { //GameWorld.objectsToRemove.Add(gameObject); } else if (gameObject.Tag != "Chain") { //GameWorld.objectsToRemove.Add(gameObject); } } } if (gameObject.Tag.Contains("Firewave")) { (gameObject.GetComponent("Physics") as Physics).Acceleration += (testVector / 15); } if (chainActivated) { if (chainTarget != null) { chainTimer += GameWorld.Instance.deltaTime; gameObject.transform.position = chainTarget.transform.position; Vector2 pull = (gameObject.GetComponent("Physics") as Physics).GetVector(GameWorld.Instance.player.transform.position, chainTarget.transform.position); pull.Normalize(); (GameWorld.Instance.player.GetComponent("Physics") as Physics).Acceleration -= pull / 10; if (chainTarget.Tag == "Enemy") { if (GameWorld.Instance.client != null) { GameWorld.Instance.client.Chain(chainTarget.Id, pull / 10); } } if (keyState.IsKeyDown(Keys.T) || chainTimer > 2 || Vector2.Distance(new Vector2(chainTarget.transform.position.X + 16, chainTarget.transform.position.Y + 16), new Vector2(GameWorld.Instance.player.transform.position.X + 16, GameWorld.Instance.player.transform.position.Y + 16)) < 35) { chainActivated = false; GameWorld.objectsToRemove.Add(gameObject); if (GameWorld.Instance.client != null && gameObject.Id == GameWorld.Instance.player.Id) { GameWorld.Instance.client.SendRemoval(gameObject.Tag, gameObject.Id); if (chainTarget.Tag == "Enemy") { GameWorld.Instance.client.ChainRemove(chainTarget.Id); } } } } } if (gameObject.Tag == "HomingMissile") { if (homingTimer > 0.5F) { //if (!GameWorld.gameObjects.Exists(x => x.Tag == "Enemy") && !GameWorld.gameObjects.Exists(x => x.Tag == "Dummy")) //{ // GameWorld.objectsToRemove.Add(gameObject); // if (GameWorld.Instance.client != null) // { // GameWorld.Instance.client.SendRemoval(gameObject.Tag, gameObject.Id); // } //} foreach (GameObject go in GameWorld.gameObjects) { if (Vector2.Distance(gameObject.transform.position, go.transform.position) < 300 && (go.Tag == "Dummy" || go.Tag == "Enemy")) { distance = Vector2.Distance(gameObject.transform.position, go.transform.position); bestTarget = go.transform.position; foreach (GameObject item in GameWorld.gameObjects) { if (Vector2.Distance(gameObject.transform.position, item.transform.position) < distance && (item.Tag == "Dummy" || item.Tag == "Enemy")) { distance = Vector2.Distance(gameObject.transform.position, item.transform.position); bestTarget = item.transform.position; } } if (bestTarget != null) { } Vector2 test = (gameObject.GetComponent("Physics") as Physics).GetVector(bestTarget, gameObject.transform.position); test.Normalize(); (gameObject.GetComponent("Physics") as Physics).Acceleration += (test / 5) * projectileSpeed; break; //if (go.Tag == "Enemy") //{ // (gameObject.GetComponent("Physics") as Physics).Acceleration += (test / 10) * projectileSpeed; // break; //} //else //{ // (gameObject.GetComponent("Physics") as Physics).Acceleration += (test / 10) * projectileSpeed; // break; //} } if (Vector2.Distance(gameObject.transform.position, go.transform.position) > 300 && (go.Tag == "Dummy" || go.Tag == "Enemy")) { //Vector2 test = (gameObject.GetComponent("Physics") as Physics).GetVector(target, gameObject.transform.position); //test.Normalize(); (gameObject.GetComponent("Physics") as Physics).Acceleration += (testVector / 10) * projectileSpeed; } } } else { homingTimer += GameWorld.Instance.deltaTime; Vector2 test = (gameObject.GetComponent("Physics") as Physics).GetVector(target, gameObject.transform.position); test.Normalize(); (gameObject.GetComponent("Physics") as Physics).Acceleration += (test / 10) * projectileSpeed; } } Vector2 oldPos = gameObject.transform.position; gameObject.transform.position += (gameObject.GetComponent("Physics") as Physics).Velocity; distanceTravelled += Vector2.Distance(oldPos, gameObject.transform.position); CheckDistance(); if (GameWorld.Instance.client != null) { if (!gameObject.Tag.Contains("Enemy")) { GameWorld.Instance.client.SendProjectile(gameObject.Tag + ",Update", gameObject.transform.position, (gameObject.GetComponent("Physics") as Physics).Velocity); } } if (abilityTimer > 2) { if (gameObject.Tag == "DeathMeteor" || gameObject.Tag.Contains("Nova")) { //GameWorld.objectsToRemove.Add(gameObject); } } } }