예제 #1
0
        public void AddDestructibleBlock(Vec2 start, Vec2 end, float blockWidth)
        {
            var block = new DestructibleBlock(blockWidth, start, end);

            DestructibleBlocks.Add(block);
            AddChild(block);
        }
예제 #2
0
파일: Bullet.cs 프로젝트: peter-fogg/RPFoD
    /*
     * Dammit dammit dammit this shouldn't be copy/pasted but whatever
     */
    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == cameFrom)
        {
            return;
        }
        Player player = other.gameObject.GetComponent <Player>();

        if (player != null)
        {
            player.health -= damage;
            Destroy(gameObject);
            return;
        }
        Robot robot = other.gameObject.GetComponent <Robot>();

        if (robot != null)
        {
            robot.health -= damage;
            Destroy(gameObject);
            return;
        }
        DestructibleBlock block = other.gameObject.GetComponent <DestructibleBlock>();

        if (block != null)
        {
            block.health -= damage;
            Destroy(gameObject);
            return;
        }
        Destroy(gameObject);
    }
예제 #3
0
 public static void Spawn(DestructibleBlock _destructibleBlock)
 {
     using (Packet _packet = new Packet((int)ServerPackets.blockSpawn))
     {
         _packet.Write(_destructibleBlock.id);
         _packet.Write(_destructibleBlock.transform.position);
         SendTCPDataToAll(_packet);
     }
 }
예제 #4
0
    public void ExecuteClip(IClip clip)
    {
        BlockSimplification.epsilon = (int64)(simplifyEpsilonPercent / 100f * blockSize * VectorEx.float2int64);

        List <Vector2i> clipVertices = clip.GetVertices();

        ClipBounds bounds = clip.GetBounds();
        int        x1     = Mathf.Max(0, (int)(bounds.lowerPoint.x / blockSize));

        if (x1 > resolutionX - 1)
        {
            return;
        }
        int y1 = Mathf.Max(0, (int)(bounds.lowerPoint.y / blockSize));

        if (y1 > resolutionY - 1)
        {
            return;
        }
        int x2 = Mathf.Min(resolutionX - 1, (int)(bounds.upperPoint.x / blockSize));

        if (x2 < 0)
        {
            return;
        }
        int y2 = Mathf.Min(resolutionY - 1, (int)(bounds.upperPoint.y / blockSize));

        if (y2 < 0)
        {
            return;
        }

        for (int x = x1; x <= x2; x++)
        {
            for (int y = y1; y <= y2; y++)
            {
                if (clip.CheckBlockOverlapping(new Vector2f((x + 0.5f) * blockSize, (y + 0.5f) * blockSize), blockSize))
                {
                    DestructibleBlock block = blocks[x + resolutionX * y];

                    List <List <Vector2i> > solutions = new List <List <Vector2i> >();

                    ClipperLib.Clipper clipper = new ClipperLib.Clipper();
                    clipper.AddPolygons(block.Polygons, ClipperLib.PolyType.ptSubject);
                    clipper.AddPolygon(clipVertices, ClipperLib.PolyType.ptClip);
                    clipper.Execute(ClipperLib.ClipType.ctDifference, solutions,
                                    ClipperLib.PolyFillType.pftNonZero, ClipperLib.PolyFillType.pftNonZero);

                    UpdateBlockBounds(x, y);

                    block.UpdateGeometryWithMoreVertices(solutions, width, height, depth);
                }
            }
        }
    }
예제 #5
0
    private DestructibleBlock CreateBlock()
    {
        GameObject childObject = new GameObject();

        childObject.name = "DestructableBlock";
        childObject.transform.SetParent(transform);
        childObject.transform.localPosition = Vector3.zero;

        DestructibleBlock blockComp = childObject.AddComponent <DestructibleBlock>();

        blockComp.SetMaterial(material);

        return(blockComp);
    }
예제 #6
0
    public void Initialize()
    {
        blocks = new DestructibleBlock[resolutionX * resolutionY];

        for (int x = 0; x < resolutionX; x++)
        {
            for (int y = 0; y < resolutionY; y++)
            {
                List <List <Vector2i> > polygons = new List <List <Vector2i> >();

                List <Vector2i> vertices = new List <Vector2i>();
                vertices.Add(new Vector2i {
                    x = x * blockSizeScaled, y = (y + 1) * blockSizeScaled
                });
                vertices.Add(new Vector2i {
                    x = x * blockSizeScaled, y = y * blockSizeScaled
                });
                vertices.Add(new Vector2i {
                    x = (x + 1) * blockSizeScaled, y = y * blockSizeScaled
                });
                vertices.Add(new Vector2i {
                    x = (x + 1) * blockSizeScaled, y = (y + 1) * blockSizeScaled
                });

                polygons.Add(vertices);

                int idx = x + resolutionX * y;

                DestructibleBlock block = CreateBlock();
                blocks[idx] = block;

                UpdateBlockBounds(x, y);

                block.UpdateGeometryWithMoreVertices(polygons, width, height, depth);
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        // First, check if the dig button has been hit
        if (Input.GetKeyDown(KeyCode.Space))
        {
            blocksAvailable += digChunk(digChunkRadius);
            playerHUD.blockCounter.SetCount(blocksAvailable / blocksPerBuild);
        }

        // Now, run the state machine for building
        switch (state)
        {
        case State.START:
            if (Input.GetMouseButtonDown(1))
            {
                /* Build button was pressed. Find out whene the mouse cursor is,
                 * and spawn our building block prefab there. */
                Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
                mousePos.z  = 0;
                placingWall = SpawnWall(mousePos);

                placingWallScript = placingWall.GetComponent <DestructibleBlock>();
                blocksPerBuild    = placingWallScript.numSubBlocks;
                subBlockSize      = placingWallScript.subBlockSize;
                origColor         = placingWallScript.getColor();
                Color newColor = inRangeColor;

                /* If not in building range, make sure to set sprite color appropriately */
                inRange = canPlaceWall(mousePos);
                if (!inRange)
                {
                    newColor = outOfRangeColor;
                }

                /* Move the block sprite to snap to sub-block size */
                Vector3 mPos = placingWall.transform.position;
                mPos.x = Mathf.Round(mPos.x / subBlockSize) * subBlockSize;
                mPos.y = Mathf.Round(mPos.y / subBlockSize) * subBlockSize;
                placingWall.transform.position = mPos;

                /* Collider is disabled and sprite is translucent until build button is released */
                newColor.a = 0.5f;
                placingWallScript.disableColliders();
                placingWallScript.setColor(newColor);

                /* Go to PLACING until the build button is released... */
                state = State.PLACING;
            }
            break;

        case State.PLACING:
            /* Move the building block prefab to keep it under the current mouse position */
            Vector3 newPos = cam.ScreenToWorldPoint(Input.mousePosition);
            newPos.x = Mathf.Round(newPos.x / subBlockSize) * subBlockSize;
            newPos.y = Mathf.Round(newPos.y / subBlockSize) * subBlockSize;
            newPos.z = 0;
            placingWall.transform.position = newPos;

            /* Set sprite color based on whether current mouse position is in building range */
            bool newInRange = canPlaceWall(newPos);
            if (newInRange != inRange)
            {
                inRange = newInRange;
                Color newColor = inRangeColor;

                if (!inRange)
                {
                    newColor = outOfRangeColor;
                }

                newColor.a = 0.5f;
                placingWallScript.setColor(newColor);
            }

            if (Input.GetMouseButtonUp(1))
            {
                /* Build button released. If the current mouse position is in
                 * range of the player, set the block sprite color + transparency
                 * back to normal and enable the block's collider again. Otherwise,
                 * destroy the block prefab, since it can't be placed. */
                if (inRange)
                {
                    placingWallScript.enableColliders();
                    Color newColor = origColor;
                    origColor.a = 1.0f;
                    placingWallScript.setColor(newColor);
                    blocksAvailable -= blocksPerBuild;
                    playerHUD.blockCounter.SetCount(blocksAvailable / blocksPerBuild);
                }
                else
                {
                    Destroy(placingWall);
                }

                placingWallScript = null;
                placingWall       = null;

                /* Back to the start state. */
                state = State.START;
            }
            break;
        }
    }
        public void Step()
        {
            if (Dead)
            {
                return;
            }
            var g = (MyGame)game;

            OldPosition = Position;
            Position   += Velocity * Time.deltaTime;

            var lineCollision = FindEarliestLineCollision();

            if (lineCollision != null)
            {
                if (bouncesLeft <= 0)
                {
                    Dead = true;
                }
                else
                {
                    bouncesLeft--;
                    ResolveCollision(lineCollision);
                }
            }

            // Destructible collisions
            //// LINES
            var linesToAdd = new List <DoubleDestructibleLineSegment>();

            foreach (var destructibleLine in g.DestructibleLines)
            {
                var(line1, line2) = CollisionUtils.BulletLineCollision(this, destructibleLine);
                if (line1 == null && line2 == null)
                {
                    continue;
                }
                destructibleLine.ShouldRemove = true;
                if (line1 != null)
                {
                    linesToAdd.Add(line1);
                }
                if (line2 != null)
                {
                    linesToAdd.Add(line2);
                }

                if (bouncesLeft <= 0)
                {
                    Dead = true;
                }
                else
                {
                    bouncesLeft--;
                }
            }

            linesToAdd.ForEach(line => {
                g.DestructibleLines.Add(line);
                g.AddChild(line);
            });

            //// CHUNKS
            foreach (var destructibleChunk in g.DestructibleChunks)
            {
                if (!CollisionUtils.BulletChunkCollision(this, destructibleChunk))
                {
                    continue;
                }
                destructibleChunk.ShouldRemove = true;
                Dead = true;
                break;
            }

            //// BLOCKS
            var chunksToAdd = new List <DestructibleChunk>();

            foreach (var destructibleBlock in g.DestructibleBlocks)
            {
                if (!CollisionUtils.BulletBlockCollision(this, destructibleBlock))
                {
                    continue;
                }
                destructibleBlock.ShouldRemove = true;
                var chunks = DestructibleBlock.Destruct(destructibleBlock);
                chunksToAdd.AddRange(chunks);
                Dead = true;
                break;
            }

            chunksToAdd.ForEach(chunk => {
                g.DestructibleChunks.Add(chunk);
                g.AddChild(chunk);
            });

            var availableTanks = new List <Tank>();

            availableTanks.AddRange(g.Enemies.Select(enemy => enemy.Tank));
            availableTanks.Add(g.Player.Tank);
            availableTanks.Remove(parentTank);
            var validColliders = availableTanks.SelectMany(tank => tank.Colliders);

            foreach (var circleCollider in validColliders)
            {
                var colliderParent = circleCollider.parent as Tank;
                var(worldPosition, worldOldPosition)     = CircleCollider.LocalToWorldCoords(circleCollider.Position, circleCollider.OldPosition, colliderParent.Position, colliderParent.OldPosition);
                var(rotatedPosition, rotatedOldPosition) = CircleCollider.ApplyRotation(worldPosition, colliderParent.Position, worldOldPosition, colliderParent.OldPosition, colliderParent.rotation);

                var collisionInfo = CollisionUtils.CircleCircleCollision(Position, OldPosition, Velocity * Time.deltaTime, Radius, rotatedPosition, circleCollider.Radius);
                if (collisionInfo == null)
                {
                    continue;
                }

                Dead = true;
                colliderParent.AIRef.Dead = true;
                break;
            }

            UpdateScreenPosition();
        }
예제 #9
0
        public void SpawnDestructibleBlock(Vector3 position)
        {
            DestructibleBlock _destructibleBlock = Instantiate(destructibleBlockPrefab, position, Quaternion.identity);

            ServerSend.Spawn(_destructibleBlock);
        }