Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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);
                }
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 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);
 }
Exemplo n.º 6
0
 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));
 }
Exemplo n.º 7
0
 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;
 }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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;
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
 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));
 }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        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;
                }
            }
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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;
                }
            }
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 20
0
 /// <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);
 }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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);
                }
            }
        }