Created
January 8, 2018 07:02
-
-
Save Redas17/539382e654fe13613f70cf21cf515f22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Tools; | |
import com.badlogic.gdx.maps.MapObject; | |
import com.badlogic.gdx.maps.objects.RectangleMapObject; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.math.Rectangle; | |
import com.badlogic.gdx.physics.box2d.Body; | |
import com.badlogic.gdx.physics.box2d.BodyDef; | |
import com.badlogic.gdx.physics.box2d.FixtureDef; | |
import com.badlogic.gdx.physics.box2d.PolygonShape; | |
import com.badlogic.gdx.physics.box2d.World; | |
import com.badlogic.gdx.utils.Array; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Screens.PlayScreen; | |
import com.redsoft.game.Sprites.Enemies.Goomba; | |
import com.redsoft.game.Sprites.TileObjects.Brick; | |
import com.redsoft.game.Sprites.TileObjects.Coin; | |
/** | |
* Created by REDAS on 16.12.2017. | |
*/ | |
public class B2WorldCreator { | |
private Array<Goomba> goombas; | |
public B2WorldCreator(PlayScreen screen) { | |
World world = screen.getWorld(); | |
TiledMap map = screen.getMap(); | |
BodyDef bdef = new BodyDef(); | |
PolygonShape shape = new PolygonShape(); | |
FixtureDef fdef = new FixtureDef(); | |
Body body; | |
for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) { | |
Rectangle rect = ((RectangleMapObject) object).getRectangle(); | |
bdef.type = BodyDef.BodyType.StaticBody; | |
bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM, (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM); | |
body = world.createBody(bdef); | |
shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM); | |
fdef.shape = shape; | |
body.createFixture(fdef); | |
} | |
for (MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) { | |
Rectangle rect = ((RectangleMapObject) object).getRectangle(); | |
bdef.type = BodyDef.BodyType.StaticBody; | |
bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM, (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM); | |
body = world.createBody(bdef); | |
shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM); | |
fdef.shape = shape; | |
fdef.filter.categoryBits = MarioBros.OBJECT_BIT; | |
body.createFixture(fdef); | |
} | |
for (MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) { | |
Rectangle rect = ((RectangleMapObject) object).getRectangle(); | |
new Brick(screen, rect); | |
} | |
for (MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)) { | |
Rectangle rect = ((RectangleMapObject) object).getRectangle(); | |
new Coin(screen, rect); | |
} | |
//create all goombas | |
goombas = new Array<Goomba>(); | |
for (MapObject object : map.getLayers().get(6).getObjects().getByType(RectangleMapObject.class)) { | |
Rectangle rect = ((RectangleMapObject) object).getRectangle(); | |
goombas.add(new Goomba(screen, rect.getX() / MarioBros.PPM, rect.getY() / MarioBros.PPM)); | |
} | |
} | |
public Array<Goomba> getGoombas() { | |
return goombas; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.TileObjects; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.audio.Sound; | |
import com.badlogic.gdx.math.Rectangle; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Scenes.Hud; | |
import com.redsoft.game.Screens.PlayScreen; | |
/** | |
* Created by REDAS on 16.12.2017. | |
*/ | |
public class Brick extends com.redsoft.game.Sprites.TileObjects.InteractiveTileObject { | |
public Brick(PlayScreen screen, Rectangle bounds) { | |
super(screen, bounds); | |
fixture.setUserData(this); | |
setCategoryFilter(MarioBros.BRICK_BIT); | |
} | |
@Override | |
public void onHeadHit() { | |
Gdx.app.log("Brick", "Collision"); | |
setCategoryFilter(MarioBros.DESTROYED_BIT); | |
getCell().setTile(null); | |
Hud.addScore(200); | |
MarioBros.manager.get("audio/sounds/breakblock.wav", Sound.class).play(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.TileObjects; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.audio.Sound; | |
import com.badlogic.gdx.maps.tiled.TiledMapTileSet; | |
import com.badlogic.gdx.math.Rectangle; | |
import com.badlogic.gdx.math.Vector2; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Scenes.Hud; | |
import com.redsoft.game.Screens.PlayScreen; | |
import com.redsoft.game.Sprites.Items.ItemDef; | |
import com.redsoft.game.Sprites.Items.Mushroom; | |
/** | |
* Created by REDAS on 16.12.2017. | |
*/ | |
public class Coin extends InteractiveTileObject { | |
private static TiledMapTileSet tileSet; | |
private final int BLANK_COIN = 28; | |
public Coin(PlayScreen screen, Rectangle bounds) { | |
super(screen, bounds); | |
tileSet = map.getTileSets().getTileSet("tileset_gutter"); | |
fixture.setUserData(this); | |
setCategoryFilter(MarioBros.COIN_BIT); | |
} | |
@Override | |
public void onHeadHit() { | |
Gdx.app.log("Coin", "Collision"); | |
if (getCell().getTile().getId() == BLANK_COIN) | |
MarioBros.manager.get("audio/sounds/bump.wav", Sound.class).play(); | |
else { | |
MarioBros.manager.get("audio/sounds/coin.wav", Sound.class).play(); | |
screen.spawnItem(new ItemDef(new Vector2(body.getPosition().x, body.getPosition().y + 16 / MarioBros.PPM), Mushroom.class)); | |
} | |
getCell().setTile(tileSet.getTile(BLANK_COIN)); | |
Hud.addScore(100); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.Enemies; | |
import com.badlogic.gdx.graphics.g2d.Sprite; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.Body; | |
import com.badlogic.gdx.physics.box2d.World; | |
import com.redsoft.game.Screens.PlayScreen; | |
/** | |
* Created by REDAS on 19.12.2017. | |
*/ | |
public abstract class Enemy extends Sprite { | |
protected World world; | |
protected PlayScreen screen; | |
protected boolean destroyed; | |
public Body b2body; | |
public Vector2 velocity; | |
public Enemy(PlayScreen screen, float x, float y) { | |
this.world = screen.getWorld(); | |
this.screen = screen; | |
setPosition(x, y); | |
defineEnemy(); | |
velocity = new Vector2(1, 0); | |
b2body.setActive(false); | |
} | |
protected abstract void defineEnemy(); | |
public abstract void update(float dt); | |
public abstract void hitOnHead(); | |
public void reverseVelocity(boolean x, boolean y) { | |
if (x) | |
velocity.x = -velocity.x; | |
if (y) | |
velocity.y = -velocity.y; | |
} | |
public boolean isDestroyed() { | |
return destroyed; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.Enemies; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
import com.badlogic.gdx.graphics.g2d.Batch; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.BodyDef; | |
import com.badlogic.gdx.physics.box2d.CircleShape; | |
import com.badlogic.gdx.physics.box2d.FixtureDef; | |
import com.badlogic.gdx.physics.box2d.PolygonShape; | |
import com.badlogic.gdx.utils.Array; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Screens.PlayScreen; | |
/** | |
* Created by REDAS on 19.12.2017. | |
*/ | |
public class Goomba extends Enemy { | |
private float stateTime; | |
private Animation walkAnimation; | |
private Array<TextureRegion> frames; | |
private boolean setToDestroy; | |
//private boolean destroyed; | |
public Goomba(PlayScreen screen, float x, float y) { | |
super(screen, x, y); | |
frames = new Array<TextureRegion>(); | |
for (int i = 0; i < 2; i++) | |
frames.add(new TextureRegion(screen.getAtlas().findRegion("goomba"), i * 16, 0, 16, 16)); | |
walkAnimation = new Animation(0.4f, frames); | |
stateTime = 0; | |
setBounds(getX(), getY(), 16 / MarioBros.PPM, 16 / MarioBros.PPM); | |
setToDestroy = false; | |
destroyed = false; | |
} | |
public void update(float dt) { | |
stateTime += dt; | |
if (setToDestroy && !destroyed) { | |
world.destroyBody(b2body); | |
destroyed = true; | |
setRegion(new TextureRegion(screen.getAtlas().findRegion("goomba"), 32, 0, 16, 16)); | |
stateTime = 0; | |
} else if (!destroyed) { | |
b2body.setLinearVelocity(velocity); | |
setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2); | |
setRegion((TextureRegion) walkAnimation.getKeyFrame(stateTime, true)); | |
} | |
} | |
@Override | |
protected void defineEnemy() { | |
BodyDef bdef = new BodyDef(); | |
bdef.position.set(getX(), getY()); | |
bdef.type = BodyDef.BodyType.DynamicBody; | |
b2body = world.createBody(bdef); | |
FixtureDef fdef = new FixtureDef(); | |
CircleShape shape = new CircleShape(); | |
shape.setRadius(6 / MarioBros.PPM); | |
fdef.filter.categoryBits = MarioBros.ENEMY_BIT; | |
fdef.filter.maskBits = MarioBros.GROUND_BIT | | |
MarioBros.COIN_BIT | | |
MarioBros.BRICK_BIT | | |
MarioBros.ENEMY_BIT | | |
MarioBros.OBJECT_BIT | | |
MarioBros.MARIO_BIT; | |
fdef.shape = shape; | |
b2body.createFixture(fdef).setUserData(this); | |
//Create the Head here: | |
PolygonShape head = new PolygonShape(); | |
Vector2[] vertice = new Vector2[4]; | |
vertice[0] = new Vector2(-5, 8).scl(1 / MarioBros.PPM); | |
vertice[1] = new Vector2(5, 8).scl(1 / MarioBros.PPM); | |
vertice[2] = new Vector2(-3, 3).scl(1 / MarioBros.PPM); | |
vertice[3] = new Vector2(3, 3).scl(1 / MarioBros.PPM); | |
head.set(vertice); | |
fdef.shape = head; | |
fdef.restitution = 0.5f; | |
fdef.filter.categoryBits = MarioBros.ENEMY_HEAD_BIT; | |
b2body.createFixture(fdef).setUserData(this); | |
} | |
public void draw(Batch batch) { | |
if (!destroyed || stateTime < 1) | |
super.draw(batch); | |
} | |
@Override | |
public void hitOnHead() { | |
setToDestroy = true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Scenes; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.scenes.scene2d.Stage; | |
import com.badlogic.gdx.scenes.scene2d.ui.Label; | |
import com.badlogic.gdx.scenes.scene2d.ui.Table; | |
import com.badlogic.gdx.utils.Disposable; | |
import com.badlogic.gdx.utils.viewport.FitViewport; | |
import com.badlogic.gdx.utils.viewport.Viewport; | |
import com.redsoft.game.MarioBros; | |
/** | |
* Created by REDAS on 15.12.2017. | |
*/ | |
public class Hud implements Disposable { | |
public Stage stage; | |
private Viewport viewport; | |
private Integer worldTimer; | |
private float timeCount; | |
private static Integer score; | |
protected Label countdownLabel; | |
protected static Label scoreLabel; | |
protected Label timeLabel; | |
protected Label levelLabel; | |
protected Label worldLabel; | |
protected Label marioLabel; | |
public Hud(SpriteBatch sb) { | |
worldTimer = 300; | |
timeCount = 0; | |
score = 0; | |
viewport = new FitViewport(MarioBros.V_WIDTH, MarioBros.V_HEIGHT, new OrthographicCamera()); | |
stage = new Stage(viewport, sb); | |
Table table = new Table(); | |
table.top(); | |
table.setFillParent(true); | |
countdownLabel = new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.WHITE)); | |
scoreLabel = new Label(String.format("%06d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE)); | |
timeLabel = new Label("TIME", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); | |
levelLabel = new Label("1-1", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); | |
worldLabel = new Label("WORLD", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); | |
marioLabel = new Label("MARIO", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); | |
table.add(marioLabel).expandX().padTop(10); | |
table.add(worldLabel).expandX().padTop(10); | |
table.add(timeLabel).expandX().padTop(10); | |
table.row(); | |
table.add(scoreLabel).expandX(); | |
table.add(levelLabel).expandX(); | |
table.add(countdownLabel).expand(); | |
stage.addActor(table); | |
} | |
public void update(float dt) { | |
timeCount += dt; | |
if (timeCount >= 1) { | |
worldTimer--; | |
countdownLabel.setText(String.format("%03d", worldTimer)); | |
timeCount = 0; | |
} | |
} | |
public static void addScore(int value) { | |
score += value; | |
scoreLabel.setText(String.format("%06d", score)); | |
} | |
@Override | |
public void dispose() { | |
stage.dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.TileObjects; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.maps.tiled.TiledMapTile; | |
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; | |
import com.badlogic.gdx.math.Rectangle; | |
import com.badlogic.gdx.physics.box2d.Body; | |
import com.badlogic.gdx.physics.box2d.BodyDef; | |
import com.badlogic.gdx.physics.box2d.Filter; | |
import com.badlogic.gdx.physics.box2d.Fixture; | |
import com.badlogic.gdx.physics.box2d.FixtureDef; | |
import com.badlogic.gdx.physics.box2d.PolygonShape; | |
import com.badlogic.gdx.physics.box2d.World; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Screens.PlayScreen; | |
/** | |
* Created by REDAS on 16.12.2017. | |
*/ | |
public abstract class InteractiveTileObject { | |
protected World world; | |
protected TiledMap map; | |
protected TiledMapTile tile; | |
protected Rectangle bounds; | |
protected Body body; | |
protected Fixture fixture; | |
protected PlayScreen screen; | |
public InteractiveTileObject(PlayScreen screen, Rectangle bounds) { | |
this.screen = screen; | |
this.world = screen.getWorld(); | |
this.map = screen.getMap(); | |
this.bounds = bounds; | |
BodyDef bdef = new BodyDef(); | |
FixtureDef fdef = new FixtureDef(); | |
PolygonShape shape = new PolygonShape(); | |
bdef.type = BodyDef.BodyType.StaticBody; | |
bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MarioBros.PPM, (bounds.getY() + bounds.getHeight() / 2) / MarioBros.PPM); | |
body = world.createBody(bdef); | |
shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM); | |
fdef.shape = shape; | |
fixture = body.createFixture(fdef); | |
} | |
public abstract void onHeadHit(); | |
public void setCategoryFilter(short filterBit) { | |
Filter filter = new Filter(); | |
filter.categoryBits = filterBit; | |
fixture.setFilterData(filter); | |
} | |
public TiledMapTileLayer.Cell getCell() { | |
TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); | |
return layer.getCell((int) (body.getPosition().x * MarioBros.PPM / 16), | |
(int) (body.getPosition().y * MarioBros.PPM / 16)); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.Items; | |
import com.badlogic.gdx.graphics.g2d.Batch; | |
import com.badlogic.gdx.graphics.g2d.Sprite; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.Body; | |
import com.badlogic.gdx.physics.box2d.World; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Screens.PlayScreen; | |
/** | |
* Created by REDAS on 06.01.2018. | |
*/ | |
public abstract class Item extends Sprite { | |
protected PlayScreen screen; | |
protected World world; | |
protected Vector2 velocity; | |
protected boolean toDestoy; | |
protected boolean destroyed; | |
protected Body body; | |
public Item(PlayScreen screen, float x, float y) { | |
this.screen = screen; | |
this.world = screen.getWorld(); | |
setPosition(x, y); | |
setBounds(getX(), getY(), 16 / MarioBros.PPM, 16 / MarioBros.PPM); | |
defineItem(); | |
toDestoy = false; | |
destroyed = false; | |
} | |
public abstract void defineItem(); | |
public abstract void use(); | |
public void update(float dt) { | |
if (toDestoy && !destroyed) { | |
world.destroyBody(body); | |
destroyed = true; | |
} | |
} | |
public void draw(Batch batch) { | |
if (!destroyed) | |
super.draw(batch); | |
} | |
public void destroy() { | |
toDestoy = true; | |
} | |
public boolean isDestroyed() { | |
return destroyed; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.Items; | |
import com.badlogic.gdx.math.Vector2; | |
/** | |
* Created by REDAS on 06.01.2018. | |
*/ | |
public class ItemDef { | |
public Vector2 position; | |
public Class<?> type; | |
public ItemDef(Vector2 position, Class<?> type) { | |
this.position = position; | |
this.type = type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
import com.badlogic.gdx.graphics.g2d.Sprite; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.Body; | |
import com.badlogic.gdx.physics.box2d.BodyDef; | |
import com.badlogic.gdx.physics.box2d.CircleShape; | |
import com.badlogic.gdx.physics.box2d.EdgeShape; | |
import com.badlogic.gdx.physics.box2d.FixtureDef; | |
import com.badlogic.gdx.physics.box2d.World; | |
import com.badlogic.gdx.utils.Array; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Screens.PlayScreen; | |
/** | |
* Created by REDAS on 16.12.2017. | |
*/ | |
public class Mario extends Sprite { | |
public enum State {FALLING, JUMPING, STANDING, RUNNING} | |
public State currentState; | |
public State previousState; | |
public World world; | |
public Body b2body; | |
private TextureRegion marioStand; | |
private Animation marioRun; | |
private Animation marioJump; | |
private float stateTimer; | |
private boolean runningRight; | |
public Mario(PlayScreen screen) { | |
super(screen.getAtlas().findRegion("little_mario")); | |
this.world = screen.getWorld(); | |
currentState = State.STANDING; | |
previousState = State.STANDING; | |
stateTimer = 0; | |
runningRight = true; | |
Array<TextureRegion> frames = new Array<TextureRegion>(); | |
for (int i = 1; i < 4; i++) | |
frames.add(new TextureRegion(getTexture(), i * 16, 11, 16, 16)); | |
marioRun = new Animation<TextureRegion>(0.1f, frames); | |
frames.clear(); | |
for (int i = 4; i < 6; i++) | |
frames.add(new TextureRegion(getTexture(), i * 16, 11, 16, 16)); | |
marioJump = new Animation<TextureRegion>(0.1f, frames); | |
marioStand = new TextureRegion(getTexture(), 1, 11, 16, 16); | |
defineMario(); | |
setBounds(0, 0, 16 / MarioBros.PPM, 16 / MarioBros.PPM); | |
setRegion(marioStand); | |
} | |
public void update(float dt) { | |
setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2); | |
setRegion(getFrame(dt)); | |
} | |
public TextureRegion getFrame(float dt) { | |
currentState = getState(); | |
TextureRegion region; | |
switch (currentState) { | |
case JUMPING: | |
region = (TextureRegion) marioJump.getKeyFrame(stateTimer); | |
break; | |
case RUNNING: | |
region = (TextureRegion) marioRun.getKeyFrame(stateTimer, true); | |
break; | |
case FALLING: | |
case STANDING: | |
default: | |
region = marioStand; | |
break; | |
} | |
if ((b2body.getLinearVelocity().x < 0 || !runningRight) && !region.isFlipX()) { | |
region.flip(true, false); | |
runningRight = false; | |
} else if ((b2body.getLinearVelocity().x > 0 || runningRight) && region.isFlipX()) { | |
region.flip(true, false); | |
runningRight = true; | |
} | |
stateTimer = currentState == previousState ? stateTimer + dt : 0; | |
previousState = currentState; | |
return region; | |
} | |
public State getState() { | |
if (b2body.getLinearVelocity().y > 0 || (b2body.getLinearVelocity().y < 0 && previousState == State.JUMPING)) | |
return State.JUMPING; | |
else if (b2body.getLinearVelocity().y < 0) | |
return State.FALLING; | |
else if (b2body.getLinearVelocity().x != 0) | |
return State.RUNNING; | |
else | |
return State.STANDING; | |
} | |
public void defineMario() { | |
BodyDef bdef = new BodyDef(); | |
bdef.position.set(32 / MarioBros.PPM, 32 / MarioBros.PPM); | |
bdef.type = BodyDef.BodyType.DynamicBody; | |
b2body = world.createBody(bdef); | |
FixtureDef fdef = new FixtureDef(); | |
CircleShape shape = new CircleShape(); | |
shape.setRadius(6 / MarioBros.PPM); | |
fdef.filter.categoryBits = MarioBros.MARIO_BIT; | |
fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT; | |
fdef.shape = shape; | |
b2body.createFixture(fdef); | |
EdgeShape head = new EdgeShape(); | |
head.set(new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); | |
fdef.shape = head; | |
fdef.isSensor = true; | |
b2body.createFixture(fdef).setUserData("head"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game; | |
import com.badlogic.gdx.Game; | |
import com.badlogic.gdx.assets.AssetManager; | |
import com.badlogic.gdx.audio.Music; | |
import com.badlogic.gdx.audio.Sound; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.redsoft.game.Screens.PlayScreen; | |
public class MarioBros extends Game { | |
public static final int V_WIDTH = 400; | |
public static final int V_HEIGHT = 208; | |
public static final float PPM = 100; | |
public static final short GROUND_BIT = 1; | |
public static final short MARIO_BIT = 2; | |
public static final short BRICK_BIT = 4; | |
public static final short COIN_BIT = 8; | |
public static final short DESTROYED_BIT = 16; | |
public static final short OBJECT_BIT = 32; | |
public static final short ENEMY_BIT = 64; | |
public static final short ENEMY_HEAD_BIT = 128; | |
public SpriteBatch batch; | |
public static AssetManager manager; | |
@Override | |
public void create() { | |
batch = new SpriteBatch(); | |
manager = new AssetManager(); | |
manager.load("audio/music/mario_music.ogg", Music.class); | |
manager.load("audio/sounds/coin.wav", Sound.class); | |
manager.load("audio/sounds/bump.wav", Sound.class); | |
manager.load("audio/sounds/breakblock.wav", Sound.class); | |
manager.finishLoading(); | |
setScreen(new PlayScreen(this)); | |
} | |
@Override | |
public void dispose() { | |
manager.dispose(); | |
batch.dispose(); | |
} | |
@Override | |
public void render() { | |
super.render(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Sprites.Items; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.BodyDef; | |
import com.badlogic.gdx.physics.box2d.CircleShape; | |
import com.badlogic.gdx.physics.box2d.FixtureDef; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Screens.PlayScreen; | |
/** | |
* Created by REDAS on 06.01.2018. | |
*/ | |
public class Mushroom extends Item { | |
public Mushroom(PlayScreen screen, float x, float y) { | |
super(screen, x, y); | |
setRegion(screen.getAtlas().findRegion("mushroom"), 0, 0, 16, 16); | |
velocity = new Vector2(0, 0); | |
} | |
@Override | |
public void defineItem() { | |
BodyDef bdef = new BodyDef(); | |
bdef.position.set(getX(), getY()); | |
bdef.type = BodyDef.BodyType.DynamicBody; | |
body = world.createBody(bdef); | |
FixtureDef fdef = new FixtureDef(); | |
CircleShape shape = new CircleShape(); | |
shape.setRadius(6 / MarioBros.PPM); | |
fdef.filter.categoryBits = MarioBros.ENEMY_BIT; | |
fdef.filter.maskBits = MarioBros.GROUND_BIT | | |
MarioBros.COIN_BIT | | |
MarioBros.BRICK_BIT | | |
MarioBros.ENEMY_BIT | | |
MarioBros.OBJECT_BIT | | |
MarioBros.MARIO_BIT; | |
fdef.shape = shape; | |
body.createFixture(fdef).setUserData(this); | |
} | |
@Override | |
public void use() { | |
destroy(); | |
} | |
@Override | |
public void update(float dt) { | |
super.update(dt); | |
if (!destroyed) { | |
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2); | |
velocity.y = body.getLinearVelocity().y; | |
body.setLinearVelocity(velocity); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Screens; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Input; | |
import com.badlogic.gdx.Screen; | |
import com.badlogic.gdx.audio.Music; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.g2d.TextureAtlas; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.maps.tiled.TmxMapLoader; | |
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; | |
import com.badlogic.gdx.physics.box2d.World; | |
import com.badlogic.gdx.utils.Array; | |
import com.badlogic.gdx.utils.viewport.FitViewport; | |
import com.badlogic.gdx.utils.viewport.Viewport; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Scenes.Hud; | |
import com.redsoft.game.Sprites.Enemies.Enemy; | |
import com.redsoft.game.Sprites.Items.Item; | |
import com.redsoft.game.Sprites.Items.ItemDef; | |
import com.redsoft.game.Sprites.Items.Mushroom; | |
import com.redsoft.game.Sprites.Mario; | |
import com.redsoft.game.Tools.B2WorldCreator; | |
import com.redsoft.game.Tools.WorldContactListener; | |
import java.util.PriorityQueue; | |
/** | |
* Created by REDAS on 15.12.2017. | |
*/ | |
public class PlayScreen implements Screen { | |
private MarioBros game; | |
private TextureAtlas atlas; | |
private OrthographicCamera gamecam; | |
private Viewport gamePort; | |
private Hud hud; | |
private TmxMapLoader mapLoader; | |
private TiledMap map; | |
private OrthogonalTiledMapRenderer renderer; | |
private World world; | |
private Box2DDebugRenderer b2dr; | |
private B2WorldCreator creator; | |
private Mario player; | |
private Music music; | |
private Array<Item> items; | |
private PriorityQueue<ItemDef> itemsToSpawn; | |
public PlayScreen(MarioBros game) { | |
atlas = new TextureAtlas("Mario_and_Enemies.pack"); | |
this.game = game; | |
gamecam = new OrthographicCamera(); | |
gamePort = new FitViewport(MarioBros.V_WIDTH / MarioBros.PPM, MarioBros.V_HEIGHT / MarioBros.PPM, gamecam); | |
hud = new Hud(game.batch); | |
mapLoader = new TmxMapLoader(); | |
map = mapLoader.load("level1.tmx"); | |
renderer = new OrthogonalTiledMapRenderer(map, 1 / MarioBros.PPM); | |
gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0); | |
world = new World(new Vector2(0, -10), true); | |
b2dr = new Box2DDebugRenderer(); | |
creator = new B2WorldCreator(this); | |
player = new Mario(this); | |
world.setContactListener(new WorldContactListener()); | |
music = MarioBros.manager.get("audio/music/mario_music.ogg", Music.class); | |
music.setLooping(true); | |
music.play(); | |
items = new Array<Item>(); | |
itemsToSpawn = new PriorityQueue<ItemDef>(); | |
} | |
public void spawnItem(ItemDef idef) { | |
itemsToSpawn.add(idef); | |
} | |
public void handleSpawningItems() { | |
if (!itemsToSpawn.isEmpty()) { | |
ItemDef idef = itemsToSpawn.poll(); | |
if (idef.type == Mushroom.class) { | |
items.add(new Mushroom(this, idef.position.x, idef.position.y)); | |
} | |
} | |
} | |
public TextureAtlas getAtlas() { | |
return atlas; | |
} | |
@Override | |
public void show() { | |
} | |
public void handleInput(float dt) { | |
if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) | |
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true); | |
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2) | |
player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true); | |
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2) | |
player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true); | |
} | |
public void update(float dt) { | |
//handle user input first | |
handleInput(dt); | |
handleSpawningItems(); | |
//takes 1 step in the physics simulation(60 times per second) | |
world.step(1 / 60f, 6, 2); | |
player.update(dt); | |
for (Enemy enemy : creator.getGoombas()) { | |
enemy.update(dt); | |
if (!enemy.isDestroyed() && enemy.getX() < player.getX() + 224 / MarioBros.PPM) { | |
enemy.b2body.setActive(true); | |
} | |
} | |
for (Item item : items) | |
item.update(dt); | |
hud.update(dt); | |
//attach our gamecam to our players.x coordinate | |
gamecam.position.x = player.b2body.getPosition().x; | |
//update our gamecam with correct coordinates after changes | |
gamecam.update(); | |
//tell our renderer to draw only what our camera can see in our game world. | |
renderer.setView(gamecam); | |
} | |
@Override | |
public void render(float delta) { | |
update(delta); | |
Gdx.gl.glClearColor(0, 0, 0, 1); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
renderer.render(); | |
b2dr.render(world, gamecam.combined); | |
game.batch.setProjectionMatrix(gamecam.combined); | |
game.batch.begin(); | |
player.draw(game.batch); | |
for (Enemy enemy : creator.getGoombas()) | |
enemy.draw(game.batch); | |
for (Item item : items) | |
item.draw(game.batch); | |
game.batch.end(); | |
game.batch.setProjectionMatrix(hud.stage.getCamera().combined); | |
hud.stage.draw(); | |
} | |
@Override | |
public void resize(int width, int height) { | |
gamePort.update(width, height); | |
} | |
public TiledMap getMap() { | |
return map; | |
} | |
public World getWorld() { | |
return world; | |
} | |
@Override | |
public void pause() { | |
} | |
@Override | |
public void resume() { | |
} | |
@Override | |
public void hide() { | |
} | |
@Override | |
public void dispose() { | |
map.dispose(); | |
renderer.dispose(); | |
world.dispose(); | |
b2dr.dispose(); | |
hud.dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redsoft.game.Tools; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.physics.box2d.Contact; | |
import com.badlogic.gdx.physics.box2d.ContactImpulse; | |
import com.badlogic.gdx.physics.box2d.ContactListener; | |
import com.badlogic.gdx.physics.box2d.Fixture; | |
import com.badlogic.gdx.physics.box2d.Manifold; | |
import com.redsoft.game.MarioBros; | |
import com.redsoft.game.Sprites.Enemies.Enemy; | |
import com.redsoft.game.Sprites.TileObjects.InteractiveTileObject; | |
/** | |
* Created by REDAS on 17.12.2017. | |
*/ | |
public class WorldContactListener implements ContactListener { | |
@Override | |
public void beginContact(Contact contact) { | |
Fixture fixA = contact.getFixtureA(); | |
Fixture fixB = contact.getFixtureB(); | |
int cDef = fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits; | |
if (fixA.getUserData() == "head" || fixB.getUserData() == "head") { | |
Fixture head = fixA.getUserData() == "head" ? fixA : fixB; | |
Fixture object = head == fixA ? fixB : fixA; | |
if (object.getUserData() != null && InteractiveTileObject.class.isAssignableFrom(object.getUserData().getClass())) { | |
((InteractiveTileObject) object.getUserData()).onHeadHit(); | |
} | |
} | |
switch (cDef) { | |
case MarioBros.ENEMY_HEAD_BIT | MarioBros.MARIO_BIT: | |
if (fixA.getFilterData().categoryBits == MarioBros.ENEMY_HEAD_BIT) | |
((Enemy) fixA.getUserData()).hitOnHead(); | |
else | |
((Enemy) fixB.getUserData()).hitOnHead(); | |
break; | |
case MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT: | |
if (fixA.getFilterData().categoryBits == MarioBros.ENEMY_BIT) | |
((Enemy) fixA.getUserData()).reverseVelocity(true, false); | |
else | |
((Enemy) fixB.getUserData()).reverseVelocity(true, false); | |
break; | |
case MarioBros.MARIO_BIT | MarioBros.ENEMY_BIT: | |
Gdx.app.log("MARIO", "DIED"); | |
break; | |
case MarioBros.ENEMY_BIT | MarioBros.ENEMY_BIT: | |
((Enemy) fixA.getUserData()).reverseVelocity(true, false); | |
((Enemy) fixB.getUserData()).reverseVelocity(true, false); | |
break; | |
} | |
} | |
@Override | |
public void endContact(Contact contact) { | |
} | |
@Override | |
public void preSolve(Contact contact, Manifold oldManifold) { | |
} | |
@Override | |
public void postSolve(Contact contact, ContactImpulse impulse) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment