Example #1
0
        public override void Update(double delta, BLevel level)
        {
            base.Update(delta, level);

            HandleMovement(delta);
            HandleCollision(level);
        }
Example #2
0
        public override void InitialiseLevel()
        {
            base.InitialiseLevel();

            // Initialising our textures (We can have up to 255 images. Make the most of this by using sprite sheets.)
            Textures[0] = BGraphics.LoadTexture("Textures/terrain_default.png");
            Textures[1] = BGraphics.LoadTexture("Textures/entities_default.png");

            // Initialising our blocks (We can have up to 254 blocks, id 1 to 254)
            Blocks.Initialise();

            // Initialising our level
            Level = new BLevel("test_map");
        }
Example #3
0
    // Start is called before the first frame update
    public virtual void Start()
    {
        speed          = UnityEngine.Random.Range(minSpeed, maxSpeed);
        followInterval = UnityEngine.Random.Range(followIntervalMin, followIntervalMax);

        var player = GameObject.FindGameObjectWithTag("Player");

        if (player)
        {
            playerTransform = player.GetComponent <RectTransform>();
        }
        bLevel = FindObjectOfType <BLevel>();

        bLevel.JoinLevel(enemyID);
        StartCoroutine(UpdateTargetPos());
    }
Example #4
0
        public void Update(BLevel level)
        {
            float xScale = ((float)width / (float)level.Width);
            float yScale = ((float)height / (float)level.Height);

            for (int x = 0; x < this.Width; x++)
            {
                for (int y = 0; y < this.Height; y++)
                {
                    // Handle entities
                    int levelX = (int)Math.Floor(x / xScale);
                    int levelY = (int)Math.Floor(y / yScale);
                    if (levelX < level.Width && levelY < level.Height)
                    {
                        if (level[levelX, levelY].IsSolid)
                        {
                            this[x, y].Obstructed = true;
                        }
                        else
                        {
                            this[x, y].Obstructed = false;
                        }
                    }

                    RectangleF gridTile = new RectangleF(x * tileSizeX, y * tileSizeY, tileSizeX, tileSizeY);
                    foreach (BEntity entity in level.Entities)
                    {
                        if (entity != null)
                        {
                            if (gridTile.IntersectsWith(new RectangleF(entity.CollisionBox.X + entity.position.X, entity.CollisionBox.Y + entity.position.Y, entity.CollisionBox.Width, entity.CollisionBox.Height)))
                            {
                                this[x, y].Obstructed = true;
                            }
                        }
                    }
                }
            }

            if (destNode != null && startNode != null)
            {
                FindPathTo(new Vector2(destNode.X * TileSizeX, destNode.Y * TileSizeY), new Vector2(startNode.X * TileSizeX, startNode.Y * TileSizeY));
            }
        }
Example #5
0
 public virtual void Update(double delta, BLevel level)
 {
 }
Example #6
0
        // TODO: Rework the collision. Characters get stuck to the side of collision boxes.
        public void HandleCollision(BLevel level)
        {
            int        minX        = (int)(Math.Floor((this.position.X - size.X / 2f) / AppInfo.GRIDSIZE));
            int        minY        = (int)(Math.Floor((this.position.Y - size.Y / 2f) / AppInfo.GRIDSIZE));
            int        maxX        = (int)(Math.Floor((this.position.X + size.X / 2f) / AppInfo.GRIDSIZE));
            int        maxY        = (int)(Math.Floor((this.position.Y + size.Y / 2f) / AppInfo.GRIDSIZE));
            RectangleF thisCollide = new RectangleF(position.X + CollisionBox.X, GroundLevel - CollisionBox.Height, CollisionBox.Width, CollisionBox.Height);

            for (int x = minX; x <= maxX; x++)
            {
                for (int y = minY; y <= maxY; y++)
                {
                    RectangleF blockRec = new RectangleF(x * AppInfo.GRIDSIZE, y * AppInfo.GRIDSIZE, AppInfo.GRIDSIZE, AppInfo.GRIDSIZE);

                    if (level[x, y].IsSolid && thisCollide.IntersectsWith(blockRec))
                    {
                        #region Resolve Collision

                        float[] depths = new float[4]
                        {
                            blockRec.Right - thisCollide.Left, // Pos X
                            blockRec.Bottom - thisCollide.Top, // Pos Y
                            thisCollide.Right - blockRec.Left, // Neg X
                            thisCollide.Bottom - blockRec.Top, // Neg Y
                        };

                        Point[] directions = new Point[4]
                        {
                            new Point(1, 0),
                            new Point(0, 1),
                            new Point(-1, 0),
                            new Point(0, -1),
                        };

                        float   min          = float.MaxValue;
                        Vector2 minDirection = Vector2.Zero;

                        for (int i = 0; i < 4; i++)
                        {
                            if (!level[x + directions[i].X, y + directions[i].Y].IsSolid &&
                                depths[i] < min)
                            {
                                min          = depths[i];
                                minDirection = new Vector2(directions[i].X, directions[i].Y);
                            }
                        }

                        if (min == float.MaxValue)
                        {
                            continue;
                        }

                        this.position += (minDirection * min);

                        if (this.velocity.X * minDirection.X < 0)
                        {
                            this.velocity.X = 0;
                        }
                        if (this.velocity.Y * minDirection.Y < 0)
                        {
                            this.velocity.Y = 0;
                        }
                        #endregion
                    }
                }
            }

            for (int e = 0; e < level.Entities.Count; e++)
            {
                if (level.Entities[e] != null)
                {
                    var entity          = level.Entities[e];
                    var entityCollision = new RectangleF(entity.position.X + entity.CollisionBox.X, entity.position.Y + entity.CollisionBox.Y, entity.CollisionBox.Width, entity.CollisionBox.Height);

                    if (thisCollide.IntersectsWith(entityCollision))
                    {
                        float[] depths = new float[4]
                        {
                            entityCollision.Right - thisCollide.Left, // Pos X
                            entityCollision.Bottom - thisCollide.Top, // Pos Y
                            thisCollide.Right - entityCollision.Left, // Neg X
                            thisCollide.Bottom - entityCollision.Top, // Neg Y
                        };

                        Point[] directions = new Point[4]
                        {
                            new Point(1, 0),
                            new Point(0, 1),
                            new Point(-1, 0),
                            new Point(0, -1),
                        };

                        float   min          = float.MaxValue;
                        Vector2 minDirection = Vector2.Zero;

                        for (int i = 0; i < 4; i++)
                        {
                            if (depths[i] < min)
                            {
                                min          = depths[i];
                                minDirection = new Vector2(directions[i].X, directions[i].Y);
                            }
                        }

                        if (min == float.MaxValue)
                        {
                            continue;
                        }

                        this.position += (minDirection * min);

                        if (this.velocity.X * minDirection.X < 0)
                        {
                            this.velocity.X = 0;
                        }
                        if (this.velocity.Y * minDirection.Y < 0)
                        {
                            this.velocity.Y = 0;
                        }
                    }
                }
            }
        }
Example #7
0
 // Start is called before the first frame update
 void Start()
 {
     spawnInterval = Random.Range(spawnIntervalMin, spawnIntervalMax);
     bLevel        = FindObjectOfType <BLevel>();
 }
Example #8
0
 public override void Update(double delta, BLevel level)
 {
     HandleInput();
     base.Update(delta, level);
 }
Example #9
0
        public override void Update(double delta, BLevel level)
        {
            base.Update(delta, level);

            UpdateSprite();
        }