Esempio n. 1
0
        public void Draw(CanvasContext2D context, int _x, int _y, int mapX, int mapY)
        {
            context.Save();
            context.Translate(_x + mapX * ZombieGameConfig.TileSize, _y + mapY * ZombieGameConfig.TileSize);

            /*
            context.Translate(ZombieGameConfig.TileSize / 2, ZombieGameConfig.TileSize / 2);
            //context.Rotate(fm);
            context.Translate(-ZombieGameConfig.TileSize / 2, -ZombieGameConfig.TileSize / 2);
            */
            context.DrawImage((CanvasElement) Image.Canvas, 0, 0);
            /*
            context.StrokeStyle = "red";
            context.StrokeRect(0, 0, ZombieGameConfig.TileSize, ZombieGameConfig.TileSize);

            switch (Collision) {
                case CollisionType.Full:
                    context.FillStyle = "rgba(233,12,22,0.6)";
                    context.FillRect(0, 0, ZombieGameConfig.TileSize, ZombieGameConfig.TileSize);
                    break;
                case CollisionType.RightHalf:
                    context.FillStyle = "rgba(233,12,22,0.6)";
                    context.FillRect(ZombieGameConfig.TileSize / 2, 0, ZombieGameConfig.TileSize / 2, ZombieGameConfig.TileSize);
                    break;
                case CollisionType.TopHalf:
                    context.FillStyle = "rgba(233,12,22,0.6)";
                    context.FillRect(0, 0, ZombieGameConfig.TileSize, ZombieGameConfig.TileSize / 2);
                    break;
                case CollisionType.LeftHalf:
                    context.FillStyle = "rgba(233,12,22,0.6)";
                    context.FillRect(0, 0, ZombieGameConfig.TileSize / 2, ZombieGameConfig.TileSize);
                    break;
                case CollisionType.BottomHalf:
                    context.FillStyle = "rgba(233,12,22,0.6)";
                    context.FillRect(0, ZombieGameConfig.TileSize / 2, ZombieGameConfig.TileSize, ZombieGameConfig.TileSize / 2);
                    break;
            }
            */ //todo enable when some sort of edit mode is enabled

            context.Restore();
        }
Esempio n. 2
0
        public override void Update(CanvasContext2D context)
        {
            RoadEvent roadEvent = null;
            int trackIndex = 0;
            while (trackIndex < _events.Length && _events[trackIndex].Distance <= _level.Position)
            {
                roadEvent = _events[trackIndex];
                trackIndex++;
            }

            float shift = 0;
            float curve = 0;

            for (int i = 0; i < RaceLevel.Lines; i++)
            {
                float distance = _level.Position + _level.DistanceTable[i];
                while ((trackIndex < _events.Length) && (_events[trackIndex].Distance <= distance))
                {
                    roadEvent = _events[trackIndex];
                    trackIndex++;
                }

                if (trackIndex < _events.Length)
                {
                    RoadEvent nextEvent = _events[trackIndex];

                    curve = (nextEvent.Curve - roadEvent.Curve) * (distance - roadEvent.Distance) / (nextEvent.Distance - roadEvent.Distance) + roadEvent.Curve;
                }

                long index = Math.Round(distance / 2000) % 2;

                if (i > 0) shift += curve * (_level.DistanceTable[i + 1] - _level.DistanceTable[i]);

                _level.Curves[i] = curve;
                _level.Shifts[i] = Math.Round(((299 - i) * _level.Shift) / RaceLevel.Lines) - 25 + shift;

                context.DrawImage(_road[index], 0, 299 - i, 850, 1, _level.Shifts[i], 599 - i, 850, 1);

                if (i == 0) _level.Curve = curve;
            }
        }
