gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
test/source/Game.java
| a9b2a64 | 1 | package source; |
| a9b2a64 | 2 | |
| a62d533 | 3 | import com.badlogic.gdx.scenes.scene2d.Actor; |
| a62d533 | 4 | import com.badlogic.gdx.scenes.scene2d.ui.Label; |
| a62d533 | 5 | import com.badlogic.gdx.scenes.scene2d.actions.Actions; |
| a9b2a64 | 6 | import scene2d.*; |
| a62d533 | 7 | |
| a62d533 | 8 | /** A Basic Game Scene for the Game |
| a62d533 | 9 | * <p> |
| a62d533 | 10 | * The Game Scene consists of all the characters, Huds, controllers for the game. All the logic for |
| a62d533 | 11 | * the game is written in the public void act(float delta) method. |
| a62d533 | 12 | * In this method we ckeck all the |
| a62d533 | 13 | * conditions for the player to win the level and changes scenes accordingly. |
| a62d533 | 14 | * <p> |
| a62d533 | 15 | * @author pyros2097 */ |
| a62d533 | 16 | public class Game extends Scene {
|
| a62d533 | 17 | Label timer; |
| a62d533 | 18 | float startTime; |
| a62d533 | 19 | float diff; |
| a62d533 | 20 | ImageJson player, enemy; |
| a62d533 | 21 | float playerSpeed = 3f; |
| a62d533 | 22 | boolean touchedDown = false; |
| a62d533 | 23 | String btnName = ""; |
| a62d533 | 24 | |
| a62d533 | 25 | public Game(){
|
| a9b2a64 | 26 | Camera.addHud("Up");
|
| a9b2a64 | 27 | Camera.addHud("Right");
|
| a9b2a64 | 28 | Camera.addHud("Left");
|
| a9b2a64 | 29 | Camera.addHud("Down");
|
| a9b2a64 | 30 | Camera.addHud("Timer");
|
| a9b2a64 | 31 | Camera.addHud("Label3");
|
| a9b2a64 | 32 | Camera.addHud("Fire");
|
| a62d533 | 33 | |
| a9b2a64 | 34 | Camera.setFollowSpeed(1f); |
| a9b2a64 | 35 | player = (ImageJson) findActor("Player");
|
| a9b2a64 | 36 | enemy = (ImageJson) findActor("Enemy");
|
| a9b2a64 | 37 | Camera.followActorContinuously(player); |
| a9b2a64 | 38 | timer = (Label)findActor("Timer");
|
| a9b2a64 | 39 | startTime = gameUptime; |
| a62d533 | 40 | } |
| a62d533 | 41 | |
| a62d533 | 42 | @Override |
| a62d533 | 43 | public void onTouchDown(Actor actor){
|
| a9b2a64 | 44 | log(actor.getName()); |
| a62d533 | 45 | touchedDown = true; |
| a62d533 | 46 | btnName = actor.getName(); |
| a62d533 | 47 | } |
| a62d533 | 48 | |
| a62d533 | 49 | @Override |
| a62d533 | 50 | public void onClick(Actor actor){
|
| a62d533 | 51 | if(actor.getName().equals("Back"))
|
| a9b2a64 | 52 | setScene("Menu");
|
| a62d533 | 53 | if(actor.getName().equals("Fire"))
|
| a62d533 | 54 | doFire(); |
| a62d533 | 55 | } |
| a62d533 | 56 | |
| a62d533 | 57 | void doFire(){
|
| a62d533 | 58 | ImageJson bullet = new ImageJson("771");
|
| a62d533 | 59 | bullet.setPosition(player.getX() + 50, player.getY() + 50); |
| a62d533 | 60 | bullet.setSize(25, 25); |
| a62d533 | 61 | bullet.addAction(Actions.sequence(Actions.moveBy(500, 0, 1f), Actions.removeActor(bullet))); |
| a9b2a64 | 62 | addActor(bullet); |
| a62d533 | 63 | } |
| a62d533 | 64 | |
| a62d533 | 65 | @Override |
| a62d533 | 66 | public void act(float delta){
|
| a9b2a64 | 67 | super.act(delta); |
| a9b2a64 | 68 | diff = gameUptime - startTime; |
| a62d533 | 69 | timer.setText("Timer: "+diff);
|
| a62d533 | 70 | if(diff>=30) |
| a9b2a64 | 71 | setScene("GameOver");
|
| a9b2a64 | 72 | if(collides(player, enemy)) |
| a9b2a64 | 73 | setScene("GameWin");
|
| a62d533 | 74 | //player.clearActions(); |
| a62d533 | 75 | switch(btnName){
|
| a62d533 | 76 | case "Right": player.addAction(Actions.moveBy(playerSpeed, 0, 0.1f)); break; |
| a62d533 | 77 | case "Up": player.addAction(Actions.moveBy(0, playerSpeed, 0.1f)); break; |
| a62d533 | 78 | case "Left": player.addAction(Actions.moveBy(-playerSpeed, 0, 0.1f)); break; |
| a62d533 | 79 | case "Down": player.addAction(Actions.moveBy(0, -playerSpeed, 0.1f)); break; |
| a62d533 | 80 | } |
| a62d533 | 81 | } |
| a62d533 | 82 | |
| a62d533 | 83 | @Override |
| a62d533 | 84 | public void onTouchUp(){
|
| a62d533 | 85 | touchedDown = false; |
| a62d533 | 86 | btnName = ""; |
| a62d533 | 87 | } |
| a62d533 | 88 | |
| a62d533 | 89 | @Override |
| a62d533 | 90 | public void onDragged(){}
|
| a62d533 | 91 | |
| a62d533 | 92 | @Override |
| a62d533 | 93 | public void onGesture(GestureType type){}
|
| a9b2a64 | 94 | |
| a9b2a64 | 95 | @Override |
| a9b2a64 | 96 | public void onKeyTyped(char key){
|
| a9b2a64 | 97 | } |
| a9b2a64 | 98 | |
| a9b2a64 | 99 | @Override |
| a9b2a64 | 100 | public void onKeyUp(int keycode){
|
| a9b2a64 | 101 | } |
| a9b2a64 | 102 | |
| a9b2a64 | 103 | @Override |
| a9b2a64 | 104 | public void onKeyDown(int keycode){
|
| a9b2a64 | 105 | } |
| a62d533 | 106 | |
| a62d533 | 107 | @Override |
| a62d533 | 108 | public void onPause(){
|
| a62d533 | 109 | } |
| a62d533 | 110 | |
| a62d533 | 111 | @Override |
| a62d533 | 112 | public void onResume(){
|
| a62d533 | 113 | } |
| a62d533 | 114 | |
| a62d533 | 115 | @Override |
| a62d533 | 116 | public void onDispose(){
|
| a62d533 | 117 | } |
| a62d533 | 118 | } |