示例#1
0
        public void LoadResource(Game game)
        {
            Player p1 = new Player();

            p1.Transformation.Position.X = 300;
            p1.Transformation.Position.Y = 300;
            SpriteComponent sprite = new SpriteComponent(p1.Transformation, 3);
            ThrustComponent thrust = new ThrustComponent(2);

            p1.AddComponent(thrust);
            p1.AddComponent(sprite);
            thrust.GenerateParticles(600);
            Texture texture = game.LoadTexture(@"C:\Source\escape-aliens\Resources\SpaceShipRed.png");

            SDL.SDL_Rect renderRect;
            SDL.SDL_Rect sourceRect;
            renderRect.x = 0;
            renderRect.y = 0;
            renderRect.w = 80;
            renderRect.h = 80;

            sourceRect.w = 167;
            sourceRect.h = 170;
            sourceRect.x = 0;
            sourceRect.y = 0;

            texture.RenderRectangle = renderRect;
            texture.SourceRectangle = sourceRect;
            sprite.AddAnimationFrame(texture);

            game.Input.KeyboardBindings.AddMapping(SDL.SDL_Scancode.SDL_SCANCODE_D, p1.RotateRight);
            game.Input.KeyboardBindings.AddMapping(SDL.SDL_Scancode.SDL_SCANCODE_A, p1.RotateLeft);
            game.Input.KeyboardBindings.AddMapping(SDL.SDL_Scancode.SDL_SCANCODE_W, p1.Forward);
            game.AddObject(p1);

            Polygon2D poly = new Polygon2D();

            poly.AddPoint(300, 300);
            poly.AddPoint(600, 400);
            poly.AddPoint(400, 500);
            Texture         polyText      = game.LoadTexture(@"C:\Source\escape-aliens\Resources\MapForeground.png");
            FilledPolygon2D filledPolygon = new FilledPolygon2D(poly, polyText);
            Asteroid        asteroid      = new Asteroid();

            asteroid.AddComponent(filledPolygon);
            game.AddObject(asteroid);
        }
        public override void Process(Entity entity)
        {
            ThrustComponent   tc = entity.GetComponent <ThrustComponent>();
            RotationComponent rc = entity.GetComponent <RotationComponent>();

            if (InputManager.Instance.isKeyReleased(Keys.Space))
            {
                PositionComponent pc    = entity.GetComponent <PositionComponent>();
                InertiaComponent  ic    = entity.GetComponent <InertiaComponent>();
                Entity            laser = this.EntityWorld.CreateEntity();

                laser.AddComponent(new ProjectileComponent(15, new TimeSpan(0, 0, 0, 0, 450)));
                laser.AddComponent(new PositionComponent(pc.Position));
                laser.AddComponent(new RotationComponent(rc.Angle, 0f, false));
                float radians = MathHelper.ToRadians(rc.Angle);
                laser.AddComponent(new InertiaComponent(Vector2.Multiply(Vector2.Normalize(new Vector2((float)Math.Sin(radians), -(float)Math.Cos(radians))), laser.GetComponent <ProjectileComponent>().MaxSpeed)));
                laser.AddComponent(new TextureComponent(new Coord(0, 0), "lasers", new Vector2(0, 0)));
            }
        }