Esempio n. 3
0
        protected override void Update(CanvasContext2D context)
        {
            if (Status != RaceStatus.Fail && Status != RaceStatus.Win)
            {
                context.DrawImage(_timeLeftFrame, 308, 10);

                Type.SetField(context, "textAlign", "right");
                context.FillStyle = "#00AD11";

                if (TimeLeft > 10000 || Math.Floor((TimeLeft / 300) % 2) != 0)
                {
                    if (TimeLeft < 0) TimeLeft = 0;
                    context.Font = "110px Digital";
                    context.FillText(Math.Floor(TimeLeft / 1000).ToString(), 475, 105);
                }

                if (Speed > 0)
                {
                    context.Save();
                    context.Scale(-1, 1);
                    long width = Math.Floor((10 * Speed) / MaxSpeed) * 22;
                    if (width > 0) context.DrawImage(_meterImage, 220 - width, 0, width, 102, -561 - width, 20, width, 102);
                    context.Restore();
                }

                context.Font = "30px Digital";
                context.FillText(Math.Floor(Speed * 5) + " Km/h", 780, 120);

                int rpmWidth = Math.Floor(_rpm / 500) * 22 + 22;

                context.DrawImage(_meterImage, 220 - rpmWidth, 0, rpmWidth, 102, 240 - rpmWidth, 20, rpmWidth, 102);
                context.FillText(Math.Floor(_rpm) + " RPM", 130, 120);

                context.BeginPath();
                context.LineWidth = 3;
                context.StrokeStyle = "#00AD11";
                context.MoveTo(5, 150);
                float x = 5 + Math.Min(Position * 735 / RoadLength, 735);
                context.LineTo(x, 150);
                context.Stroke();
                context.ClosePath();

                context.BeginPath();
                context.StrokeStyle = "#006808";
                context.MoveTo(x, 150);
                context.LineTo(790, 150);
                context.Stroke();
                context.ClosePath();
                context.DrawImage(_markerImage, x, 142);
            }

            if (Status == RaceStatus.Running)
            {
                TimeLeft -= DeltaTime;

                if (TimeLeft < 0)
                {
                    _music.Pause();
                    CarSystem.CarObject.CurrentAnimation = "Forward";
                    Status = RaceStatus.Fail;
                    ShowMessage(_failMessage);
                    RemoveSystem(_engineSoundSystem);
                    RemoveSystem(_npcSystem);
                    pendingTimers.Add(Window.SetTimeout(delegate()
                    {
                        UpdateMessage("<p>Press a key to continue.</p>");
                    }, 3000));
                }
            }
        }
