package pong.alexjeffery.org; import pulpcore.Stage; import pulpcore.animation.Easing; import pulpcore.animation.Timeline; import pulpcore.image.CoreGraphics; import pulpcore.image.CoreImage; import pulpcore.scene.Scene2D; import pulpcore.sprite.Button; import pulpcore.sprite.FilledSprite; import pulpcore.sprite.Group; import pulpcore.sprite.Label; import pulpcore.sprite.Sprite; public class TitleScene extends Scene2D { Resources resources; Group sceneGroup; Timeline timeline; FilledSprite background; Label titleLabel; Label instructionsLabel; Button playButton; public void load() { //Get a hold of the resources helper class resources = Resources.Instance(); //Create a new timeline. This will be used for the titles animation timeline = new Timeline(); //Create a new group. This will be used to holg the GUI items for some simple GUI effects sceneGroup = new Group(); //Create a black background and add it the the scene CreateBackground(); //Create a animated title and add it the GUI group CreateTitle(); //Create a button to start a new game and add it to the GUI group CreatePlayButton(); //Create the instructions text and add to the GUI group CreateInstructions(); //Add the group containing all GUI items to the scene addLayer(sceneGroup); //Add the timeline that controls the GUI items to the scene addTimeline(timeline); //Start the time line animating timeline.play(); } public void update(int elapsedTime) { if(playButton.isClicked() == true) { //Start to swap scene timeline = new Timeline(); timeline.interruptScene(new PlayScene(), 1000); addTimeline(timeline); timeline.play(); //Animate the scene out and the new scene in sceneGroup.x.animateTo(Stage.getWidth() * -1, 2500, Easing.STRONG_OUT); } } private void CreateBackground() { //Create a new filled sprite background = new FilledSprite(CoreGraphics.BLACK); //Add the filled sprite to the scene //This is added as the first sprite so it will get painted first in every refresh add(background); } private void CreateTitle() { int x = Stage.getWidth() / 2; //Set to the middle of the screen int y = 75; int labelWidth = -1; // auto set width int labelHeight = 50; int animationLength = 1500; //Create a new label for the title titleLabel = new Label(resources.Font("Pong").tint(CoreGraphics.WHITE), "Pong", x, y, labelWidth, labelHeight); //Position the label by it's center titleLabel.setAnchor(Sprite.CENTER); //Hide the label initially titleLabel.visible.set(false); //Animate the title label //These lines will make the title labels width and height animate from 0 to their correct value //Creating a kind of pop and grow from the center effect timeline.animate(titleLabel.width, 0, titleLabel.width.getAsInt(), animationLength, Easing.ELASTIC_OUT, 0); timeline.animate(titleLabel.height, 0, labelHeight, animationLength, Easing.ELASTIC_OUT, 0); timeline.set(titleLabel.visible, true, 1); //Add the label to the group sceneGroup.add(titleLabel); } private void CreateInstructions() { int x = Stage.getWidth() / 2; //Set to the middle of the screen int y = Stage.getHeight() / 2 + 25; int labelWidth = -1; // auto set width int labelHeight = 25; int animationLength = 1500; //Create a new label for the title titleLabel = new Label(resources.Font("Pong").tint(CoreGraphics.WHITE), "Player one keys: (A, Z) Player two keys: (K, M)", x, y, labelWidth, labelHeight); //Position the label by it's center titleLabel.setAnchor(Sprite.CENTER); //Hide the label initially titleLabel.visible.set(false); //Animate the title label //These lines will make the title labels width and height animate from 0 to their correct value //Creating a kind of pop and grow from the center effect timeline.animate(titleLabel.width, 0, titleLabel.width.getAsInt(), animationLength, Easing.ELASTIC_OUT, 0); timeline.animate(titleLabel.height, 0, labelHeight, animationLength, Easing.ELASTIC_OUT, 0); timeline.set(titleLabel.visible, true, 1); //Add the label to the group sceneGroup.add(titleLabel); } private void CreatePlayButton() { int x = Stage.getWidth() / 2; //Set to the middle of the screen int y = 200; //Get the image for the button CoreImage image = resources.PlayButtonImage(); //Create the button playButton = new Button(image.split(3), 0, image.getWidth() / 3); playButton.x.set(x); playButton.y.set(y); //Position the button by it's center playButton.setAnchor(Sprite.CENTER); //Hide the play button playButton.visible.set(false); //Animate the button. //These lines will animate the play button so that it pops in the same manner as the title does. //How ever it is delayed slightly so it pops just after the title pops in int animationStartDelay = 150; int animationLength = 1500; timeline.animate(playButton.width, 0, playButton.width.get(), animationLength, Easing.ELASTIC_OUT, animationStartDelay); timeline.animate(playButton.height, 0, playButton.height.get(), animationLength, Easing.ELASTIC_OUT, animationStartDelay); timeline.set(playButton.visible, true, animationStartDelay); //Add the button to the scene sceneGroup.add(playButton); } }