示例#3
0
        public override void Process(Entity entity)
        {
            ThrustComponent tc = entity.GetComponent <ThrustComponent>();

            if (tc.ForwardThrust)
            {
                InertiaComponent  ic = entity.GetComponent <InertiaComponent>();
                RotationComponent rc = entity.GetComponent <RotationComponent>();

                double angleInDegrees = MathHelper.ToRadians(rc.Angle);
                ic.Inertia.X += (float)Math.Sin(angleInDegrees) * tc.Acceleration;
                ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * tc.Acceleration;

                if (ic.Inertia.Length() > tc.MaxSpeed)
                {
                    ic.Inertia.Normalize();
                    ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed);
                }
            }

            if (tc.BackwardThrust)
            {
                InertiaComponent  ic = entity.GetComponent <InertiaComponent>();
                RotationComponent rc = entity.GetComponent <RotationComponent>();

                float currentSpeed = ic.Inertia.Length();

                ic.Inertia = Vector2.Multiply(ic.Inertia, tc.Deceleration);

                if (ic.Inertia.Length() < tc.Acceleration)
                {
                    ic.Inertia.X = 0;
                    ic.Inertia.Y = 0;
                }
            }

            if (tc.LeftThrust)
            {
                InertiaComponent  ic = entity.GetComponent <InertiaComponent>();
                RotationComponent rc = entity.GetComponent <RotationComponent>();

                double angleInDegrees = MathHelper.ToRadians(rc.Angle - 90); // 90 degrees anti-clockwise
                ic.Inertia.X += (float)Math.Sin(angleInDegrees) * 0.05f;
                ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * 0.05f;

                if (ic.Inertia.Length() > tc.MaxSpeed)
                {
                    ic.Inertia.Normalize();
                    ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed);
                }
            }

            if (tc.RightThrust)
            {
                InertiaComponent  ic = entity.GetComponent <InertiaComponent>();
                RotationComponent rc = entity.GetComponent <RotationComponent>();

                double angleInDegrees = MathHelper.ToRadians(rc.Angle + 90); // 90 degrees clockwise
                ic.Inertia.X += (float)Math.Sin(angleInDegrees) * 0.05f;
                ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * 0.05f;

                if (ic.Inertia.Length() > tc.MaxSpeed)
                {
                    ic.Inertia.Normalize();
                    ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed);
                }
            }
        }
示例#4
0
        public override void Process(Entity entity)
        {
            NavigationComponent nc = entity.GetComponent <NavigationComponent>();

            if (nc.TargetLocation.HasValue)
            {
                PositionComponent pc               = entity.GetComponent <PositionComponent>();
                float             deltaX           = pc.Position.X - nc.TargetLocation.Value.X;
                float             deltaY           = pc.Position.Y - nc.TargetLocation.Value.Y;
                float             distanceToTarget = (float)Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY));

                if (distanceToTarget < 15)
                {
                    nc.TargetLocation = null;
                    entity.GetComponent <ThrustComponent>().ForwardThrust  = false;
                    entity.GetComponent <ThrustComponent>().BackwardThrust = true;
                    return;
                }

                float angle        = (float)Math.Atan2((nc.TargetLocation.Value.Y - pc.Position.Y), (nc.TargetLocation.Value.X - pc.Position.X)) + MathHelper.PiOver2;
                float angleDegrees = MathHelper.ToDegrees(angle);
                if (angleDegrees < 0)
                {
                    angleDegrees += 360;
                }

                RotationComponent rc = entity.GetComponent <RotationComponent>();

                float angleDiff = angleDegrees - rc.Angle;

                if (angleDiff > 180)
                {
                    angleDiff -= 360;
                }
                if (angleDiff < -180)
                {
                    angleDiff += 360;
                }

                if (angleDiff < -rc.RotationSpeed)
                {
                    entity.GetComponent <ThrustComponent>().ForwardThrust  = false;
                    entity.GetComponent <ThrustComponent>().BackwardThrust = true;
                    rc.Angle -= rc.RotationSpeed;
                    if (rc.Angle < 0)
                    {
                        rc.Angle += 360;
                    }
                }
                else if (angleDiff > rc.RotationSpeed)
                {
                    entity.GetComponent <ThrustComponent>().ForwardThrust  = false;
                    entity.GetComponent <ThrustComponent>().BackwardThrust = true;
                    rc.Angle += rc.RotationSpeed;
                    if (rc.Angle > 360)
                    {
                        rc.Angle -= 360;
                    }
                }
                else
                {
                    ThrustComponent  tc = entity.GetComponent <ThrustComponent>();
                    InertiaComponent ic = entity.GetComponent <InertiaComponent>();

                    float ticksToTarget = distanceToTarget / ic.Inertia.Length();
                    float ticksToStop   = (float)Math.Log(ic.Inertia.Length() / tc.Deceleration) / tc.Deceleration;

                    if (ticksToTarget > ticksToStop + 15)
                    {
                        tc.ForwardThrust  = true;
                        tc.BackwardThrust = false;
                    }
                    else
                    {
                        tc.ForwardThrust  = false;
                        tc.BackwardThrust = true;
                    }
                }
            }
            else
            {
                if (entity.GetComponent <InertiaComponent>().Inertia.Length() == 0)
                {
                    entity.GetComponent <ThrustComponent>().ForwardThrust  = false;
                    entity.GetComponent <ThrustComponent>().BackwardThrust = false;
                }
            }
        }