private void AddTestObjects(int numObjects) { const int border = 40; Random r = new Random(); for (int i = 0; i < numObjects; ++i) { Quad egg = new Quad(); egg.Texture = textures[1]; egg.Width = textures[1].Width; egg.Height = textures[1].Height; egg.AlignPivot(HAlign.Center, VAlign.Center); egg.X = r.Next(border, (int)Stage.StageWidth - border); egg.Y = r.Next(border, (int)Stage.StageHeight - border); egg.Rotation = (float)(r.Next(0, 100) / 100.0f * Math.PI); _container.AddChild(egg); } }
/// <summary> /// Sets the screen up (UI components, multimedia content, etc.) /// </summary> public override void Initialize() { base.Initialize(); //Loads the spritesheet SpriteSheet spriteSheetChuck = ResourceManager.CreateSpriteSheet("chuckSpriteSheet"); //Create the Sprites Sprite spriteHead = new Sprite("head", spriteSheetChuck["head"]) { Pivot = Vector2.One / 2, Touchable = false }; Sprite spriteBody = new Sprite("body", spriteSheetChuck["body"]) { Pivot = Vector2.One / 2 }; Sprite spriteArmLeft1 = new Sprite("arm_left_1", spriteSheetChuck["arm_left_1"]) { Pivot = new Vector2(0.75f, 0.65f), Touchable = false }; Sprite spriteArmLeft2 = new Sprite("arm_left_2", spriteSheetChuck["arm_left_2"]) { Pivot = new Vector2(0.75f, 0.25f), Touchable = false }; Sprite spriteArmRight1 = new Sprite("arm_right_1", spriteSheetChuck["arm_right_1"]) { Pivot = new Vector2(0.75f, 0.35f), Touchable = false }; Sprite spriteArmRight2 = new Sprite("arm_right_2", spriteSheetChuck["arm_right_2"]) { Pivot = new Vector2(0.75f, 0.75f), Touchable = false }; Sprite spriteFootLeft = new Sprite("foot_left", spriteSheetChuck["foot_left"]) { Pivot = Vector2.One / 2, Touchable = false }; Sprite spriteFootRight = new Sprite("foot_right", spriteSheetChuck["foot_right"]) { Pivot = Vector2.One / 2, Touchable = false }; spriteBody.Draggable = true; // Sprite tree spriteBody.AddChild(spriteHead); spriteBody.AddChild(spriteArmLeft2); spriteBody.AddChild(spriteArmRight2); spriteBody.AddChild(spriteFootLeft); spriteBody.AddChild(spriteFootRight); spriteArmLeft2.AddChild(spriteArmLeft1); spriteArmRight2.AddChild(spriteArmRight1); // Adds the player to the screen AddComponent(spriteBody, 0.5f * Preferences.ViewportManager.VirtualWidth, 0.5f * Preferences.ViewportManager.VirtualHeight); // Positions spriteArmLeft2.Position = spriteBody.Position + Vector2.UnitY * spriteBody.Size.Y * spriteBody.Pivot.Y * 0.9f; spriteArmRight2.Position = spriteBody.Position - Vector2.UnitY * spriteBody.Size.Y * spriteBody.Pivot.Y * 0.9f; spriteArmLeft1.Position = spriteArmLeft2.Position - new Vector2(spriteArmLeft2.Size.X * spriteArmLeft2.Pivot.X, -spriteArmLeft2.Size.Y * spriteArmLeft2.Pivot.Y); spriteArmRight1.Position = spriteArmRight2.Position - new Vector2(spriteArmRight2.Size.X * spriteArmRight2.Pivot.X, spriteArmLeft2.Size.Y * spriteArmLeft2.Pivot.Y); spriteFootLeft.Position = spriteBody.Position + Vector2.UnitY * spriteBody.Size * spriteBody.Pivot * 0.3f - Vector2.UnitX * 10; spriteFootRight.Position = spriteBody.Position - Vector2.UnitY * spriteBody.Size * spriteBody.Pivot * 0.3f - Vector2.UnitX * 10; // ordered SendToFront(spriteHead); SendToFront(spriteArmLeft1); SendToFront(spriteArmRight1); // Rotation slider Slider sliderAngle = new Slider() { Pivot = new Vector2(0.5f, 1.0f) }; sliderAngle.ValueChangeEvent += (s) => { spriteBody.Rotation = MathHelper.ToRadians(sliderAngle.Value * 3.60f); }; AddComponent(sliderAngle, Preferences.ViewportManager.BottomCenterAnchor); ///////// DEFEND ANIMATION //////// // Animaciones Animation animationDefendHead = Animation.CreateAnimation(9); animationDefendHead.AnimationType = AnimationType.Relative; animationDefendHead.AddKey(new KeyFrame(0, Vector2.Zero)); animationDefendHead.AddKey(new KeyFrame(2, -Vector2.UnitX * 2)); animationDefendHead.AddKey(new KeyFrame(4, Vector2.Zero)); animationDefendHead.AddKey(new KeyFrame(6, Vector2.UnitX * 2)); animationDefendHead.AddKey(new KeyFrame(8, Vector2.Zero)); animationDefendHead.FramePerSeconds = 10; AddAnimation(animationDefendHead); //Some variables to use in animations float degree = 45f; int framesDefend = 30; int waitfps = 60; Animation animationDefendLeftArm = Animation.CreateAnimation(framesDefend); animationDefendLeftArm.AnimationType = AnimationType.Relative; animationDefendLeftArm.AddKey(new KeyFrame(0, Vector2.Zero, 0.0f, 1.0f)); animationDefendLeftArm.AddKey(new KeyFrame((int)(0.5f * framesDefend), Vector2.Zero, MathHelper.ToRadians(degree), 1.0f)); animationDefendLeftArm.AddKey(new KeyFrame(framesDefend - 1, Vector2.Zero, 0.0f, 1.0f)); animationDefendLeftArm.FramePerSeconds = waitfps; AddAnimation(animationDefendLeftArm); Animation animationDefendRightArm = Animation.CreateAnimation(framesDefend); animationDefendRightArm.AnimationType = AnimationType.Relative; animationDefendRightArm.AddKey(new KeyFrame(0, Vector2.Zero, 0.0f, 1.0f)); animationDefendRightArm.AddKey(new KeyFrame((int)(0.5f * framesDefend), Vector2.Zero, MathHelper.ToRadians(-degree), 1.0f)); animationDefendRightArm.AddKey(new KeyFrame(framesDefend - 1, Vector2.Zero, 0.0f, 1.0f)); animationDefendRightArm.FramePerSeconds = waitfps; AddAnimation(animationDefendRightArm); ///////// RIGHT ATTACK ANIMATION /////// int rpfps = 60; int frames = 20; // body Animation animationAttackPunchBody = Animation.CreateAnimation(frames); animationAttackPunchBody.AnimationType = AnimationType.Relative; animationAttackPunchBody.AddKey(new KeyFrame(0, Vector2.Zero, 0.0f, 1.0f)); animationAttackPunchBody.AddKey(new KeyFrame((int)(frames * 0.5), -Vector2.UnitX * 20, MathHelper.ToRadians(-85), 1.0f)); animationAttackPunchBody.AddKey(new KeyFrame(frames - 1, Vector2.Zero, 0.0f, 1.0f)); animationAttackPunchBody.FramePerSeconds = rpfps; AddAnimation(animationAttackPunchBody); // head Animation animationRightAttackHead = Animation.CreateAnimation(frames); animationRightAttackHead.AnimationType = AnimationType.Relative; animationRightAttackHead.AddKey(new KeyFrame(0, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackHead.AddKey(new KeyFrame((int)(frames * 0.5), Vector2.Zero, MathHelper.ToRadians(+85), 1.0f)); animationRightAttackHead.AddKey(new KeyFrame(frames - 1, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackHead.FramePerSeconds = rpfps; AddAnimation(animationRightAttackHead); // right arm Animation animationRightAttackRightArm = Animation.CreateAnimation(frames); animationRightAttackRightArm.AnimationType = AnimationType.Relative; animationRightAttackRightArm.AddKey(new KeyFrame(0, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackRightArm.AddKey(new KeyFrame((int)(frames * 0.5), Vector2.Zero, MathHelper.ToRadians(35), 1.0f)); animationRightAttackRightArm.AddKey(new KeyFrame(frames - 1, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackRightArm.FramePerSeconds = rpfps; AddAnimation(animationRightAttackRightArm); // right hand Animation animationRightAttackRightHand = Animation.CreateAnimation(frames); animationRightAttackRightHand.AnimationType = AnimationType.Relative; animationRightAttackRightHand.AddKey(new KeyFrame(0, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackRightHand.AddKey(new KeyFrame((int)(frames * 0.5), Vector2.Zero, MathHelper.ToRadians(45), 1.0f)); animationRightAttackRightHand.AddKey(new KeyFrame(frames - 1, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackRightHand.FramePerSeconds = rpfps; AddAnimation(animationRightAttackRightHand); // left arm Animation animationRightAttackLeftArm = Animation.CreateAnimation(frames); animationRightAttackLeftArm.AnimationType = AnimationType.Relative; animationRightAttackLeftArm.AddKey(new KeyFrame(0, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackLeftArm.AddKey(new KeyFrame((int)(frames * 0.5), Vector2.Zero, MathHelper.ToRadians(45), 1.0f)); animationRightAttackLeftArm.AddKey(new KeyFrame(frames - 1, Vector2.Zero, 0.0f, 1.0f)); animationRightAttackLeftArm.FramePerSeconds = rpfps; AddAnimation(animationRightAttackLeftArm); // attack Vector2 v = new Vector2(0, 0); Animation animationAttack = Animation.CreateAnimation(frames); animationAttack.AnimationType = AnimationType.Relative; animationAttack.AddKey(new KeyFrame(0, v, 0.0f, 1.0f)); animationAttack.AddKey(new KeyFrame((int)(frames * 0.5), v, 0.0f, 3.0f)); animationAttack.AddKey(new KeyFrame(frames - 1, v, 0.0f, 1.0f)); animationAttack.FramePerSeconds = rpfps; animationAttack.IsLooped = true; AddAnimation(animationAttack); Button btnDefend = new Button("Defend") { Pivot = new Vector2(0.5f, 0.0f) }; btnDefend.Released += (s) => { if (animationDefendRightArm.State == AnimationState.Stopped && animationAttackPunchBody.State == AnimationState.Stopped) { animationDefendHead.Play(spriteHead); animationDefendLeftArm.Play(spriteArmLeft2); animationDefendRightArm.Play(spriteArmRight2); } }; AddComponent(btnDefend, Preferences.ViewportManager.TopCenterAnchor); Button btnAttack = new Button("Attack") { Pivot = new Vector2(0.5f, 0.0f) }; btnAttack.Released += (s) => { if (animationAttackPunchBody.State == AnimationState.Stopped && animationDefendRightArm.State == AnimationState.Stopped) { animationAttackPunchBody.Play(spriteBody); animationRightAttackHead.Play(spriteHead); animationRightAttackRightArm.Play(spriteArmRight2); animationRightAttackRightHand.Play(spriteArmRight1); animationRightAttackLeftArm.Play(spriteArmLeft2); } }; AddComponent(btnAttack, btnDefend.Position + Vector2.UnitY * btnDefend.Size); }
/// <summary> /// Sets the screen up (UI components, multimedia content, etc.) /// </summary> public override void Initialize() { base.Initialize(); // Background SetBackground(ResourceManager.CreateImage("Images/background_ios"), Screen.Adjustment.CENTER); // Sprite sheet image SpriteSheet iSpriteSheet = ResourceManager.CreateSpriteSheet("Images/PigeonsAttackSpriteSheet"); // Pigeons Sprite[] lPigeons = new Sprite[5]; Image iPigeonLookingAtLeft = iSpriteSheet["pigeon_1"]; Image iPigeonLookingAtRight = iSpriteSheet["pigeon_2"]; random = new Random(); lPigeons[0] = new Sprite("pigeon", iPigeonLookingAtLeft); lPigeons[1] = new Sprite("pigeon", iPigeonLookingAtLeft); lPigeons[2] = new Sprite("pigeon", iPigeonLookingAtRight); lPigeons[3] = new Sprite("pigeon", iPigeonLookingAtLeft); lPigeons[4] = new Sprite("pigeon", iPigeonLookingAtRight); AddComponent(lPigeons[0], 79, 12); AddComponent(lPigeons[1], 207, 15); AddComponent(lPigeons[2], 345, 16); AddComponent(lPigeons[3], 495, 16); AddComponent(lPigeons[4], 634, 7); // Car cCar = new Sprite("cCar", Image.CreateImage(Color.Transparent, 238, 164)); // Car itself lCar = new Sprite("car", iSpriteSheet["car"]); cCar.AddChild(lCar); // Tires Image iTire = iSpriteSheet["steel"]; lLeftTire = new Sprite("lLeftTire", iTire) { Position = new Vector2(70, 138), Pivot = Vector2.One / 2}; cCar.AddChild(lLeftTire); lRightTire = new Sprite("lRightTire", iTire) { Position = new Vector2(162, 138), Pivot = Vector2.One / 2 }; cCar.AddChild(lRightTire); AddComponent(cCar, 11, 480 - 164 - 24); if (AccelerometerSensor.Instance.IsConnected) { accelerometerDetected = true; AccelerometerSensor.Instance.Start(); } #region Shits Image iShit = iSpriteSheet["shit"]; lShit = new Sprite("shit", iShit) { Pivot = Vector2.One / 2}; // The shit will be hidden since the very begining, so doesn't matter where to place it AddComponent(lShit, -100, -100); lShit.Visible = false; shitPositions = new Vector2[5]; shitPositions[0] = new Vector2(135, 63); shitPositions[1] = new Vector2(267, 70); shitPositions[2] = new Vector2(367, 70); shitPositions[3] = new Vector2(553, 70); shitPositions[4] = new Vector2(657, 63); elapsedTimeBetweenShits = TimeSpan.Zero; // 60 will cause the animation to take 1 s to complete aShitFalling = Animation.CreateAnimation(75); aShitFalling.AnimationType = AnimationType.Relative; aShitFalling.AddKey(new KeyFrame(0, Vector2.Zero)); aShitFalling.AddKey(new KeyFrame(aShitFalling.NumFrames - 1, new Vector2(0, 400))); // Once the shit touches the road it must dissapear aShitFalling.EndEvent += delegate { lShit.Visible = false; }; AddAnimation(aShitFalling); #endregion Shits // Semaphore lights lRed = new Sprite("red", iSpriteSheet["red"]); AddComponent(lRed, 716, 187); lAmber = new Sprite("amber", iSpriteSheet["amber"]); AddComponent(lAmber, 715, 215); lGreen = new Sprite("green", iSpriteSheet["green"]); AddComponent(lGreen, 717, 241); lAmber.Visible = lGreen.Visible = false; // This var will hold the playing time elapsedGameTime = TimeSpan.Zero; #if DEBUG lLog = new Label("N/A", Color.Black, Color.White); AddComponent(lLog, Preferences.ViewportManager.TopLeftAnchor); #endif impacts = 0; minClamp = 11; // The car won't overpass the semaphore until it turns green maxClamp = 545; Font fImpacts = ResourceManager.CreateFont("Fonts/spritefont_0_9"); lImpacts = new Label("0", Color.White, Color.Transparent) { Font = fImpacts}; AddComponent(lImpacts, 107, 155); lImpacts.Visible = false; lImpactsBrand = new Sprite("impacts", iSpriteSheet["impacts"]); AddComponent(lImpactsBrand, 197, 160); lImpactsBrand.Visible = false; }