Esempio n. 4
0
        protected override void PreUpdate(CanvasContext2D context)
        {
            // Draw background
            context.DrawImage(_backgroundImage, 0, 0);

            switch (Status)
            {
                case RaceStatus.Running:
                    // Handle left and right turns
                    if (Speed > 0)
                    {
                        float increment = (60 - Math.Max(Speed, 20)) / 80;

                        if (Left)
                        {
                            Shift += increment * DeltaTime;
                            CarSystem.CarObject.CurrentAnimation = Down ? "b-Left" : "Left";
                        }
                        if (Right)
                        {
                            Shift -= increment * DeltaTime;
                            CarSystem.CarObject.CurrentAnimation = Down ? "b-Right" : "Right";
                        }
                    }

                    if (!(Left ^ Right)) CarSystem.CarObject.CurrentAnimation = Down ? "b-Forward" : "Forward";

                    // Handle acceleration, braking and inertia
                    if (Down) Speed -= 0.4f;
                    else if (Up) Speed += 0.3f;
                    else Speed -= 0.1f;

                    if (Up) _rpm += 40;
                    else _rpm -= 40;
                    if (_rpm > 4500) _rpm = 4500;
                    else if (_rpm < 200) _rpm = 200;

                    // When driving off the road
                    if (Math.Abs(Shift) > 350)
                    {
                        Speed *= 0.95f;

                        if (Math.Abs(Shift) > 450)
                            Shift = (Shift / Math.Abs(Shift)) * 450;
                    }
                    break;

                case RaceStatus.Win:
                case RaceStatus.Fail:
                    Speed -= 1;
                    _rpm -= 40;
                    break;

                case RaceStatus.Crashing:
                    Speed -= 0.3f;
                    Shift -= Shift * DeltaTime / 1000;
                    break;
            }

            // Speed capping
            if (Speed > MaxSpeed) Speed = MaxSpeed;
            if (Speed < 0) Speed = 0;

            // Calculating new position
            Position += Speed * DeltaTime;

            // Drift in turns
            Shift += Curve * Speed * 150;

            if (Position >= RoadLength && Status == RaceStatus.Running)
            {
                _music.Pause();
                Status = RaceStatus.Win;
                CarSystem.CarObject.StartAnimation("Skid");
                ShowMessage(_winMessage);
                RemoveSystem(_engineSoundSystem);
                RemoveSystem(_npcSystem);

                int timeLeft = Math.Floor(TimeLeft / 1000);
                CurrentGame.Score += 1000 + timeLeft * 500;

                //if (!_practice)
                //{
                //    pendingTimers.Add(
                //    Window.SetTimeout(delegate()
                //    {
                //        UpdateMessage("<p>Time Left: " + timeLeft + "</p>");
                //    }, 1500));

                //    pendingTimers.Add(
                //    Window.SetTimeout(delegate()
                //    {
                //        UpdateMessage("<p>Score: " + CurrentGame.Score + "</p>");
                //    }, 2500));
                //}

                pendingTimers.Add(
                Window.SetTimeout(delegate()
                {
                    UpdateMessage("<p>Press a key to continue.</p>");
                }, 3000));
            }
        }
        private bool DrawTriangle(CanvasContext2D ctx, ImageElement im, double x0, double y0, double x1, double y1, double x2, double y2,
                                double sx0, double sy0, double sx1, double sy1, double sx2, double sy2)
        {
            bool inside;

            if (factor == 1.0)
            {
                inside = Intersects(0, Width, 0, Height, x0, y0, x1, y1, x2, y2);
            }
            else
            {
                hw = Width / 2;
                qw = hw * factor;
                hh = Height / 2;
                qh = hh * factor;
                inside = Intersects(hw - qw, hw + qw, hh - qh, hh + qh, x0, y0, x1, y1, x2, y2);
            }

            if (!inside)
            {
                return false;
            }

            double edgeOffset = isOutlined ? ContractionInPixels : ExpansionInPixels;
            Vector2d expandedS0 = GetMiterPoint(Vector2d.Create(x0, y0), Vector2d.Create(x1, y1), Vector2d.Create(x2, y2), edgeOffset);
            Vector2d expandedS1 = GetMiterPoint(Vector2d.Create(x1, y1), Vector2d.Create(x0, y0), Vector2d.Create(x2, y2), edgeOffset);
            Vector2d expandedS2 = GetMiterPoint(Vector2d.Create(x2, y2), Vector2d.Create(x1, y1), Vector2d.Create(x0, y0), edgeOffset);

            x0 = expandedS0.X;
            y0 = expandedS0.Y;
            x1 = expandedS1.X;
            y1 = expandedS1.Y;
            x2 = expandedS2.X;
            y2 = expandedS2.Y;

            ctx.Save();
            ctx.BeginPath();
            ctx.MoveTo(x0, y0);
            ctx.LineTo(x1, y1);
            ctx.LineTo(x2, y2);
            ctx.ClosePath();
            ctx.Clip();

            double denom = sx0 * (sy2 - sy1) - sx1 * sy2 + sx2 * sy1 + (sx1 - sx2) * sy0;
            if (denom == 0)
            {
                ctx.Restore();
                return false;
            }
            double m11 = -(sy0 * (x2 - x1) - sy1 * x2 + sy2 * x1 + (sy1 - sy2) * x0) / denom;
            double m12 = (sy1 * y2 + sy0 * (y1 - y2) - sy2 * y1 + (sy2 - sy1) * y0) / denom;
            double m21 = (sx0 * (x2 - x1) - sx1 * x2 + sx2 * x1 + (sx1 - sx2) * x0) / denom;
            double m22 = -(sx1 * y2 + sx0 * (y1 - y2) - sx2 * y1 + (sx2 - sx1) * y0) / denom;
            double dx = (sx0 * (sy2 * x1 - sy1 * x2) + sy0 * (sx1 * x2 - sx2 * x1) + (sx2 * sy1 - sx1 * sy2) * x0) / denom;
            double dy = (sx0 * (sy2 * y1 - sy1 * y2) + sy0 * (sx1 * y2 - sx2 * y1) + (sx2 * sy1 - sx1 * sy2) * y0) / denom;

            ctx.Transform(m11, m12, m21, m22, dx, dy);

            ctx.DrawImage(im, 0, 0);

            ctx.Restore();

            if (factor != 1.0)
            {
                ctx.BeginPath();
                ctx.MoveTo(hw - qw, hh - qh);
                ctx.LineTo(hw + qw, hh - qh);
                ctx.LineTo(hw + qw, hh + qh);
                ctx.LineTo(hw - qw, hh + qh);
                ctx.ClosePath();
                ctx.StrokeStyle = "yellow";
                ctx.Stroke();

            }

            return true;
        }
