public bool IsNodeSolid(float fx, float fy, RectObject self, bool ignoreObjects = false) { int x = (int)fx; int y = (int)fy; return(IsNodeSolid(x, y, self, ignoreObjects)); }
public bool IsNodeSolid(Vector2 pos, RectObject self, bool ignoreObjects = false) { int x = (int)pos.x; int y = (int)pos.y; return(IsNodeSolid(x, y, self, ignoreObjects)); }
public static void SpawnExplosion(GameObject expl, Vector2 pos, int size, int radius, float force, int puffs, float puffForce, GameObject puffObject = null, System.Action onExplosion = null) { if (expl == null) { expl = GameManager.GM.DefaultExplosionObject; } RectObject obj = GameManager.GM.SpawnObject(expl, pos); if (obj is Projectile) { Projectile proj = (Projectile)obj; proj.ExplosionRadius = radius; proj.ExplosionForce = force; proj.PuffCount = puffs; proj.PuffForce = puffForce; if (puffObject == null) { puffObject = GameManager.GM.DefaultExplosionPuffObject; } proj.ExplosionPuffObject = puffObject; proj.OnExplosion = onExplosion; Animator anim = proj.GetComponent <Animator>(); if (anim) { anim.SetInteger("size", size); } } }
public RectObject SpawnSimpleObject(Vector2Int pos, Vector2 vel, string name, Color col, GameObject container) { GameObject obj = new GameObject(name); obj.transform.parent = container.transform; obj.transform.position = GridPosToWorldPos(new Vector2(pos.x, pos.y)); RectObject robj = obj.AddComponent <RectObject>(); robj.Position = pos; robj.Velocity = vel; robj.Width = 1; robj.Height = 1; SpriteRenderer sr = obj.gameObject.AddComponent <SpriteRenderer>(); Texture2D tex = new Texture2D(1, 1); Color[] pixels = new Color[1]; pixels[0] = col; tex.SetPixels(pixels); Sprite spr = Sprite.Create(tex, new Rect(0, 0, 1, 1), Vector2.zero); sr.sprite = spr; sr.sortingOrder = 10; return(robj); }
public bool IsSolid(RectObject self, bool ignoreObjects = false, System.Func <RectObject, bool> ignoreCondition = null) { if (Solid) { return(true); } if (Object && Object != self && !ignoreObjects) { if (ignoreCondition != null) { bool ignoreObject = ignoreCondition(Object); if (!ignoreObject) { return(true); } else if (ignoreObject) { return(false); } } else { return(true); } } return(false); }
public bool IsNodeSolid(int x, int y, RectObject self, bool ignoreObjects = false) { if (!IsInBounds(x, y)) { return(false); } return(MapGrid[x, y].IsSolid(self, ignoreObjects)); }
public Collision(Node collisionNode, RectObject collisionObject, Vector2Int collisionPos, Vector2Int hitPos, Vector2Int hitPart, Vector2 slide) { CollisionNode = collisionNode; CollisionObject = collisionObject; CollisionPos = collisionPos; HitPos = hitPos; HitPart = hitPart; HCollision = -1; VCollision = -1; Direction = Vector2Int.zero; Slide = slide; }
public void Shoot(float power) { //Debug.Log("Shoot"); State = WormState.Busy; Vector2 spawnPos = GetSpawnPosition(CurrentWeapon.GetWidth(), CurrentWeapon.GetHeight(), Angle, 1.0f); //Debug.LogFormat("Spawn position to position : {0} / {1}", spawnPos, Position); RectObject proj = GameManager.GM.SpawnObject(CurrentWeapon.Projectile, spawnPos); proj.AddForce(GetAim() * power); //CurrentWeapon = null; SetSpriteShooting(Angle); }
public RectObject SpawnFluid(Vector2Int pos, Color col, Vector2 vel, string name) { RectObject fluid = SpawnSimpleObject(pos, vel, name, col, FluidsContainer); fluid.CollisionIgnoringCondition = (w) => w is WormObject; fluid.Elasticity = 0; fluid.Mass = 0.9f; fluid.OnTickEvent += FluidCheck; fluid.DisableHorizontalMovement = true; fluid.DisableUpwardsMovement = true; return(fluid); }
public void SpawnDebugObject(Vector2Int pos) { objectCount++; Vector3 npos = new Vector3(pos.x / 100.0f, pos.y / 100.0f, 0); GameObject obj = Instantiate(DebugObjectPrefab, Vector3.zero, Quaternion.identity, ObjectContainer.transform); obj.name = "Debug_object_" + objectCount; RectObject rectObj = obj.GetComponentInChildren <RectObject>(); rectObj.SetSize(DebugObjectSize); Transform t = rectObj.transform; t.position = npos; }
private RectObject SpawnExplosionPuff(GameObject puff, float angle, float dst, float force) { if (puff == null) { return(null); } angle = angle * Mathf.PI / 180.0f; Vector2 vel = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)); Vector2 pos = Position + vel * dst; RectObject obj = GameManager.GM.SpawnObject(puff, pos); obj.Velocity = vel * force; return(obj); }
public bool IsNodeSolid(Vector2 pos, Vector2Int dir, int count, RectObject self, bool ignoreObjects = false) { for (int i = 0; i < count; i++) { int x = (int)pos.x + i * dir.x; int y = (int)pos.y + i * dir.y; if (IsNodeSolid(x, y, self, ignoreObjects)) { return(true); } } return(false); }
protected override bool OnCollideWithObject(RectObject other, ref Vector2 vel) { if (other is WormObject && vel.magnitude > KnockOutVelocity) { //Debug.LogFormat("Hit a worm, vel {0}", vel); WormObject worm = (WormObject)other; vel *= 0.8f; //Debug.Break(); worm.AddForce(vel); worm.SetFalling(); return(false); } return(base.OnCollideWithObject(other, ref vel)); }
public RectObject SpawnObject(GameObject prefab, Vector2 pos) { GameObject gameObj = Instantiate(prefab, ObjectContainer.transform); gameObj.transform.position = GridPosToWorldPos(pos); RectObject obj = gameObj.GetComponent <RectObject>(); obj.Position = new Vector2(pos.x, pos.y); if (!obj.DisableObjectCollisions) { SetNodeObject(obj); } //Debug.LogFormat("Spawned {0} at {1}", obj, pos); return(obj); }
public void SetNodeObject(RectObject obj) { Vector2Int pos = obj.GetCurrentPos(); for (int x = 0; x < obj.Width; x++) { for (int y = 0; y < obj.Height; y++) { int nx = pos.x + x; int ny = pos.y + y; if (nx < 0 || nx >= Width || ny < 0 || ny >= Height) { continue; } MapGrid[pos.x + x, pos.y + y].Object = obj; } } }
public HashSet <RectObject> Explode(Vector2Int pos, int radius, int forceRadius, float maxForce, int debrisChance = 16) { HashSet <RectObject> hitObjects = new HashSet <RectObject>(); System.Action <Node> act = (x) => { if (x.Solid == true && GRandom.GetInt(1, 100) <= debrisChance) { Color col = MapSprite.texture.GetPixel(x.Position.x, x.Position.y); string name = "Debris_" + debrisCount; //RectObject debris = SpawnDebris(x.Position, col, Vector2.zero, name, GRandom.GetFloat(2.0f, 4.0f)); RectObject debris = SpawnDebris(x.Position, col, Vector2.zero, name, 0, false, true); hitObjects.Add(debris); } x.Solid = false; if (x.Object) { if (!hitObjects.Contains(x.Object)) { hitObjects.Add(x.Object); } } }; RadiusAction(pos, radius, MapGrid, act, null, NoColor); foreach (var hitObject in hitObjects) { Vector2 objectCenter = hitObject.GetCenter(); float dist = Vector2.Distance(pos, objectCenter); float dist_b = dist / radius; float force = Mathf.Lerp(maxForce, 0, dist_b); Vector2 vel = new Vector2(objectCenter.x - pos.x, objectCenter.y - pos.y).normalized; hitObject.AddForce(vel * force); if (hitObject is WormObject) { WormObject worm = (WormObject)hitObject; int damage = (int)(force / DamageFactor); worm.GetHurt(damage, DamageType.Explosion); } } mapChanged = true; return(hitObjects); }
public void MergeDebris(RectObject debris) { Color col = debris.GetComponent <SpriteRenderer>().sprite.texture.GetPixel(0, 0); Vector2Int pos = debris.GetCurrentPos(); Vector2Int bot = pos; bot.y -= 1; if (IsInBounds(bot) && IsInBounds(pos)) { Node b = MapGrid[bot.x, bot.y]; Node n = MapGrid[pos.x, pos.y]; if (b.Solid && !b.Object && !n.Object) //Checking if bottom node (the node debris is on) is solid but NOT an object. Also checking current node if not occupied by any object. { Destroy(debris.gameObject); n.Solid = true; MapSprite.texture.SetPixel(pos.x, pos.y, col); mapChanged = true; } } }
public RectObject SpawnDebris(Vector2Int pos, Color col, Vector2 vel, string name, float lifetime, bool disableCollisions = true, bool merge = false) { RectObject debris = SpawnSimpleObject(pos, vel, name, col, DebrisContainer); debris.DisableObjectCollisions = disableCollisions; debris.IgnoredByObjects = true; debris.CollisionIgnoringCondition = (w) => w is WormObject; debris.Elasticity = 0.6f; debris.Mass = 0.9f; if (lifetime > 0) { Destroy(debris.gameObject, GRandom.GetFloat(lifetime, lifetime + 2.0f)); } else if (merge) { debris.OnStopEvent += MergeDebris; } debrisCount++; debris.OnDestroyEvent += (d) => debrisCount--; return(debris); }
public bool IsNodeSolid(Vector2 pos, Vector2 vel, int count_x, int count_y, RectObject self, bool ignoreObjects = false) { Vector2 newpos = pos; if (vel.x > 0) { newpos.x += count_x - 1; if (IsNodeSolid(newpos, Vector2Int.up, count_y, self, ignoreObjects)) { return(true); } } else if (vel.x < 0) { if (IsNodeSolid(pos, Vector2Int.up, count_y, self, ignoreObjects)) { return(true); } } if (vel.y > 0) { newpos.y += count_y - 1; if (IsNodeSolid(newpos, Vector2Int.right, count_x, self, ignoreObjects)) { return(true); } } else if (vel.y < 0) { if (IsNodeSolid(pos, Vector2Int.right, count_x, self, ignoreObjects)) { return(true); } } return(false); }
/// <summary> /// If returns false, it won't bounce of the object. /// </summary> protected virtual bool OnCollideWithObject(RectObject other, ref Vector2 vel) { vel = vel / 2.0f; other.AddForce(-vel); return(true); }
public Collision CreateCollision(int hitPosX, int hitPosY, int hitPartX, int hitPartY, Node n, RectObject colobj, int hcol, int vcol, Vector2Int dir, Vector2 slide) { Vector2Int hitPos = new Vector2Int(hitPosX, hitPosY); Vector2Int hitPart = new Vector2Int(hitPartX, hitPartY); Collision col = new Collision(n, colobj, n.Position, hitPos, hitPart, slide); col.VCollision = vcol; col.HCollision = hcol; col.Direction = dir; return(col); }
public void FluidCheck(RectObject fluid) { float flowSpeed = 0.25f; if (fluid.Velocity.y < 0) // && Mathf.Abs(fluid.Velocity.x) > flowSpeed) { return; } Vector2Int pos = fluid.GetCurrentPos(); Vector2Int bot = pos; bot.y--; for (int i = 1; i < 4; i++) { Vector2Int left = bot; left.x -= i; Vector2Int righ = bot; righ.x += i; Node lNode = null; Node rNode = null; if (IsInBounds(left)) { lNode = MapGrid[left.x, left.y]; } if (IsInBounds(righ)) { rNode = MapGrid[righ.x, righ.y]; } Node flow = null; float dir = 0; if (lNode && rNode) { if (!lNode.Solid && !rNode.Solid) { if (GRandom.GetBool()) { flow = lNode; dir = -flowSpeed; } else { flow = rNode; dir = flowSpeed; } } else if (!lNode.Solid) { flow = lNode; dir = -flowSpeed; } else if (!rNode.Solid) { flow = rNode; dir = flowSpeed; } } else if (lNode && !lNode.Solid) { flow = lNode; dir = -flowSpeed; } else if (rNode && !rNode.Solid) { flow = rNode; dir = flowSpeed; } if (flow && !flow.IsSolid(fluid)) { Node u = MapGrid[flow.Position.x, flow.Position.y + 1]; if (u.IsSolid(fluid)) { continue; } // fluid.AddForce(new Vector2(dir, 0)); ClearNodeObject(fluid.Position, 1, 1); fluid.LastPosition = fluid.Position; fluid.Position = new Vector2(fluid.Position.x + dir, fluid.Position.y); //fluid.Position.x += 0.5f; //fluid.Position.y += 0.5f; SetNodeObject(fluid); } } }