├── settings.gradle ├── README.md ├── android ├── assets │ ├── badlogic.jpg │ ├── Mario GFX │ │ ├── goomba.png │ │ ├── turtle.png │ │ ├── fireball.png │ │ ├── mushroom.png │ │ ├── big_mario.png │ │ └── little_mario.png │ ├── tileset_gutter.png │ ├── Mario_and_Enemies.png │ ├── audio │ │ ├── sounds │ │ │ ├── bump.wav │ │ │ ├── coin.wav │ │ │ ├── stomp.wav │ │ │ ├── mariodie.wav │ │ │ ├── powerup.wav │ │ │ ├── breakblock.wav │ │ │ ├── powerdown.wav │ │ │ └── powerup_spawn.wav │ │ └── music │ │ │ └── mario_music.ogg │ ├── Mario_and_Enemies.pack │ └── level1.tmx ├── ic_launcher-web.png ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ └── values │ │ ├── strings.xml │ │ └── styles.xml ├── project.properties ├── src │ └── com │ │ └── brentaureli │ │ └── mariobros │ │ └── android │ │ └── AndroidLauncher.java ├── AndroidManifest.xml ├── proguard-project.txt └── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── core ├── build.gradle └── src │ └── com │ └── brentaureli │ └── mariobros │ ├── Sprites │ ├── Items │ │ ├── ItemDef.java │ │ ├── Item.java │ │ └── Mushroom.java │ ├── TileObjects │ │ ├── Brick.java │ │ ├── Coin.java │ │ └── InteractiveTileObject.java │ ├── Enemies │ │ ├── Enemy.java │ │ ├── Goomba.java │ │ └── Turtle.java │ ├── Other │ │ └── FireBall.java │ └── Mario.java │ ├── MarioBros.java │ ├── Screens │ ├── GameOverScreen.java │ └── PlayScreen.java │ ├── Scenes │ └── Hud.java │ └── Tools │ ├── WorldContactListener.java │ └── B2WorldCreator.java ├── desktop ├── src │ └── com │ │ └── brentaureli │ │ └── mariobros │ │ └── desktop │ │ └── DesktopLauncher.java └── build.gradle ├── .gitignore ├── gradlew.bat └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'desktop', 'android', 'core' -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SuperMario 2 | # YouTube Tutorials @ http://www.BrentAureli.com -------------------------------------------------------------------------------- /android/assets/badlogic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/badlogic.jpg -------------------------------------------------------------------------------- /android/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/ic_launcher-web.png -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.daemon=true 2 | org.gradle.jvmargs=-Xms128m -Xmx512m 3 | org.gradle.configureondemand=true -------------------------------------------------------------------------------- /android/assets/Mario GFX/goomba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/Mario GFX/goomba.png -------------------------------------------------------------------------------- /android/assets/Mario GFX/turtle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/Mario GFX/turtle.png -------------------------------------------------------------------------------- /android/assets/tileset_gutter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/tileset_gutter.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /android/assets/Mario GFX/fireball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/Mario GFX/fireball.png -------------------------------------------------------------------------------- /android/assets/Mario GFX/mushroom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/Mario GFX/mushroom.png -------------------------------------------------------------------------------- /android/assets/Mario_and_Enemies.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/Mario_and_Enemies.png -------------------------------------------------------------------------------- /android/assets/audio/sounds/bump.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/bump.wav -------------------------------------------------------------------------------- /android/assets/audio/sounds/coin.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/coin.wav -------------------------------------------------------------------------------- /android/assets/audio/sounds/stomp.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/stomp.wav -------------------------------------------------------------------------------- /android/assets/Mario GFX/big_mario.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/Mario GFX/big_mario.png -------------------------------------------------------------------------------- /android/assets/audio/sounds/mariodie.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/mariodie.wav -------------------------------------------------------------------------------- /android/assets/audio/sounds/powerup.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/powerup.wav -------------------------------------------------------------------------------- /android/assets/Mario GFX/little_mario.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/Mario GFX/little_mario.png -------------------------------------------------------------------------------- /android/assets/audio/music/mario_music.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/music/mario_music.ogg -------------------------------------------------------------------------------- /android/assets/audio/sounds/breakblock.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/breakblock.wav -------------------------------------------------------------------------------- /android/assets/audio/sounds/powerdown.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/powerdown.wav -------------------------------------------------------------------------------- /android/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /android/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /android/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /android/assets/audio/sounds/powerup_spawn.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/assets/audio/sounds/powerup_spawn.wav -------------------------------------------------------------------------------- /android/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrentAureli/SuperMario/HEAD/android/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /android/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Mario Bros 5 | 6 | 7 | -------------------------------------------------------------------------------- /core/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: "java" 2 | 3 | sourceCompatibility = 1.6 4 | [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' 5 | 6 | sourceSets.main.java.srcDirs = [ "src/" ] 7 | 8 | 9 | eclipse.project { 10 | name = appName + "-core" 11 | } 12 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Sep 21 13:08:26 CEST 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Items/ItemDef.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.Items; 2 | 3 | import com.badlogic.gdx.math.Vector2; 4 | 5 | /** 6 | * Created by brentaureli on 9/24/15. 7 | */ 8 | public class ItemDef { 9 | public Vector2 position; 10 | public Class type; 11 | 12 | public ItemDef(Vector2 position, Class type){ 13 | this.position = position; 14 | this.type = type; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /android/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /desktop/src/com/brentaureli/mariobros/desktop/DesktopLauncher.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.desktop; 2 | 3 | import com.badlogic.gdx.backends.lwjgl.LwjglApplication; 4 | import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; 5 | import com.brentaureli.mariobros.MarioBros; 6 | 7 | public class DesktopLauncher { 8 | public static void main (String[] arg) { 9 | LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); 10 | config.width = 1200; 11 | config.height = 624; 12 | new LwjglApplication(new MarioBros(), config); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /android/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | -------------------------------------------------------------------------------- /android/src/com/brentaureli/mariobros/android/AndroidLauncher.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.android; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.badlogic.gdx.backends.android.AndroidApplication; 6 | import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; 7 | import com.brentaureli.mariobros.MarioBros; 8 | 9 | public class AndroidLauncher extends AndroidApplication { 10 | @Override 11 | protected void onCreate (Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 14 | initialize(new MarioBros(), config); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /android/assets/Mario_and_Enemies.pack: -------------------------------------------------------------------------------- 1 | 2 | Mario_and_Enemies.png 3 | format: RGBA8888 4 | filter: Nearest,Nearest 5 | repeat: none 6 | big_mario 7 | rotate: false 8 | xy: 1, 29 9 | size: 336, 32 10 | orig: 336, 32 11 | offset: 0, 0 12 | index: -1 13 | little_mario 14 | rotate: false 15 | xy: 1, 11 16 | size: 224, 16 17 | orig: 224, 16 18 | offset: 0, 0 19 | index: -1 20 | turtle 21 | rotate: false 22 | xy: 339, 37 23 | size: 96, 24 24 | orig: 96, 24 25 | offset: 0, 0 26 | index: -1 27 | goomba 28 | rotate: false 29 | xy: 227, 11 30 | size: 48, 16 31 | orig: 48, 16 32 | offset: 0, 0 33 | index: -1 34 | fireball 35 | rotate: false 36 | xy: 1, 1 37 | size: 32, 8 38 | orig: 32, 8 39 | offset: 0, 0 40 | index: -1 41 | mushroom 42 | rotate: false 43 | xy: 437, 45 44 | size: 16, 16 45 | orig: 16, 16 46 | offset: 0, 0 47 | index: -1 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Java 2 | 3 | *.class 4 | *.war 5 | *.ear 6 | hs_err_pid* 7 | 8 | ## GWT 9 | war/ 10 | html/war/gwt_bree/ 11 | html/gwt-unitCache/ 12 | .apt_generated/ 13 | html/war/WEB-INF/deploy/ 14 | html/war/WEB-INF/classes/ 15 | .gwt/ 16 | gwt-unitCache/ 17 | www-test/ 18 | .gwt-tmp/ 19 | 20 | ## Android Studio and Intellij and Android in general 21 | android/libs/armeabi/ 22 | android/libs/armeabi-v7a/ 23 | android/libs/x86/ 24 | android/gen/ 25 | .idea/ 26 | *.ipr 27 | *.iws 28 | *.iml 29 | out/ 30 | com_crashlytics_export_strings.xml 31 | 32 | ## Eclipse 33 | .classpath 34 | .project 35 | .metadata 36 | **/bin/ 37 | tmp/ 38 | *.tmp 39 | *.bak 40 | *.swp 41 | *~.nib 42 | local.properties 43 | .settings/ 44 | .loadpath 45 | .externalToolBuilders/ 46 | *.launch 47 | 48 | ## NetBeans 49 | **/nbproject/private/ 50 | build/ 51 | nbbuild/ 52 | dist/ 53 | nbdist/ 54 | nbactions.xml 55 | nb-configuration.xml 56 | 57 | ## Gradle 58 | 59 | .gradle 60 | gradle-app.setting 61 | build/ 62 | 63 | ## OS Specific 64 | .DS_Store 65 | -------------------------------------------------------------------------------- /android/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 14 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/TileObjects/Brick.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.TileObjects; 2 | 3 | import com.badlogic.gdx.audio.Sound; 4 | import com.badlogic.gdx.maps.MapObject; 5 | import com.badlogic.gdx.math.Rectangle; 6 | import com.brentaureli.mariobros.MarioBros; 7 | import com.brentaureli.mariobros.Scenes.Hud; 8 | import com.brentaureli.mariobros.Screens.PlayScreen; 9 | import com.brentaureli.mariobros.Sprites.Mario; 10 | 11 | /** 12 | * Created by brentaureli on 8/28/15. 13 | */ 14 | public class Brick extends InteractiveTileObject { 15 | public Brick(PlayScreen screen, MapObject object){ 16 | super(screen, object); 17 | fixture.setUserData(this); 18 | setCategoryFilter(MarioBros.BRICK_BIT); 19 | } 20 | 21 | @Override 22 | public void onHeadHit(Mario mario) { 23 | if(mario.isBig()) { 24 | setCategoryFilter(MarioBros.DESTROYED_BIT); 25 | getCell().setTile(null); 26 | Hud.addScore(200); 27 | MarioBros.manager.get("audio/sounds/breakblock.wav", Sound.class).play(); 28 | } 29 | MarioBros.manager.get("audio/sounds/bump.wav", Sound.class).play(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Enemies/Enemy.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.Enemies; 2 | 3 | import com.badlogic.gdx.graphics.g2d.Sprite; 4 | import com.badlogic.gdx.math.Vector2; 5 | import com.badlogic.gdx.physics.box2d.Body; 6 | import com.badlogic.gdx.physics.box2d.World; 7 | import com.brentaureli.mariobros.Screens.PlayScreen; 8 | import com.brentaureli.mariobros.Sprites.Mario; 9 | 10 | /** 11 | * Created by brentaureli on 9/14/15. 12 | */ 13 | public abstract class Enemy extends Sprite { 14 | protected World world; 15 | protected PlayScreen screen; 16 | public Body b2body; 17 | public Vector2 velocity; 18 | 19 | public Enemy(PlayScreen screen, float x, float y){ 20 | this.world = screen.getWorld(); 21 | this.screen = screen; 22 | setPosition(x, y); 23 | defineEnemy(); 24 | velocity = new Vector2(-1, -2); 25 | b2body.setActive(false); 26 | } 27 | 28 | protected abstract void defineEnemy(); 29 | public abstract void update(float dt); 30 | public abstract void hitOnHead(Mario mario); 31 | public abstract void hitByEnemy(Enemy enemy); 32 | 33 | public void reverseVelocity(boolean x, boolean y){ 34 | if(x) 35 | velocity.x = -velocity.x; 36 | if(y) 37 | velocity.y = -velocity.y; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /desktop/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: "java" 2 | 3 | sourceCompatibility = 1.6 4 | sourceSets.main.java.srcDirs = [ "src/" ] 5 | 6 | project.ext.mainClassName = "com.brentaureli.mariobros.desktop.DesktopLauncher" 7 | project.ext.assetsDir = new File("../android/assets"); 8 | 9 | task run(dependsOn: classes, type: JavaExec) { 10 | main = project.mainClassName 11 | classpath = sourceSets.main.runtimeClasspath 12 | standardInput = System.in 13 | workingDir = project.assetsDir 14 | ignoreExitValue = true 15 | } 16 | 17 | task dist(type: Jar) { 18 | from files(sourceSets.main.output.classesDir) 19 | from files(sourceSets.main.output.resourcesDir) 20 | from {configurations.compile.collect {zipTree(it)}} 21 | from files(project.assetsDir); 22 | 23 | manifest { 24 | attributes 'Main-Class': project.mainClassName 25 | } 26 | } 27 | 28 | dist.dependsOn classes 29 | 30 | eclipse { 31 | project { 32 | name = appName + "-desktop" 33 | linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/android/assets' 34 | } 35 | } 36 | 37 | task afterEclipseImport(description: "Post processing after project generation", group: "IDE") { 38 | doLast { 39 | def classpath = new XmlParser().parse(file(".classpath")) 40 | new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]); 41 | def writer = new FileWriter(file(".classpath")) 42 | def printer = new XmlNodePrinter(new PrintWriter(writer)) 43 | printer.setPreserveWhitespace(true) 44 | printer.print(classpath) 45 | } 46 | } -------------------------------------------------------------------------------- /android/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | 22 | -verbose 23 | 24 | -dontwarn android.support.** 25 | -dontwarn com.badlogic.gdx.backends.android.AndroidFragmentApplication 26 | -dontwarn com.badlogic.gdx.utils.GdxBuild 27 | -dontwarn com.badlogic.gdx.physics.box2d.utils.Box2DBuild 28 | -dontwarn com.badlogic.gdx.jnigen.BuildTarget* 29 | 30 | -keepclassmembers class com.badlogic.gdx.backends.android.AndroidInput* { 31 | (com.badlogic.gdx.Application, android.content.Context, java.lang.Object, com.badlogic.gdx.backends.android.AndroidApplicationConfiguration); 32 | } 33 | 34 | -keepclassmembers class com.badlogic.gdx.physics.box2d.World { 35 | boolean contactFilter(long, long); 36 | void beginContact(long); 37 | void endContact(long); 38 | void preSolve(long, long); 39 | void postSolve(long, long); 40 | boolean reportFixture(long); 41 | float reportRayFixture(long, float, float, float, float, float); 42 | } 43 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Items/Item.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.Items; 2 | 3 | import com.badlogic.gdx.graphics.g2d.Batch; 4 | import com.badlogic.gdx.graphics.g2d.Sprite; 5 | import com.badlogic.gdx.math.Vector2; 6 | import com.badlogic.gdx.physics.box2d.Body; 7 | import com.badlogic.gdx.physics.box2d.World; 8 | import com.brentaureli.mariobros.MarioBros; 9 | import com.brentaureli.mariobros.Screens.PlayScreen; 10 | import com.brentaureli.mariobros.Sprites.Mario; 11 | 12 | /** 13 | * Created by brentaureli on 9/24/15. 14 | */ 15 | public abstract class Item extends Sprite { 16 | protected PlayScreen screen; 17 | protected World world; 18 | protected Vector2 velocity; 19 | protected boolean toDestroy; 20 | protected boolean destroyed; 21 | protected Body body; 22 | 23 | public Item(PlayScreen screen, float x, float y){ 24 | this.screen = screen; 25 | this.world = screen.getWorld(); 26 | toDestroy = false; 27 | destroyed = false; 28 | 29 | setPosition(x, y); 30 | setBounds(getX(), getY(), 16 / MarioBros.PPM, 16 / MarioBros.PPM); 31 | defineItem(); 32 | } 33 | 34 | public abstract void defineItem(); 35 | public abstract void use(Mario mario); 36 | 37 | public void update(float dt){ 38 | if(toDestroy && !destroyed){ 39 | world.destroyBody(body); 40 | destroyed = true; 41 | } 42 | } 43 | 44 | public void draw(Batch batch){ 45 | if(!destroyed) 46 | super.draw(batch); 47 | } 48 | 49 | public void destroy(){ 50 | toDestroy = true; 51 | } 52 | public void reverseVelocity(boolean x, boolean y){ 53 | if(x) 54 | velocity.x = -velocity.x; 55 | if(y) 56 | velocity.y = -velocity.y; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/TileObjects/Coin.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.TileObjects; 2 | 3 | import com.badlogic.gdx.audio.Sound; 4 | import com.badlogic.gdx.maps.MapObject; 5 | import com.badlogic.gdx.maps.tiled.TiledMapTileSet; 6 | import com.badlogic.gdx.math.Rectangle; 7 | import com.badlogic.gdx.math.Vector2; 8 | import com.brentaureli.mariobros.MarioBros; 9 | import com.brentaureli.mariobros.Scenes.Hud; 10 | import com.brentaureli.mariobros.Screens.PlayScreen; 11 | import com.brentaureli.mariobros.Sprites.Items.ItemDef; 12 | import com.brentaureli.mariobros.Sprites.Items.Mushroom; 13 | import com.brentaureli.mariobros.Sprites.Mario; 14 | 15 | /** 16 | * Created by brentaureli on 8/28/15. 17 | */ 18 | public class Coin extends InteractiveTileObject { 19 | private static TiledMapTileSet tileSet; 20 | private final int BLANK_COIN = 28; 21 | 22 | public Coin(PlayScreen screen, MapObject object){ 23 | super(screen, object); 24 | tileSet = map.getTileSets().getTileSet("tileset_gutter"); 25 | fixture.setUserData(this); 26 | setCategoryFilter(MarioBros.COIN_BIT); 27 | } 28 | 29 | @Override 30 | public void onHeadHit(Mario mario) { 31 | if(getCell().getTile().getId() == BLANK_COIN) 32 | MarioBros.manager.get("audio/sounds/bump.wav", Sound.class).play(); 33 | else { 34 | if(object.getProperties().containsKey("mushroom")) { 35 | screen.spawnItem(new ItemDef(new Vector2(body.getPosition().x, body.getPosition().y + 16 / MarioBros.PPM), 36 | Mushroom.class)); 37 | MarioBros.manager.get("audio/sounds/powerup_spawn.wav", Sound.class).play(); 38 | } 39 | else 40 | MarioBros.manager.get("audio/sounds/coin.wav", Sound.class).play(); 41 | getCell().setTile(tileSet.getTile(BLANK_COIN)); 42 | Hud.addScore(100); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Items/Mushroom.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.Items; 2 | 3 | import com.badlogic.gdx.math.Vector2; 4 | import com.badlogic.gdx.physics.box2d.BodyDef; 5 | import com.badlogic.gdx.physics.box2d.CircleShape; 6 | import com.badlogic.gdx.physics.box2d.FixtureDef; 7 | import com.brentaureli.mariobros.MarioBros; 8 | import com.brentaureli.mariobros.Screens.PlayScreen; 9 | import com.brentaureli.mariobros.Sprites.Mario; 10 | 11 | /** 12 | * Created by brentaureli on 9/24/15. 13 | */ 14 | public class Mushroom extends Item { 15 | public Mushroom(PlayScreen screen, float x, float y) { 16 | super(screen, x, y); 17 | setRegion(screen.getAtlas().findRegion("mushroom"), 0, 0, 16, 16); 18 | velocity = new Vector2(0.7f, 0); 19 | } 20 | 21 | @Override 22 | public void defineItem() { 23 | BodyDef bdef = new BodyDef(); 24 | bdef.position.set(getX(), getY()); 25 | bdef.type = BodyDef.BodyType.DynamicBody; 26 | body = world.createBody(bdef); 27 | 28 | FixtureDef fdef = new FixtureDef(); 29 | CircleShape shape = new CircleShape(); 30 | shape.setRadius(6 / MarioBros.PPM); 31 | fdef.filter.categoryBits = MarioBros.ITEM_BIT; 32 | fdef.filter.maskBits = MarioBros.MARIO_BIT | 33 | MarioBros.OBJECT_BIT | 34 | MarioBros.GROUND_BIT | 35 | MarioBros.COIN_BIT | 36 | MarioBros.BRICK_BIT; 37 | 38 | fdef.shape = shape; 39 | body.createFixture(fdef).setUserData(this); 40 | } 41 | 42 | @Override 43 | public void use(Mario mario) { 44 | destroy(); 45 | mario.grow(); 46 | } 47 | 48 | @Override 49 | public void update(float dt) { 50 | super.update(dt); 51 | setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2); 52 | velocity.y = body.getLinearVelocity().y; 53 | body.setLinearVelocity(velocity); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/MarioBros.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros; 2 | 3 | import com.badlogic.gdx.Game; 4 | import com.badlogic.gdx.assets.AssetManager; 5 | import com.badlogic.gdx.audio.Music; 6 | import com.badlogic.gdx.audio.Sound; 7 | import com.badlogic.gdx.graphics.g2d.SpriteBatch; 8 | import com.badlogic.gdx.physics.box2d.Filter; 9 | import com.brentaureli.mariobros.Screens.PlayScreen; 10 | 11 | public class MarioBros extends Game { 12 | //Virtual Screen size and Box2D Scale(Pixels Per Meter) 13 | public static final int V_WIDTH = 400; 14 | public static final int V_HEIGHT = 208; 15 | public static final float PPM = 100; 16 | 17 | //Box2D Collision Bits 18 | public static final short NOTHING_BIT = 0; 19 | public static final short GROUND_BIT = 1; 20 | public static final short MARIO_BIT = 2; 21 | public static final short BRICK_BIT = 4; 22 | public static final short COIN_BIT = 8; 23 | public static final short DESTROYED_BIT = 16; 24 | public static final short OBJECT_BIT = 32; 25 | public static final short ENEMY_BIT = 64; 26 | public static final short ENEMY_HEAD_BIT = 128; 27 | public static final short ITEM_BIT = 256; 28 | public static final short MARIO_HEAD_BIT = 512; 29 | public static final short FIREBALL_BIT = 1024; 30 | 31 | public SpriteBatch batch; 32 | 33 | /* WARNING Using AssetManager in a static way can cause issues, especially on Android. 34 | Instead you may want to pass around Assetmanager to those the classes that need it. 35 | We will use it in the static context to save time for now. */ 36 | public static AssetManager manager; 37 | 38 | @Override 39 | public void create () { 40 | batch = new SpriteBatch(); 41 | manager = new AssetManager(); 42 | manager.load("audio/music/mario_music.ogg", Music.class); 43 | manager.load("audio/sounds/coin.wav", Sound.class); 44 | manager.load("audio/sounds/bump.wav", Sound.class); 45 | manager.load("audio/sounds/breakblock.wav", Sound.class); 46 | manager.load("audio/sounds/powerup_spawn.wav", Sound.class); 47 | manager.load("audio/sounds/powerup.wav", Sound.class); 48 | manager.load("audio/sounds/powerdown.wav", Sound.class); 49 | manager.load("audio/sounds/stomp.wav", Sound.class); 50 | manager.load("audio/sounds/mariodie.wav", Sound.class); 51 | 52 | manager.finishLoading(); 53 | 54 | setScreen(new PlayScreen(this)); 55 | } 56 | 57 | 58 | @Override 59 | public void dispose() { 60 | super.dispose(); 61 | manager.dispose(); 62 | batch.dispose(); 63 | } 64 | 65 | @Override 66 | public void render () { 67 | super.render(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Screens/GameOverScreen.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Screens; 2 | 3 | import com.badlogic.gdx.Game; 4 | import com.badlogic.gdx.Gdx; 5 | import com.badlogic.gdx.Screen; 6 | import com.badlogic.gdx.graphics.Color; 7 | import com.badlogic.gdx.graphics.GL20; 8 | import com.badlogic.gdx.graphics.OrthographicCamera; 9 | import com.badlogic.gdx.graphics.g2d.BitmapFont; 10 | import com.badlogic.gdx.scenes.scene2d.Event; 11 | import com.badlogic.gdx.scenes.scene2d.EventListener; 12 | import com.badlogic.gdx.scenes.scene2d.InputEvent; 13 | import com.badlogic.gdx.scenes.scene2d.InputListener; 14 | import com.badlogic.gdx.scenes.scene2d.Stage; 15 | import com.badlogic.gdx.scenes.scene2d.ui.Label; 16 | import com.badlogic.gdx.scenes.scene2d.ui.Table; 17 | import com.badlogic.gdx.scenes.scene2d.ui.TextButton; 18 | import com.badlogic.gdx.utils.viewport.FitViewport; 19 | import com.badlogic.gdx.utils.viewport.Viewport; 20 | import com.brentaureli.mariobros.MarioBros; 21 | 22 | /** 23 | * Created by brentaureli on 10/8/15. 24 | */ 25 | public class GameOverScreen implements Screen { 26 | private Viewport viewport; 27 | private Stage stage; 28 | 29 | private Game game; 30 | 31 | public GameOverScreen(Game game){ 32 | this.game = game; 33 | viewport = new FitViewport(MarioBros.V_WIDTH, MarioBros.V_HEIGHT, new OrthographicCamera()); 34 | stage = new Stage(viewport, ((MarioBros) game).batch); 35 | 36 | Label.LabelStyle font = new Label.LabelStyle(new BitmapFont(), Color.WHITE); 37 | 38 | Table table = new Table(); 39 | table.center(); 40 | table.setFillParent(true); 41 | 42 | Label gameOverLabel = new Label("GAME OVER", font); 43 | Label playAgainLabel = new Label("Click to Play Again", font); 44 | 45 | table.add(gameOverLabel).expandX(); 46 | table.row(); 47 | table.add(playAgainLabel).expandX().padTop(10f); 48 | 49 | stage.addActor(table); 50 | } 51 | 52 | @Override 53 | public void show() { 54 | 55 | } 56 | 57 | @Override 58 | public void render(float delta) { 59 | if(Gdx.input.justTouched()) { 60 | game.setScreen(new PlayScreen((MarioBros) game)); 61 | dispose(); 62 | } 63 | Gdx.gl.glClearColor(0, 0, 0, 1); 64 | Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 65 | stage.draw(); 66 | } 67 | 68 | @Override 69 | public void resize(int width, int height) { 70 | 71 | } 72 | 73 | @Override 74 | public void pause() { 75 | 76 | } 77 | 78 | @Override 79 | public void resume() { 80 | 81 | } 82 | 83 | @Override 84 | public void hide() { 85 | 86 | } 87 | 88 | @Override 89 | public void dispose() { 90 | stage.dispose(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/TileObjects/InteractiveTileObject.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.TileObjects; 2 | 3 | import com.badlogic.gdx.maps.MapObject; 4 | import com.badlogic.gdx.maps.objects.RectangleMapObject; 5 | import com.badlogic.gdx.maps.tiled.TiledMap; 6 | import com.badlogic.gdx.maps.tiled.TiledMapTile; 7 | import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; 8 | import com.badlogic.gdx.math.Rectangle; 9 | import com.badlogic.gdx.physics.box2d.Body; 10 | import com.badlogic.gdx.physics.box2d.BodyDef; 11 | import com.badlogic.gdx.physics.box2d.Filter; 12 | import com.badlogic.gdx.physics.box2d.Fixture; 13 | import com.badlogic.gdx.physics.box2d.FixtureDef; 14 | import com.badlogic.gdx.physics.box2d.PolygonShape; 15 | import com.badlogic.gdx.physics.box2d.World; 16 | import com.badlogic.gdx.utils.Array; 17 | import com.brentaureli.mariobros.MarioBros; 18 | import com.brentaureli.mariobros.Scenes.Hud; 19 | import com.brentaureli.mariobros.Screens.PlayScreen; 20 | import com.brentaureli.mariobros.Sprites.Mario; 21 | 22 | /** 23 | * Created by brentaureli on 8/28/15. 24 | */ 25 | public abstract class InteractiveTileObject { 26 | protected World world; 27 | protected TiledMap map; 28 | protected Rectangle bounds; 29 | protected Body body; 30 | protected PlayScreen screen; 31 | protected MapObject object; 32 | 33 | protected Fixture fixture; 34 | 35 | public InteractiveTileObject(PlayScreen screen, MapObject object){ 36 | this.object = object; 37 | this.screen = screen; 38 | this.world = screen.getWorld(); 39 | this.map = screen.getMap(); 40 | this.bounds = ((RectangleMapObject) object).getRectangle(); 41 | 42 | BodyDef bdef = new BodyDef(); 43 | FixtureDef fdef = new FixtureDef(); 44 | PolygonShape shape = new PolygonShape(); 45 | 46 | bdef.type = BodyDef.BodyType.StaticBody; 47 | bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MarioBros.PPM, (bounds.getY() + bounds.getHeight() / 2) / MarioBros.PPM); 48 | 49 | body = world.createBody(bdef); 50 | 51 | shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM); 52 | fdef.shape = shape; 53 | fixture = body.createFixture(fdef); 54 | 55 | } 56 | 57 | public abstract void onHeadHit(Mario mario); 58 | public void setCategoryFilter(short filterBit){ 59 | Filter filter = new Filter(); 60 | filter.categoryBits = filterBit; 61 | fixture.setFilterData(filter); 62 | } 63 | 64 | public TiledMapTileLayer.Cell getCell(){ 65 | TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); 66 | return layer.getCell((int)(body.getPosition().x * MarioBros.PPM / 16), 67 | (int)(body.getPosition().y * MarioBros.PPM / 16)); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Other/FireBall.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.Other; 2 | 3 | import com.badlogic.gdx.graphics.g2d.Animation; 4 | import com.badlogic.gdx.graphics.g2d.Sprite; 5 | import com.badlogic.gdx.graphics.g2d.TextureRegion; 6 | import com.badlogic.gdx.math.Vector2; 7 | import com.badlogic.gdx.physics.box2d.Body; 8 | import com.badlogic.gdx.physics.box2d.BodyDef; 9 | import com.badlogic.gdx.physics.box2d.CircleShape; 10 | import com.badlogic.gdx.physics.box2d.FixtureDef; 11 | import com.badlogic.gdx.physics.box2d.PolygonShape; 12 | import com.badlogic.gdx.physics.box2d.World; 13 | import com.badlogic.gdx.utils.Array; 14 | import com.brentaureli.mariobros.MarioBros; 15 | import com.brentaureli.mariobros.Screens.PlayScreen; 16 | 17 | /** 18 | * Created by brentaureli on 10/12/15. 19 | */ 20 | public class FireBall extends Sprite { 21 | 22 | PlayScreen screen; 23 | World world; 24 | Array frames; 25 | Animation fireAnimation; 26 | float stateTime; 27 | boolean destroyed; 28 | boolean setToDestroy; 29 | boolean fireRight; 30 | 31 | Body b2body; 32 | public FireBall(PlayScreen screen, float x, float y, boolean fireRight){ 33 | this.fireRight = fireRight; 34 | this.screen = screen; 35 | this.world = screen.getWorld(); 36 | frames = new Array(); 37 | for(int i = 0; i < 4; i++){ 38 | frames.add(new TextureRegion(screen.getAtlas().findRegion("fireball"), i * 8, 0, 8, 8)); 39 | } 40 | fireAnimation = new Animation(0.2f, frames); 41 | setRegion(fireAnimation.getKeyFrame(0)); 42 | setBounds(x, y, 6 / MarioBros.PPM, 6 / MarioBros.PPM); 43 | defineFireBall(); 44 | } 45 | 46 | public void defineFireBall(){ 47 | BodyDef bdef = new BodyDef(); 48 | bdef.position.set(fireRight ? getX() + 12 /MarioBros.PPM : getX() - 12 /MarioBros.PPM, getY()); 49 | bdef.type = BodyDef.BodyType.DynamicBody; 50 | if(!world.isLocked()) 51 | b2body = world.createBody(bdef); 52 | 53 | FixtureDef fdef = new FixtureDef(); 54 | CircleShape shape = new CircleShape(); 55 | shape.setRadius(3 / MarioBros.PPM); 56 | fdef.filter.categoryBits = MarioBros.FIREBALL_BIT; 57 | fdef.filter.maskBits = MarioBros.GROUND_BIT | 58 | MarioBros.COIN_BIT | 59 | MarioBros.BRICK_BIT | 60 | MarioBros.ENEMY_BIT | 61 | MarioBros.OBJECT_BIT; 62 | 63 | fdef.shape = shape; 64 | fdef.restitution = 1; 65 | fdef.friction = 0; 66 | b2body.createFixture(fdef).setUserData(this); 67 | b2body.setLinearVelocity(new Vector2(fireRight ? 2 : -2, 2.5f)); 68 | } 69 | 70 | public void update(float dt){ 71 | stateTime += dt; 72 | setRegion(fireAnimation.getKeyFrame(stateTime, true)); 73 | setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2); 74 | if((stateTime > 3 || setToDestroy) && !destroyed) { 75 | world.destroyBody(b2body); 76 | destroyed = true; 77 | } 78 | if(b2body.getLinearVelocity().y > 2f) 79 | b2body.setLinearVelocity(b2body.getLinearVelocity().x, 2f); 80 | if((fireRight && b2body.getLinearVelocity().x < 0) || (!fireRight && b2body.getLinearVelocity().x > 0)) 81 | setToDestroy(); 82 | } 83 | 84 | public void setToDestroy(){ 85 | setToDestroy = true; 86 | } 87 | 88 | public boolean isDestroyed(){ 89 | return destroyed; 90 | } 91 | 92 | 93 | } 94 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Scenes/Hud.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Scenes; 2 | 3 | import com.badlogic.gdx.Gdx; 4 | import com.badlogic.gdx.graphics.Color; 5 | import com.badlogic.gdx.graphics.OrthographicCamera; 6 | import com.badlogic.gdx.graphics.g2d.BitmapFont; 7 | import com.badlogic.gdx.graphics.g2d.SpriteBatch; 8 | import com.badlogic.gdx.scenes.scene2d.Stage; 9 | import com.badlogic.gdx.scenes.scene2d.ui.Label; 10 | import com.badlogic.gdx.scenes.scene2d.ui.Table; 11 | import com.badlogic.gdx.utils.Disposable; 12 | import com.badlogic.gdx.utils.viewport.FitViewport; 13 | import com.badlogic.gdx.utils.viewport.Viewport; 14 | import com.brentaureli.mariobros.MarioBros; 15 | 16 | /** 17 | * Created by brentaureli on 8/17/15. 18 | */ 19 | public class Hud implements Disposable{ 20 | 21 | //Scene2D.ui Stage and its own Viewport for HUD 22 | public Stage stage; 23 | private Viewport viewport; 24 | 25 | //Mario score/time Tracking Variables 26 | private Integer worldTimer; 27 | private boolean timeUp; // true when the world timer reaches 0 28 | private float timeCount; 29 | private static Integer score; 30 | 31 | //Scene2D widgets 32 | private Label countdownLabel; 33 | private static Label scoreLabel; 34 | private Label timeLabel; 35 | private Label levelLabel; 36 | private Label worldLabel; 37 | private Label marioLabel; 38 | 39 | public Hud(SpriteBatch sb){ 40 | //define our tracking variables 41 | worldTimer = 300; 42 | timeCount = 0; 43 | score = 0; 44 | 45 | 46 | //setup the HUD viewport using a new camera seperate from our gamecam 47 | //define our stage using that viewport and our games spritebatch 48 | viewport = new FitViewport(MarioBros.V_WIDTH, MarioBros.V_HEIGHT, new OrthographicCamera()); 49 | stage = new Stage(viewport, sb); 50 | 51 | //define a table used to organize our hud's labels 52 | Table table = new Table(); 53 | //Top-Align table 54 | table.top(); 55 | //make the table fill the entire stage 56 | table.setFillParent(true); 57 | 58 | //define our labels using the String, and a Label style consisting of a font and color 59 | countdownLabel = new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.WHITE)); 60 | scoreLabel =new Label(String.format("%06d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE)); 61 | timeLabel = new Label("TIME", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); 62 | levelLabel = new Label("1-1", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); 63 | worldLabel = new Label("WORLD", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); 64 | marioLabel = new Label("MARIO", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); 65 | 66 | //add our labels to our table, padding the top, and giving them all equal width with expandX 67 | table.add(marioLabel).expandX().padTop(10); 68 | table.add(worldLabel).expandX().padTop(10); 69 | table.add(timeLabel).expandX().padTop(10); 70 | //add a second row to our table 71 | table.row(); 72 | table.add(scoreLabel).expandX(); 73 | table.add(levelLabel).expandX(); 74 | table.add(countdownLabel).expandX(); 75 | 76 | //add our table to the stage 77 | stage.addActor(table); 78 | 79 | } 80 | 81 | public void update(float dt){ 82 | timeCount += dt; 83 | if(timeCount >= 1){ 84 | if (worldTimer > 0) { 85 | worldTimer--; 86 | } else { 87 | timeUp = true; 88 | } 89 | countdownLabel.setText(String.format("%03d", worldTimer)); 90 | timeCount = 0; 91 | } 92 | } 93 | 94 | public static void addScore(int value){ 95 | score += value; 96 | scoreLabel.setText(String.format("%06d", score)); 97 | } 98 | 99 | @Override 100 | public void dispose() { stage.dispose(); } 101 | 102 | public boolean isTimeUp() { return timeUp; } 103 | } 104 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | android { 2 | buildToolsVersion "22.0.1" 3 | compileSdkVersion 22 4 | sourceSets { 5 | main { 6 | manifest.srcFile 'AndroidManifest.xml' 7 | java.srcDirs = ['src'] 8 | aidl.srcDirs = ['src'] 9 | renderscript.srcDirs = ['src'] 10 | res.srcDirs = ['res'] 11 | assets.srcDirs = ['assets'] 12 | jniLibs.srcDirs = ['libs'] 13 | } 14 | 15 | instrumentTest.setRoot('tests') 16 | } 17 | } 18 | 19 | 20 | // called every time gradle gets executed, takes the native dependencies of 21 | // the natives configuration, and extracts them to the proper libs/ folders 22 | // so they get packed with the APK. 23 | task copyAndroidNatives() { 24 | file("libs/armeabi/").mkdirs(); 25 | file("libs/armeabi-v7a/").mkdirs(); 26 | file("libs/x86/").mkdirs(); 27 | 28 | configurations.natives.files.each { jar -> 29 | def outputDir = null 30 | if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a") 31 | if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi") 32 | if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86") 33 | if(outputDir != null) { 34 | copy { 35 | from zipTree(jar) 36 | into outputDir 37 | include "*.so" 38 | } 39 | } 40 | } 41 | } 42 | 43 | task run(type: Exec) { 44 | def path 45 | def localProperties = project.file("../local.properties") 46 | if (localProperties.exists()) { 47 | Properties properties = new Properties() 48 | localProperties.withInputStream { instr -> 49 | properties.load(instr) 50 | } 51 | def sdkDir = properties.getProperty('sdk.dir') 52 | if (sdkDir) { 53 | path = sdkDir 54 | } else { 55 | path = "$System.env.ANDROID_HOME" 56 | } 57 | } else { 58 | path = "$System.env.ANDROID_HOME" 59 | } 60 | 61 | def adb = path + "/platform-tools/adb" 62 | commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.brentaureli.mariobros.android/com.brentaureli.mariobros.android.AndroidLauncher' 63 | } 64 | 65 | // sets up the Android Eclipse project, using the old Ant based build. 66 | eclipse { 67 | // need to specify Java source sets explicitely, SpringSource Gradle Eclipse plugin 68 | // ignores any nodes added in classpath.file.withXml 69 | sourceSets { 70 | main { 71 | java.srcDirs "src", 'gen' 72 | } 73 | } 74 | 75 | jdt { 76 | sourceCompatibility = 1.6 77 | targetCompatibility = 1.6 78 | } 79 | 80 | classpath { 81 | plusConfigurations += [ project.configurations.compile ] 82 | containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES' 83 | } 84 | 85 | project { 86 | name = appName + "-android" 87 | natures 'com.android.ide.eclipse.adt.AndroidNature' 88 | buildCommands.clear(); 89 | buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder" 90 | buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder" 91 | buildCommand "org.eclipse.jdt.core.javabuilder" 92 | buildCommand "com.android.ide.eclipse.adt.ApkBuilder" 93 | } 94 | } 95 | 96 | // sets up the Android Idea project, using the old Ant based build. 97 | idea { 98 | module { 99 | sourceDirs += file("src"); 100 | scopes = [ COMPILE: [plus:[project.configurations.compile]]] 101 | 102 | iml { 103 | withXml { 104 | def node = it.asNode() 105 | def builder = NodeBuilder.newInstance(); 106 | builder.current = node; 107 | builder.component(name: "FacetManager") { 108 | facet(type: "android", name: "Android") { 109 | configuration { 110 | option(name: "UPDATE_PROPERTY_FILES", value:"true") 111 | } 112 | } 113 | } 114 | } 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Tools/WorldContactListener.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Tools; 2 | 3 | import com.badlogic.gdx.Gdx; 4 | import com.badlogic.gdx.physics.box2d.Contact; 5 | import com.badlogic.gdx.physics.box2d.ContactImpulse; 6 | import com.badlogic.gdx.physics.box2d.ContactListener; 7 | import com.badlogic.gdx.physics.box2d.Fixture; 8 | import com.badlogic.gdx.physics.box2d.Manifold; 9 | import com.brentaureli.mariobros.MarioBros; 10 | import com.brentaureli.mariobros.Sprites.Enemies.Enemy; 11 | import com.brentaureli.mariobros.Sprites.Items.Item; 12 | import com.brentaureli.mariobros.Sprites.Mario; 13 | import com.brentaureli.mariobros.Sprites.Other.FireBall; 14 | import com.brentaureli.mariobros.Sprites.TileObjects.InteractiveTileObject; 15 | 16 | /** 17 | * Created by brentaureli on 9/4/15. 18 | */ 19 | public class WorldContactListener implements ContactListener { 20 | @Override 21 | public void beginContact(Contact contact) { 22 | Fixture fixA = contact.getFixtureA(); 23 | Fixture fixB = contact.getFixtureB(); 24 | 25 | int cDef = fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits; 26 | 27 | switch (cDef){ 28 | case MarioBros.MARIO_HEAD_BIT | MarioBros.BRICK_BIT: 29 | case MarioBros.MARIO_HEAD_BIT | MarioBros.COIN_BIT: 30 | if(fixA.getFilterData().categoryBits == MarioBros.MARIO_HEAD_BIT) 31 | ((InteractiveTileObject) fixB.getUserData()).onHeadHit((Mario) fixA.getUserData()); 32 | else 33 | ((InteractiveTileObject) fixA.getUserData()).onHeadHit((Mario) fixB.getUserData()); 34 | break; 35 | case MarioBros.ENEMY_HEAD_BIT | MarioBros.MARIO_BIT: 36 | if(fixA.getFilterData().categoryBits == MarioBros.ENEMY_HEAD_BIT) 37 | ((Enemy)fixA.getUserData()).hitOnHead((Mario) fixB.getUserData()); 38 | else 39 | ((Enemy)fixB.getUserData()).hitOnHead((Mario) fixA.getUserData()); 40 | break; 41 | case MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT: 42 | if(fixA.getFilterData().categoryBits == MarioBros.ENEMY_BIT) 43 | ((Enemy)fixA.getUserData()).reverseVelocity(true, false); 44 | else 45 | ((Enemy)fixB.getUserData()).reverseVelocity(true, false); 46 | break; 47 | case MarioBros.MARIO_BIT | MarioBros.ENEMY_BIT: 48 | if(fixA.getFilterData().categoryBits == MarioBros.MARIO_BIT) 49 | ((Mario) fixA.getUserData()).hit((Enemy)fixB.getUserData()); 50 | else 51 | ((Mario) fixB.getUserData()).hit((Enemy)fixA.getUserData()); 52 | break; 53 | case MarioBros.ENEMY_BIT | MarioBros.ENEMY_BIT: 54 | ((Enemy)fixA.getUserData()).hitByEnemy((Enemy)fixB.getUserData()); 55 | ((Enemy)fixB.getUserData()).hitByEnemy((Enemy)fixA.getUserData()); 56 | break; 57 | case MarioBros.ITEM_BIT | MarioBros.OBJECT_BIT: 58 | if(fixA.getFilterData().categoryBits == MarioBros.ITEM_BIT) 59 | ((Item)fixA.getUserData()).reverseVelocity(true, false); 60 | else 61 | ((Item)fixB.getUserData()).reverseVelocity(true, false); 62 | break; 63 | case MarioBros.ITEM_BIT | MarioBros.MARIO_BIT: 64 | if(fixA.getFilterData().categoryBits == MarioBros.ITEM_BIT) 65 | ((Item)fixA.getUserData()).use((Mario) fixB.getUserData()); 66 | else 67 | ((Item)fixB.getUserData()).use((Mario) fixA.getUserData()); 68 | break; 69 | case MarioBros.FIREBALL_BIT | MarioBros.OBJECT_BIT: 70 | if(fixA.getFilterData().categoryBits == MarioBros.FIREBALL_BIT) 71 | ((FireBall)fixA.getUserData()).setToDestroy(); 72 | else 73 | ((FireBall)fixB.getUserData()).setToDestroy(); 74 | break; 75 | } 76 | } 77 | 78 | @Override 79 | public void endContact(Contact contact) { 80 | } 81 | 82 | @Override 83 | public void preSolve(Contact contact, Manifold oldManifold) { 84 | } 85 | 86 | @Override 87 | public void postSolve(Contact contact, ContactImpulse impulse) { 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Enemies/Goomba.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.Enemies; 2 | 3 | import com.badlogic.gdx.audio.Sound; 4 | import com.badlogic.gdx.graphics.g2d.Animation; 5 | import com.badlogic.gdx.graphics.g2d.Batch; 6 | import com.badlogic.gdx.graphics.g2d.TextureRegion; 7 | import com.badlogic.gdx.math.Vector2; 8 | import com.badlogic.gdx.physics.box2d.BodyDef; 9 | import com.badlogic.gdx.physics.box2d.CircleShape; 10 | import com.badlogic.gdx.physics.box2d.FixtureDef; 11 | import com.badlogic.gdx.physics.box2d.PolygonShape; 12 | import com.badlogic.gdx.utils.Array; 13 | import com.brentaureli.mariobros.MarioBros; 14 | import com.brentaureli.mariobros.Screens.PlayScreen; 15 | import com.brentaureli.mariobros.Sprites.Mario; 16 | 17 | /** 18 | * Created by brentaureli on 9/14/15. 19 | */ 20 | public class Goomba extends com.brentaureli.mariobros.Sprites.Enemies.Enemy 21 | { 22 | private float stateTime; 23 | private Animation walkAnimation; 24 | private Array frames; 25 | private boolean setToDestroy; 26 | private boolean destroyed; 27 | float angle; 28 | 29 | 30 | public Goomba(PlayScreen screen, float x, float y) { 31 | super(screen, x, y); 32 | frames = new Array(); 33 | for(int i = 0; i < 2; i++) 34 | frames.add(new TextureRegion(screen.getAtlas().findRegion("goomba"), i * 16, 0, 16, 16)); 35 | walkAnimation = new Animation(0.4f, frames); 36 | stateTime = 0; 37 | setBounds(getX(), getY(), 16 / MarioBros.PPM, 16 / MarioBros.PPM); 38 | setToDestroy = false; 39 | destroyed = false; 40 | angle = 0; 41 | } 42 | 43 | public void update(float dt){ 44 | stateTime += dt; 45 | if(setToDestroy && !destroyed){ 46 | world.destroyBody(b2body); 47 | destroyed = true; 48 | setRegion(new TextureRegion(screen.getAtlas().findRegion("goomba"), 32, 0, 16, 16)); 49 | stateTime = 0; 50 | } 51 | else if(!destroyed) { 52 | b2body.setLinearVelocity(velocity); 53 | setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2); 54 | setRegion(walkAnimation.getKeyFrame(stateTime, true)); 55 | } 56 | } 57 | 58 | @Override 59 | protected void defineEnemy() { 60 | BodyDef bdef = new BodyDef(); 61 | bdef.position.set(getX(), getY()); 62 | bdef.type = BodyDef.BodyType.DynamicBody; 63 | b2body = world.createBody(bdef); 64 | 65 | FixtureDef fdef = new FixtureDef(); 66 | CircleShape shape = new CircleShape(); 67 | shape.setRadius(6 / MarioBros.PPM); 68 | fdef.filter.categoryBits = MarioBros.ENEMY_BIT; 69 | fdef.filter.maskBits = MarioBros.GROUND_BIT | 70 | MarioBros.COIN_BIT | 71 | MarioBros.BRICK_BIT | 72 | MarioBros.ENEMY_BIT | 73 | MarioBros.OBJECT_BIT | 74 | MarioBros.MARIO_BIT; 75 | 76 | fdef.shape = shape; 77 | b2body.createFixture(fdef).setUserData(this); 78 | 79 | //Create the Head here: 80 | PolygonShape head = new PolygonShape(); 81 | Vector2[] vertice = new Vector2[4]; 82 | vertice[0] = new Vector2(-5, 8).scl(1 / MarioBros.PPM); 83 | vertice[1] = new Vector2(5, 8).scl(1 / MarioBros.PPM); 84 | vertice[2] = new Vector2(-3, 3).scl(1 / MarioBros.PPM); 85 | vertice[3] = new Vector2(3, 3).scl(1 / MarioBros.PPM); 86 | head.set(vertice); 87 | 88 | fdef.shape = head; 89 | fdef.restitution = 0.5f; 90 | fdef.filter.categoryBits = MarioBros.ENEMY_HEAD_BIT; 91 | b2body.createFixture(fdef).setUserData(this); 92 | 93 | } 94 | 95 | public void draw(Batch batch){ 96 | if(!destroyed || stateTime < 1) 97 | super.draw(batch); 98 | } 99 | 100 | 101 | 102 | @Override 103 | public void hitOnHead(Mario mario) { 104 | setToDestroy = true; 105 | MarioBros.manager.get("audio/sounds/stomp.wav", Sound.class).play(); 106 | } 107 | 108 | @Override 109 | public void hitByEnemy(Enemy enemy) { 110 | if(enemy instanceof Turtle && ((Turtle) enemy).currentState == Turtle.State.MOVING_SHELL) 111 | setToDestroy = true; 112 | else 113 | reverseVelocity(true, false); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Tools/B2WorldCreator.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Tools; 2 | 3 | import com.badlogic.gdx.maps.MapObject; 4 | import com.badlogic.gdx.maps.objects.RectangleMapObject; 5 | import com.badlogic.gdx.maps.tiled.TiledMap; 6 | import com.badlogic.gdx.math.Rectangle; 7 | import com.badlogic.gdx.physics.box2d.Body; 8 | import com.badlogic.gdx.physics.box2d.BodyDef; 9 | import com.badlogic.gdx.physics.box2d.FixtureDef; 10 | import com.badlogic.gdx.physics.box2d.PolygonShape; 11 | import com.badlogic.gdx.physics.box2d.World; 12 | import com.badlogic.gdx.utils.Array; 13 | import com.brentaureli.mariobros.MarioBros; 14 | import com.brentaureli.mariobros.Screens.PlayScreen; 15 | import com.brentaureli.mariobros.Sprites.Enemies.Enemy; 16 | import com.brentaureli.mariobros.Sprites.Enemies.Turtle; 17 | import com.brentaureli.mariobros.Sprites.TileObjects.Brick; 18 | import com.brentaureli.mariobros.Sprites.TileObjects.Coin; 19 | import com.brentaureli.mariobros.Sprites.Enemies.Goomba; 20 | 21 | /** 22 | * Created by brentaureli on 8/28/15. 23 | */ 24 | public class B2WorldCreator { 25 | private Array goombas; 26 | private Array turtles; 27 | 28 | public B2WorldCreator(PlayScreen screen){ 29 | World world = screen.getWorld(); 30 | TiledMap map = screen.getMap(); 31 | //create body and fixture variables 32 | BodyDef bdef = new BodyDef(); 33 | PolygonShape shape = new PolygonShape(); 34 | FixtureDef fdef = new FixtureDef(); 35 | Body body; 36 | 37 | //create ground bodies/fixtures 38 | for(MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){ 39 | Rectangle rect = ((RectangleMapObject) object).getRectangle(); 40 | 41 | bdef.type = BodyDef.BodyType.StaticBody; 42 | bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM, (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM); 43 | 44 | body = world.createBody(bdef); 45 | 46 | shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM); 47 | fdef.shape = shape; 48 | body.createFixture(fdef); 49 | } 50 | 51 | //create pipe bodies/fixtures 52 | for(MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)){ 53 | Rectangle rect = ((RectangleMapObject) object).getRectangle(); 54 | 55 | bdef.type = BodyDef.BodyType.StaticBody; 56 | bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM, (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM); 57 | 58 | body = world.createBody(bdef); 59 | 60 | shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM); 61 | fdef.shape = shape; 62 | fdef.filter.categoryBits = MarioBros.OBJECT_BIT; 63 | body.createFixture(fdef); 64 | } 65 | 66 | //create brick bodies/fixtures 67 | for(MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)){ 68 | new Brick(screen, object); 69 | } 70 | 71 | //create coin bodies/fixtures 72 | for(MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)){ 73 | 74 | new Coin(screen, object); 75 | } 76 | 77 | //create all goombas 78 | goombas = new Array(); 79 | for(MapObject object : map.getLayers().get(6).getObjects().getByType(RectangleMapObject.class)){ 80 | Rectangle rect = ((RectangleMapObject) object).getRectangle(); 81 | goombas.add(new Goomba(screen, rect.getX() / MarioBros.PPM, rect.getY() / MarioBros.PPM)); 82 | } 83 | turtles = new Array(); 84 | for(MapObject object : map.getLayers().get(7).getObjects().getByType(RectangleMapObject.class)){ 85 | Rectangle rect = ((RectangleMapObject) object).getRectangle(); 86 | turtles.add(new Turtle(screen, rect.getX() / MarioBros.PPM, rect.getY() / MarioBros.PPM)); 87 | } 88 | } 89 | 90 | public Array getGoombas() { 91 | return goombas; 92 | } 93 | public Array getEnemies(){ 94 | Array enemies = new Array(); 95 | enemies.addAll(goombas); 96 | enemies.addAll(turtles); 97 | return enemies; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Enemies/Turtle.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites.Enemies; 2 | 3 | import com.badlogic.gdx.graphics.g2d.Animation; 4 | import com.badlogic.gdx.graphics.g2d.TextureRegion; 5 | import com.badlogic.gdx.math.Vector2; 6 | import com.badlogic.gdx.physics.box2d.BodyDef; 7 | import com.badlogic.gdx.physics.box2d.CircleShape; 8 | import com.badlogic.gdx.physics.box2d.FixtureDef; 9 | import com.badlogic.gdx.physics.box2d.PolygonShape; 10 | import com.badlogic.gdx.utils.Array; 11 | import com.brentaureli.mariobros.MarioBros; 12 | import com.brentaureli.mariobros.Screens.PlayScreen; 13 | import com.brentaureli.mariobros.Sprites.Mario; 14 | 15 | /** 16 | * Created by brentaureli on 10/10/15. 17 | */ 18 | public class Turtle extends Enemy { 19 | public static final int KICK_LEFT = -2; 20 | public static final int KICK_RIGHT = 2; 21 | public enum State {WALKING, MOVING_SHELL, STANDING_SHELL} 22 | public State currentState; 23 | public State previousState; 24 | private float stateTime; 25 | private Animation walkAnimation; 26 | private Array frames; 27 | private TextureRegion shell; 28 | private boolean setToDestroy; 29 | private boolean destroyed; 30 | 31 | 32 | public Turtle(PlayScreen screen, float x, float y) { 33 | super(screen, x, y); 34 | frames = new Array(); 35 | frames.add(new TextureRegion(screen.getAtlas().findRegion("turtle"), 0, 0, 16, 24)); 36 | frames.add(new TextureRegion(screen.getAtlas().findRegion("turtle"), 16, 0, 16, 24)); 37 | shell = new TextureRegion(screen.getAtlas().findRegion("turtle"), 64, 0, 16, 24); 38 | walkAnimation = new Animation(0.2f, frames); 39 | currentState = previousState = State.WALKING; 40 | 41 | setBounds(getX(), getY(), 16 / MarioBros.PPM, 24 / MarioBros.PPM); 42 | 43 | } 44 | 45 | @Override 46 | protected void defineEnemy() { 47 | BodyDef bdef = new BodyDef(); 48 | bdef.position.set(getX(), getY()); 49 | bdef.type = BodyDef.BodyType.DynamicBody; 50 | b2body = world.createBody(bdef); 51 | 52 | FixtureDef fdef = new FixtureDef(); 53 | CircleShape shape = new CircleShape(); 54 | shape.setRadius(6 / MarioBros.PPM); 55 | fdef.filter.categoryBits = MarioBros.ENEMY_BIT; 56 | fdef.filter.maskBits = MarioBros.GROUND_BIT | 57 | MarioBros.COIN_BIT | 58 | MarioBros.BRICK_BIT | 59 | MarioBros.ENEMY_BIT | 60 | MarioBros.OBJECT_BIT | 61 | MarioBros.MARIO_BIT; 62 | 63 | fdef.shape = shape; 64 | b2body.createFixture(fdef).setUserData(this); 65 | 66 | //Create the Head here: 67 | PolygonShape head = new PolygonShape(); 68 | Vector2[] vertice = new Vector2[4]; 69 | vertice[0] = new Vector2(-5, 8).scl(1 / MarioBros.PPM); 70 | vertice[1] = new Vector2(5, 8).scl(1 / MarioBros.PPM); 71 | vertice[2] = new Vector2(-3, 3).scl(1 / MarioBros.PPM); 72 | vertice[3] = new Vector2(3, 3).scl(1 / MarioBros.PPM); 73 | head.set(vertice); 74 | 75 | fdef.shape = head; 76 | fdef.restitution = 1.8f; 77 | fdef.filter.categoryBits = MarioBros.ENEMY_HEAD_BIT; 78 | b2body.createFixture(fdef).setUserData(this); 79 | } 80 | 81 | public TextureRegion getFrame(float dt){ 82 | TextureRegion region; 83 | 84 | switch (currentState){ 85 | case MOVING_SHELL: 86 | case STANDING_SHELL: 87 | region = shell; 88 | break; 89 | case WALKING: 90 | default: 91 | region = walkAnimation.getKeyFrame(stateTime, true); 92 | break; 93 | } 94 | 95 | if(velocity.x > 0 && region.isFlipX() == false){ 96 | region.flip(true, false); 97 | } 98 | if(velocity.x < 0 && region.isFlipX() == true){ 99 | region.flip(true, false); 100 | } 101 | stateTime = currentState == previousState ? stateTime + dt : 0; 102 | //update previous state 103 | previousState = currentState; 104 | //return our final adjusted frame 105 | return region; 106 | } 107 | 108 | @Override 109 | public void update(float dt) { 110 | setRegion(getFrame(dt)); 111 | if(currentState == State.STANDING_SHELL && stateTime > 5){ 112 | currentState = State.WALKING; 113 | velocity.x = 1; 114 | System.out.println("WAKE UP SHELL"); 115 | } 116 | 117 | setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - 8 /MarioBros.PPM); 118 | b2body.setLinearVelocity(velocity); 119 | } 120 | 121 | @Override 122 | public void hitOnHead(Mario mario) { 123 | if(currentState == State.STANDING_SHELL) { 124 | if(mario.b2body.getPosition().x > b2body.getPosition().x) 125 | velocity.x = -2; 126 | else 127 | velocity.x = 2; 128 | currentState = State.MOVING_SHELL; 129 | System.out.println("Set to moving shell"); 130 | } 131 | else { 132 | currentState = State.STANDING_SHELL; 133 | velocity.x = 0; 134 | } 135 | } 136 | 137 | @Override 138 | public void hitByEnemy(Enemy enemy) { 139 | reverseVelocity(true, false); 140 | } 141 | 142 | public void kick(int direction){ 143 | velocity.x = direction; 144 | currentState = State.MOVING_SHELL; 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Screens/PlayScreen.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Screens; 2 | 3 | import com.badlogic.gdx.Gdx; 4 | import com.badlogic.gdx.Input; 5 | import com.badlogic.gdx.Screen; 6 | import com.badlogic.gdx.audio.Music; 7 | import com.badlogic.gdx.graphics.GL20; 8 | import com.badlogic.gdx.graphics.OrthographicCamera; 9 | import com.badlogic.gdx.graphics.g2d.TextureAtlas; 10 | import com.badlogic.gdx.maps.tiled.TiledMap; 11 | import com.badlogic.gdx.maps.tiled.TmxMapLoader; 12 | import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; 13 | import com.badlogic.gdx.math.Vector2; 14 | import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; 15 | import com.badlogic.gdx.physics.box2d.World; 16 | import com.badlogic.gdx.utils.Array; 17 | import com.badlogic.gdx.utils.viewport.FitViewport; 18 | import com.badlogic.gdx.utils.viewport.Viewport; 19 | import com.brentaureli.mariobros.MarioBros; 20 | import com.brentaureli.mariobros.Scenes.Hud; 21 | import com.brentaureli.mariobros.Sprites.Enemies.Enemy; 22 | import com.brentaureli.mariobros.Sprites.Items.Item; 23 | import com.brentaureli.mariobros.Sprites.Items.ItemDef; 24 | import com.brentaureli.mariobros.Sprites.Items.Mushroom; 25 | import com.brentaureli.mariobros.Sprites.Mario; 26 | import com.brentaureli.mariobros.Tools.B2WorldCreator; 27 | import com.brentaureli.mariobros.Tools.WorldContactListener; 28 | 29 | import java.util.PriorityQueue; 30 | import java.util.concurrent.LinkedBlockingQueue; 31 | 32 | /** 33 | * Created by brentaureli on 8/14/15. 34 | */ 35 | public class PlayScreen implements Screen{ 36 | //Reference to our Game, used to set Screens 37 | private MarioBros game; 38 | private TextureAtlas atlas; 39 | public static boolean alreadyDestroyed = false; 40 | 41 | //basic playscreen variables 42 | private OrthographicCamera gamecam; 43 | private Viewport gamePort; 44 | private Hud hud; 45 | 46 | //Tiled map variables 47 | private TmxMapLoader maploader; 48 | private TiledMap map; 49 | private OrthogonalTiledMapRenderer renderer; 50 | 51 | //Box2d variables 52 | private World world; 53 | private Box2DDebugRenderer b2dr; 54 | private B2WorldCreator creator; 55 | 56 | //sprites 57 | private Mario player; 58 | 59 | private Music music; 60 | 61 | private Array items; 62 | private LinkedBlockingQueue itemsToSpawn; 63 | 64 | 65 | public PlayScreen(MarioBros game){ 66 | atlas = new TextureAtlas("Mario_and_Enemies.pack"); 67 | 68 | this.game = game; 69 | //create cam used to follow mario through cam world 70 | gamecam = new OrthographicCamera(); 71 | 72 | //create a FitViewport to maintain virtual aspect ratio despite screen size 73 | gamePort = new FitViewport(MarioBros.V_WIDTH / MarioBros.PPM, MarioBros.V_HEIGHT / MarioBros.PPM, gamecam); 74 | 75 | //create our game HUD for scores/timers/level info 76 | hud = new Hud(game.batch); 77 | 78 | //Load our map and setup our map renderer 79 | maploader = new TmxMapLoader(); 80 | map = maploader.load("level1.tmx"); 81 | renderer = new OrthogonalTiledMapRenderer(map, 1 / MarioBros.PPM); 82 | 83 | //initially set our gamcam to be centered correctly at the start of of map 84 | gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0); 85 | 86 | //create our Box2D world, setting no gravity in X, -10 gravity in Y, and allow bodies to sleep 87 | world = new World(new Vector2(0, -10), true); 88 | //allows for debug lines of our box2d world. 89 | b2dr = new Box2DDebugRenderer(); 90 | 91 | creator = new B2WorldCreator(this); 92 | 93 | //create mario in our game world 94 | player = new Mario(this); 95 | 96 | world.setContactListener(new WorldContactListener()); 97 | 98 | music = MarioBros.manager.get("audio/music/mario_music.ogg", Music.class); 99 | music.setLooping(true); 100 | music.setVolume(0.3f); 101 | //music.play(); 102 | 103 | items = new Array(); 104 | itemsToSpawn = new LinkedBlockingQueue(); 105 | } 106 | 107 | public void spawnItem(ItemDef idef){ 108 | itemsToSpawn.add(idef); 109 | } 110 | 111 | 112 | public void handleSpawningItems(){ 113 | if(!itemsToSpawn.isEmpty()){ 114 | ItemDef idef = itemsToSpawn.poll(); 115 | if(idef.type == Mushroom.class){ 116 | items.add(new Mushroom(this, idef.position.x, idef.position.y)); 117 | } 118 | } 119 | } 120 | 121 | 122 | public TextureAtlas getAtlas(){ 123 | return atlas; 124 | } 125 | 126 | @Override 127 | public void show() { 128 | 129 | 130 | } 131 | 132 | public void handleInput(float dt){ 133 | //control our player using immediate impulses 134 | if(player.currentState != Mario.State.DEAD) { 135 | if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) 136 | player.jump(); 137 | if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2) 138 | player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true); 139 | if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2) 140 | player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true); 141 | if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) 142 | player.fire(); 143 | } 144 | 145 | } 146 | 147 | public void update(float dt){ 148 | //handle user input first 149 | handleInput(dt); 150 | handleSpawningItems(); 151 | 152 | //takes 1 step in the physics simulation(60 times per second) 153 | world.step(1 / 60f, 6, 2); 154 | 155 | player.update(dt); 156 | for(Enemy enemy : creator.getEnemies()) { 157 | enemy.update(dt); 158 | if(enemy.getX() < player.getX() + 224 / MarioBros.PPM) { 159 | enemy.b2body.setActive(true); 160 | } 161 | } 162 | 163 | for(Item item : items) 164 | item.update(dt); 165 | 166 | hud.update(dt); 167 | 168 | //attach our gamecam to our players.x coordinate 169 | if(player.currentState != Mario.State.DEAD) { 170 | gamecam.position.x = player.b2body.getPosition().x; 171 | } 172 | 173 | //update our gamecam with correct coordinates after changes 174 | gamecam.update(); 175 | //tell our renderer to draw only what our camera can see in our game world. 176 | renderer.setView(gamecam); 177 | 178 | } 179 | 180 | 181 | @Override 182 | public void render(float delta) { 183 | //separate our update logic from render 184 | update(delta); 185 | 186 | //Clear the game screen with Black 187 | Gdx.gl.glClearColor(0, 0, 0, 1); 188 | Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 189 | 190 | //render our game map 191 | renderer.render(); 192 | 193 | //renderer our Box2DDebugLines 194 | b2dr.render(world, gamecam.combined); 195 | 196 | game.batch.setProjectionMatrix(gamecam.combined); 197 | game.batch.begin(); 198 | player.draw(game.batch); 199 | for (Enemy enemy : creator.getEnemies()) 200 | enemy.draw(game.batch); 201 | for (Item item : items) 202 | item.draw(game.batch); 203 | game.batch.end(); 204 | 205 | //Set our batch to now draw what the Hud camera sees. 206 | game.batch.setProjectionMatrix(hud.stage.getCamera().combined); 207 | hud.stage.draw(); 208 | 209 | if(gameOver()){ 210 | game.setScreen(new GameOverScreen(game)); 211 | dispose(); 212 | } 213 | 214 | } 215 | 216 | public boolean gameOver(){ 217 | if(player.currentState == Mario.State.DEAD && player.getStateTimer() > 3){ 218 | return true; 219 | } 220 | return false; 221 | } 222 | 223 | @Override 224 | public void resize(int width, int height) { 225 | //updated our game viewport 226 | gamePort.update(width,height); 227 | 228 | } 229 | 230 | public TiledMap getMap(){ 231 | return map; 232 | } 233 | public World getWorld(){ 234 | return world; 235 | } 236 | 237 | @Override 238 | public void pause() { 239 | 240 | } 241 | 242 | @Override 243 | public void resume() { 244 | 245 | } 246 | 247 | @Override 248 | public void hide() { 249 | 250 | } 251 | 252 | @Override 253 | public void dispose() { 254 | //dispose of all our opened resources 255 | map.dispose(); 256 | renderer.dispose(); 257 | world.dispose(); 258 | b2dr.dispose(); 259 | hud.dispose(); 260 | } 261 | 262 | public Hud getHud(){ return hud; } 263 | } 264 | -------------------------------------------------------------------------------- /android/assets/level1.tmx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | eJztwzEJAAAMA7Ay/4bqrir2JZBeUlVVVVVVVVVVVVVV1ccDxULniQ== 117 | 118 | 119 | 120 | 121 | eJztl01OwzAQhd2wAtoiYNWtl0CLBBSk3oyDAGsuAeRs2TBRO8p08PgndpomWOqnOM7Yfh77xU1ZKFXuOFFNOZPJDAvuX7yvr1XRQOspb1D3DnxEaMA+YvuJGTt23KHPIevfspgcdu4xoC/Ro1jH6+k9ra/LX3D9Bn488irlFvuw9dPVfnDpH8Mcsn5/NpNw/X1Bz1V6j16WPEuhbW1rk3JvxO6Htv0NdQ5Zvz/o39R7qAu4f8vir39pHfd3xfpa/KNvafhZkWJjchTaVtKDzxY7TM/HhlZbXHGbAf5/pmep9N3b9vzNZPpGq/H613RfMUpDWYrLZI4Jrcbr30xmzGiVzr9T1dD3vGLh32EF+Y7q8nvqFHJ7BiiP/kNifUkxP6ltIfRLcy2VffoPGRPRylx2oVX7tlxb23yjBo6rneTfGbSdAxeR+wn3ZUwfx0b93/vTY2438OyWePIqIPZYsc2hT7TBg9qQS65fG7wite1auwmXfpN/6zN3SYg5g3Ff9r2+IfvO953jmhv15CVcH4BrId7l32PwjWsOKdcgBG3xIK3j+rXgFVMd1R97JtH2XIOkScq/yb/z3dm7Uk05dA3OIW4KzA7s3zWM9Ux4mdj33ZrEYZ3vO4fGrdmYFXmO4z9adGBsCv0pcsf785lDyjXwhe93HJPXc/02r/B6rp/vj1D92N6kQdJE9T+xd5Dk33vi31fHGnDuIGYJrBL4tz6WMvukyk3Oc/qcZvb5BRdfbQI= 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | -------------------------------------------------------------------------------- /core/src/com/brentaureli/mariobros/Sprites/Mario.java: -------------------------------------------------------------------------------- 1 | package com.brentaureli.mariobros.Sprites; 2 | 3 | import com.badlogic.gdx.audio.Music; 4 | import com.badlogic.gdx.audio.Sound; 5 | import com.badlogic.gdx.graphics.g2d.Animation; 6 | import com.badlogic.gdx.graphics.g2d.Batch; 7 | import com.badlogic.gdx.graphics.g2d.Sprite; 8 | import com.badlogic.gdx.graphics.g2d.TextureRegion; 9 | import com.badlogic.gdx.math.Vector2; 10 | import com.badlogic.gdx.physics.box2d.Body; 11 | import com.badlogic.gdx.physics.box2d.BodyDef; 12 | import com.badlogic.gdx.physics.box2d.CircleShape; 13 | import com.badlogic.gdx.physics.box2d.EdgeShape; 14 | import com.badlogic.gdx.physics.box2d.Filter; 15 | import com.badlogic.gdx.physics.box2d.Fixture; 16 | import com.badlogic.gdx.physics.box2d.FixtureDef; 17 | import com.badlogic.gdx.physics.box2d.World; 18 | import com.badlogic.gdx.utils.Array; 19 | import com.brentaureli.mariobros.MarioBros; 20 | import com.brentaureli.mariobros.Screens.PlayScreen; 21 | import com.brentaureli.mariobros.Sprites.Other.FireBall; 22 | import com.brentaureli.mariobros.Sprites.Enemies.*; 23 | 24 | /** 25 | * Created by brentaureli on 8/27/15. 26 | */ 27 | public class Mario extends Sprite { 28 | public enum State { FALLING, JUMPING, STANDING, RUNNING, GROWING, DEAD }; 29 | public State currentState; 30 | public State previousState; 31 | 32 | public World world; 33 | public Body b2body; 34 | 35 | private TextureRegion marioStand; 36 | private Animation marioRun; 37 | private TextureRegion marioJump; 38 | private TextureRegion marioDead; 39 | private TextureRegion bigMarioStand; 40 | private TextureRegion bigMarioJump; 41 | private Animation bigMarioRun; 42 | private Animation growMario; 43 | 44 | private float stateTimer; 45 | private boolean runningRight; 46 | private boolean marioIsBig; 47 | private boolean runGrowAnimation; 48 | private boolean timeToDefineBigMario; 49 | private boolean timeToRedefineMario; 50 | private boolean marioIsDead; 51 | private PlayScreen screen; 52 | 53 | private Array fireballs; 54 | 55 | public Mario(PlayScreen screen){ 56 | //initialize default values 57 | this.screen = screen; 58 | this.world = screen.getWorld(); 59 | currentState = State.STANDING; 60 | previousState = State.STANDING; 61 | stateTimer = 0; 62 | runningRight = true; 63 | 64 | Array frames = new Array(); 65 | 66 | //get run animation frames and add them to marioRun Animation 67 | for(int i = 1; i < 4; i++) 68 | frames.add(new TextureRegion(screen.getAtlas().findRegion("little_mario"), i * 16, 0, 16, 16)); 69 | marioRun = new Animation(0.1f, frames); 70 | 71 | frames.clear(); 72 | 73 | for(int i = 1; i < 4; i++) 74 | frames.add(new TextureRegion(screen.getAtlas().findRegion("big_mario"), i * 16, 0, 16, 32)); 75 | bigMarioRun = new Animation(0.1f, frames); 76 | 77 | frames.clear(); 78 | 79 | //get set animation frames from growing mario 80 | frames.add(new TextureRegion(screen.getAtlas().findRegion("big_mario"), 240, 0, 16, 32)); 81 | frames.add(new TextureRegion(screen.getAtlas().findRegion("big_mario"), 0, 0, 16, 32)); 82 | frames.add(new TextureRegion(screen.getAtlas().findRegion("big_mario"), 240, 0, 16, 32)); 83 | frames.add(new TextureRegion(screen.getAtlas().findRegion("big_mario"), 0, 0, 16, 32)); 84 | growMario = new Animation(0.2f, frames); 85 | 86 | 87 | //get jump animation frames and add them to marioJump Animation 88 | marioJump = new TextureRegion(screen.getAtlas().findRegion("little_mario"), 80, 0, 16, 16); 89 | bigMarioJump = new TextureRegion(screen.getAtlas().findRegion("big_mario"), 80, 0, 16, 32); 90 | 91 | //create texture region for mario standing 92 | marioStand = new TextureRegion(screen.getAtlas().findRegion("little_mario"), 0, 0, 16, 16); 93 | bigMarioStand = new TextureRegion(screen.getAtlas().findRegion("big_mario"), 0, 0, 16, 32); 94 | 95 | //create dead mario texture region 96 | marioDead = new TextureRegion(screen.getAtlas().findRegion("little_mario"), 96, 0, 16, 16); 97 | 98 | //define mario in Box2d 99 | defineMario(); 100 | 101 | //set initial values for marios location, width and height. And initial frame as marioStand. 102 | setBounds(0, 0, 16 / MarioBros.PPM, 16 / MarioBros.PPM); 103 | setRegion(marioStand); 104 | 105 | fireballs = new Array(); 106 | 107 | } 108 | 109 | public void update(float dt){ 110 | 111 | // time is up : too late mario dies T_T 112 | // the !isDead() method is used to prevent multiple invocation 113 | // of "die music" and jumping 114 | // there is probably better ways to do that but it works for now. 115 | if (screen.getHud().isTimeUp() && !isDead()) { 116 | die(); 117 | } 118 | 119 | //update our sprite to correspond with the position of our Box2D body 120 | if(marioIsBig) 121 | setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2 - 6 / MarioBros.PPM); 122 | else 123 | setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2); 124 | //update sprite with the correct frame depending on marios current action 125 | setRegion(getFrame(dt)); 126 | if(timeToDefineBigMario) 127 | defineBigMario(); 128 | if(timeToRedefineMario) 129 | redefineMario(); 130 | 131 | for(FireBall ball : fireballs) { 132 | ball.update(dt); 133 | if(ball.isDestroyed()) 134 | fireballs.removeValue(ball, true); 135 | } 136 | 137 | } 138 | 139 | public TextureRegion getFrame(float dt){ 140 | //get marios current state. ie. jumping, running, standing... 141 | currentState = getState(); 142 | 143 | TextureRegion region; 144 | 145 | //depending on the state, get corresponding animation keyFrame. 146 | switch(currentState){ 147 | case DEAD: 148 | region = marioDead; 149 | break; 150 | case GROWING: 151 | region = growMario.getKeyFrame(stateTimer); 152 | if(growMario.isAnimationFinished(stateTimer)) { 153 | runGrowAnimation = false; 154 | } 155 | break; 156 | case JUMPING: 157 | region = marioIsBig ? bigMarioJump : marioJump; 158 | break; 159 | case RUNNING: 160 | region = marioIsBig ? bigMarioRun.getKeyFrame(stateTimer, true) : marioRun.getKeyFrame(stateTimer, true); 161 | break; 162 | case FALLING: 163 | case STANDING: 164 | default: 165 | region = marioIsBig ? bigMarioStand : marioStand; 166 | break; 167 | } 168 | 169 | //if mario is running left and the texture isnt facing left... flip it. 170 | if((b2body.getLinearVelocity().x < 0 || !runningRight) && !region.isFlipX()){ 171 | region.flip(true, false); 172 | runningRight = false; 173 | } 174 | 175 | //if mario is running right and the texture isnt facing right... flip it. 176 | else if((b2body.getLinearVelocity().x > 0 || runningRight) && region.isFlipX()){ 177 | region.flip(true, false); 178 | runningRight = true; 179 | } 180 | 181 | //if the current state is the same as the previous state increase the state timer. 182 | //otherwise the state has changed and we need to reset timer. 183 | stateTimer = currentState == previousState ? stateTimer + dt : 0; 184 | //update previous state 185 | previousState = currentState; 186 | //return our final adjusted frame 187 | return region; 188 | 189 | } 190 | 191 | public State getState(){ 192 | //Test to Box2D for velocity on the X and Y-Axis 193 | //if mario is going positive in Y-Axis he is jumping... or if he just jumped and is falling remain in jump state 194 | if(marioIsDead) 195 | return State.DEAD; 196 | else if(runGrowAnimation) 197 | return State.GROWING; 198 | else if((b2body.getLinearVelocity().y > 0 && currentState == State.JUMPING) || (b2body.getLinearVelocity().y < 0 && previousState == State.JUMPING)) 199 | return State.JUMPING; 200 | //if negative in Y-Axis mario is falling 201 | else if(b2body.getLinearVelocity().y < 0) 202 | return State.FALLING; 203 | //if mario is positive or negative in the X axis he is running 204 | else if(b2body.getLinearVelocity().x != 0) 205 | return State.RUNNING; 206 | //if none of these return then he must be standing 207 | else 208 | return State.STANDING; 209 | } 210 | 211 | public void grow(){ 212 | if( !isBig() ) { 213 | runGrowAnimation = true; 214 | marioIsBig = true; 215 | timeToDefineBigMario = true; 216 | setBounds(getX(), getY(), getWidth(), getHeight() * 2); 217 | MarioBros.manager.get("audio/sounds/powerup.wav", Sound.class).play(); 218 | } 219 | } 220 | 221 | public void die() { 222 | 223 | if (!isDead()) { 224 | 225 | MarioBros.manager.get("audio/music/mario_music.ogg", Music.class).stop(); 226 | MarioBros.manager.get("audio/sounds/mariodie.wav", Sound.class).play(); 227 | marioIsDead = true; 228 | Filter filter = new Filter(); 229 | filter.maskBits = MarioBros.NOTHING_BIT; 230 | 231 | for (Fixture fixture : b2body.getFixtureList()) { 232 | fixture.setFilterData(filter); 233 | } 234 | 235 | b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true); 236 | } 237 | } 238 | 239 | public boolean isDead(){ 240 | return marioIsDead; 241 | } 242 | 243 | public float getStateTimer(){ 244 | return stateTimer; 245 | } 246 | 247 | public boolean isBig(){ 248 | return marioIsBig; 249 | } 250 | 251 | public void jump(){ 252 | if ( currentState != State.JUMPING ) { 253 | b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true); 254 | currentState = State.JUMPING; 255 | } 256 | } 257 | 258 | public void hit(Enemy enemy){ 259 | if(enemy instanceof Turtle && ((Turtle) enemy).currentState == Turtle.State.STANDING_SHELL) 260 | ((Turtle) enemy).kick(enemy.b2body.getPosition().x > b2body.getPosition().x ? Turtle.KICK_RIGHT : Turtle.KICK_LEFT); 261 | else { 262 | if (marioIsBig) { 263 | marioIsBig = false; 264 | timeToRedefineMario = true; 265 | setBounds(getX(), getY(), getWidth(), getHeight() / 2); 266 | MarioBros.manager.get("audio/sounds/powerdown.wav", Sound.class).play(); 267 | } else { 268 | die(); 269 | } 270 | } 271 | } 272 | 273 | public void redefineMario(){ 274 | Vector2 position = b2body.getPosition(); 275 | world.destroyBody(b2body); 276 | 277 | BodyDef bdef = new BodyDef(); 278 | bdef.position.set(position); 279 | bdef.type = BodyDef.BodyType.DynamicBody; 280 | b2body = world.createBody(bdef); 281 | 282 | FixtureDef fdef = new FixtureDef(); 283 | CircleShape shape = new CircleShape(); 284 | shape.setRadius(6 / MarioBros.PPM); 285 | fdef.filter.categoryBits = MarioBros.MARIO_BIT; 286 | fdef.filter.maskBits = MarioBros.GROUND_BIT | 287 | MarioBros.COIN_BIT | 288 | MarioBros.BRICK_BIT | 289 | MarioBros.ENEMY_BIT | 290 | MarioBros.OBJECT_BIT | 291 | MarioBros.ENEMY_HEAD_BIT | 292 | MarioBros.ITEM_BIT; 293 | 294 | fdef.shape = shape; 295 | b2body.createFixture(fdef).setUserData(this); 296 | 297 | EdgeShape head = new EdgeShape(); 298 | head.set(new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); 299 | fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; 300 | fdef.shape = head; 301 | fdef.isSensor = true; 302 | 303 | b2body.createFixture(fdef).setUserData(this); 304 | 305 | timeToRedefineMario = false; 306 | 307 | } 308 | 309 | public void defineBigMario(){ 310 | Vector2 currentPosition = b2body.getPosition(); 311 | world.destroyBody(b2body); 312 | 313 | BodyDef bdef = new BodyDef(); 314 | bdef.position.set(currentPosition.add(0, 10 / MarioBros.PPM)); 315 | bdef.type = BodyDef.BodyType.DynamicBody; 316 | b2body = world.createBody(bdef); 317 | 318 | FixtureDef fdef = new FixtureDef(); 319 | CircleShape shape = new CircleShape(); 320 | shape.setRadius(6 / MarioBros.PPM); 321 | fdef.filter.categoryBits = MarioBros.MARIO_BIT; 322 | fdef.filter.maskBits = MarioBros.GROUND_BIT | 323 | MarioBros.COIN_BIT | 324 | MarioBros.BRICK_BIT | 325 | MarioBros.ENEMY_BIT | 326 | MarioBros.OBJECT_BIT | 327 | MarioBros.ENEMY_HEAD_BIT | 328 | MarioBros.ITEM_BIT; 329 | 330 | fdef.shape = shape; 331 | b2body.createFixture(fdef).setUserData(this); 332 | shape.setPosition(new Vector2(0, -14 / MarioBros.PPM)); 333 | b2body.createFixture(fdef).setUserData(this); 334 | 335 | EdgeShape head = new EdgeShape(); 336 | head.set(new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); 337 | fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; 338 | fdef.shape = head; 339 | fdef.isSensor = true; 340 | 341 | b2body.createFixture(fdef).setUserData(this); 342 | timeToDefineBigMario = false; 343 | } 344 | 345 | public void defineMario(){ 346 | BodyDef bdef = new BodyDef(); 347 | bdef.position.set(32 / MarioBros.PPM, 32 / MarioBros.PPM); 348 | bdef.type = BodyDef.BodyType.DynamicBody; 349 | b2body = world.createBody(bdef); 350 | 351 | FixtureDef fdef = new FixtureDef(); 352 | CircleShape shape = new CircleShape(); 353 | shape.setRadius(6 / MarioBros.PPM); 354 | fdef.filter.categoryBits = MarioBros.MARIO_BIT; 355 | fdef.filter.maskBits = MarioBros.GROUND_BIT | 356 | MarioBros.COIN_BIT | 357 | MarioBros.BRICK_BIT | 358 | MarioBros.ENEMY_BIT | 359 | MarioBros.OBJECT_BIT | 360 | MarioBros.ENEMY_HEAD_BIT | 361 | MarioBros.ITEM_BIT; 362 | 363 | fdef.shape = shape; 364 | b2body.createFixture(fdef).setUserData(this); 365 | 366 | EdgeShape head = new EdgeShape(); 367 | head.set(new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); 368 | fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; 369 | fdef.shape = head; 370 | fdef.isSensor = true; 371 | 372 | b2body.createFixture(fdef).setUserData(this); 373 | } 374 | 375 | public void fire(){ 376 | fireballs.add(new FireBall(screen, b2body.getPosition().x, b2body.getPosition().y, runningRight ? true : false)); 377 | } 378 | 379 | public void draw(Batch batch){ 380 | super.draw(batch); 381 | for(FireBall ball : fireballs) 382 | ball.draw(batch); 383 | } 384 | } 385 | --------------------------------------------------------------------------------