Esempio n. 6
0
        public void Draw(CanvasContext2D context)
        {
            if (!Visible || Script.IsNullOrUndefined(CurrentAnimation)) return;

            Sprite sprite = AnimationSequences[CurrentAnimation].Sprites[CurrentFrame];

            if (Scale == 1)
            {
                context.DrawImage(sprite.Image, Location.X - sprite.HandleX, Location.Y - sprite.HandleY);
            }
            else
            {
                context.DrawImage(sprite.Image,
                    Location.X - sprite.HandleX * Scale,
                    Location.Y - sprite.HandleY * Scale,
                    sprite.Image.NaturalWidth * Scale,
                    sprite.Image.NaturalHeight * Scale);
            }
        }
Esempio n. 7
0
 protected override void Update(CanvasContext2D context)
 {
     context.DrawImage(_titleImage, (800 - _titleImage.NaturalWidth) / 2, 90 + Math.Cos(Ticks / 1000) * 20);
 }
Esempio n. 8
0
 private static void drawImage(CanvasContext2D context, CanvasInformation inf, double size)
 {
     if (inf.ImageReady)
         context.DrawImage(inf.Image, 0, 0);
     else context.DrawImage(inf.Canvas, 0, 0);
 }
Esempio n. 9
0
 protected override void PreUpdate(CanvasContext2D context)
 {
     context.DrawImage(_backgroundImage, 0, 0);
     Position += BaseSpeed * DeltaTime;
 }
 public override void Update(CanvasContext2D context)
 {
     context.DrawImage(_backgroundImage, 0, 0);
 }
Esempio n. 11
0
        private void UpdateExplosions(CanvasContext2D context, float speed)
        {
            if (Math.Random() * 100 < 2)
            {
                Explosion explosion = new Explosion();
                explosion.X = Math.Random() * 800;
                explosion.start = DateTime.Now;
                _explosions.Add(explosion);
            }

            Explosion toDelete = null;

            foreach (Explosion explosion in _explosions)
            {
                explosion.X = explosion.X - (0.3f * speed);
                double y = 240 + (DateTime.Now - explosion.start) * 0.5;
                context.DrawImage(_explosionImage, explosion.X - 133, y);
                if (y >= 319) toDelete = explosion;
            }

            if (toDelete != null) _explosions.Remove(toDelete);
        }
Esempio n. 12
0
        public override void Update(CanvasContext2D context)
        {
            context.Save();
            context.BeginPath();
            context.Rect(0, 0, 800, 320);
            context.ClosePath();
            context.Clip();

            float speed = -(_level.Curve * _level.Speed * 50);
            if (_level.Left && _level.Speed > 0) speed += 1;
            if (_level.Right && _level.Speed > 0) speed -= 1;

            UpdateExplosions(context, speed);

            UpdateFlak(context, speed);

            foreach (Cloud cloud in _clouds)
            {
                float scale = (400 - cloud.Y) / 400;

                cloud.X = (cloud.X - (0.3f - speed) * scale + 2400) % 2400;

                context.DrawImage(
                    _cloudImage,
                    cloud.X - 1200,
                    cloud.Y - (_cloudImage.NaturalHeight / 2) * scale,
                    _cloudImage.NaturalWidth * scale,
                    _cloudImage.NaturalHeight * scale);
            }
            context.Restore();
        }
Esempio n. 13
0
        private void UpdateFlak(CanvasContext2D context, float speed)
        {
            if (Math.Random() * 100 < 2)
            {
                float x = Math.Random() * 800;
                float startAngle = Math.Random() * 2 - 3;
                float interval = 0.08f - Math.Random() / 100;

                for (int i = 0; i < 5 + Math.Floor(Math.Random() * 8); i++)
                {
                    _flakObjects.Add(new FlakObject(startAngle + i * interval, x, i * 400));
                }
            }

            List<FlakObject> toRemove = new List<FlakObject>();

            foreach (FlakObject flakObject in _flakObjects)
            {
                if (flakObject.Update()) toRemove.Add(flakObject);
                flakObject.X += 0.3f * speed;
                context.DrawImage(_flakImage, flakObject.X, flakObject.Y);
            }

            foreach (FlakObject flakObject in toRemove) _flakObjects.Remove(flakObject);
        }
Esempio n. 14
0
        public override void Draw(CanvasContext2D context)
        {
            context.FillStyle = "red";
            context.FillRect(100, 100, 200, 200);

            context.DrawImage(someImage, 250, 250);
            context.DrawImage(someImage, 350, 350, 100, 100, 200, 200, 100, 100);

            for (int i = 0; i < DebugText.Length; i++) {
                if (DebugText[i].Truthy()) {
                    context.Save();
                    context.StrokeStyle = "white";
                    context.StrokeText(DebugText[i].ToString(), Screen.Width - 120, i * 20 + 150);
                    context.Restore();
                }
            }
        }
Esempio n. 15
0
        private bool DrawTriangle(CanvasContext2D ctx, ImageElement im, double x0, double y0, double x1, double y1, double x2, double y2,
                                double sx0, double sy0, double sx1, double sy1, double sx2, double sy2)
        {
            if (!Intersects(0, Width, 0, Height, x0, y0, x1, y1, x2, y2))
            {
                return false;
            }

            //double edgeOffset = isOutlined ? ContractionInPixels : ExpansionInPixels;
            //Vector2d expandedS0 = GetMiterPoint(Vector2d.Create(x0, y0), Vector2d.Create(x1, y1), Vector2d.Create(x2, y2), ExpansionInPixels);
            //Vector2d expandedS1 = GetMiterPoint(Vector2d.Create(x1, y1), Vector2d.Create(x0, y0), Vector2d.Create(x2, y2), ExpansionInPixels);
            //Vector2d expandedS2 = GetMiterPoint(Vector2d.Create(x2, y2), Vector2d.Create(x1, y1), Vector2d.Create(x0, y0), ExpansionInPixels);

            //Vector2d expandedS0 = MiterPoint(x0, y0, x1, y1, x2, y2);
            //Vector2d expandedS1 = MiterPoint(x1, y1, x0, y0, x2, y2);
            //Vector2d expandedS2 = MiterPoint(x2, y2, x1, y1, x0, y0);
            MiterPointOut(expandedS0, x0, y0, x1, y1, x2, y2, ExpansionInPixels);
            MiterPointOut(expandedS1, x1, y1, x0, y0, x2, y2, ExpansionInPixels);
            MiterPointOut(expandedS2, x2, y2, x1, y1, x0, y0, ExpansionInPixels);

            x0 = expandedS0.X;
            y0 = expandedS0.Y;
            x1 = expandedS1.X;
            y1 = expandedS1.Y;
            x2 = expandedS2.X;
            y2 = expandedS2.Y;

            ctx.Save();
            if (RenderingOn)
            {

                ctx.BeginPath();
                ctx.MoveTo(x0, y0);
                ctx.LineTo(x1, y1);
                ctx.LineTo(x2, y2);
                ctx.ClosePath();
                ctx.Clip();
            }
            double denom = sx0 * (sy2 - sy1) - sx1 * sy2 + sx2 * sy1 + (sx1 - sx2) * sy0;
            //if (denom == 0)
            //{
            //    ctx.Restore();
            //    return false;
            //}
            double m11 = -(sy0 * (x2 - x1) - sy1 * x2 + sy2 * x1 + (sy1 - sy2) * x0) / denom;
            double m12 = (sy1 * y2 + sy0 * (y1 - y2) - sy2 * y1 + (sy2 - sy1) * y0) / denom;
            double m21 = (sx0 * (x2 - x1) - sx1 * x2 + sx2 * x1 + (sx1 - sx2) * x0) / denom;
            double m22 = -(sx1 * y2 + sx0 * (y1 - y2) - sx2 * y1 + (sx2 - sx1) * y0) / denom;
            double dx = (sx0 * (sy2 * x1 - sy1 * x2) + sy0 * (sx1 * x2 - sx2 * x1) + (sx2 * sy1 - sx1 * sy2) * x0) / denom;
            double dy = (sx0 * (sy2 * y1 - sy1 * y2) + sy0 * (sx1 * y2 - sx2 * y1) + (sx2 * sy1 - sx1 * sy2) * y0) / denom;

            ctx.Transform(m11, m12, m21, m22, dx, dy);

            if (RenderingOn)
            {
                ctx.Alpha = Opacity;

                if (lighting < 1.0)
                {
                    ctx.Alpha = 1;
                    ctx.FillStyle = "Black";
                    ctx.FillRect(0, 0, Width, Height);
                    ctx.Alpha = lighting * Opacity;
                }

                ctx.DrawImage(im, 0, 0);
            }

            ctx.Restore();
            return true;
        }