RepeatingBackground and added Entities

This commit is contained in:
Mike Schwörer 2015-09-14 16:40:30 +02:00
parent 9dc0b6c617
commit 34fe132efa
8 changed files with 165 additions and 9 deletions

BIN
android/assets/cannon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 801 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 449 B

View File

@ -1,6 +1,7 @@
package de.samdev.cannonshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import de.samdev.absgdx.framework.AgdxGame;
@ -12,14 +13,22 @@ public class CannonGame extends AgdxGame {
public void onCreate() {
Textures.init();
pushLayer(new StandardLevel(this));
setLayer(new StandardLevel(this));
setDebugFont(new BitmapFont(Gdx.files.internal("consolefont.fnt")));
settings.debugVisualMenu.set(false);
settings.debugMenuLayerTextInfos.set(false);
settings.debugEnabled.set(true);
}
@Override
public void onUpdate(float arg0) {
// TODO Auto-generated method stub
public void onUpdate(float delta) {
if (Gdx.input.isKeyJustPressed(Keys.F1)) settings.debugEnabled.doSwitch();
if (Gdx.input.isKeyJustPressed(Keys.F2)) settings.debugVisualEntities.doSwitch();
if (Gdx.input.isKeyJustPressed(Keys.F3)) settings.debugVisualMap.doSwitch();
if (Gdx.input.isKeyJustPressed(Keys.F4)) settings.debugVisualMenu.doSwitch();
if (Gdx.input.isKeyJustPressed(Keys.F5)) settings.debugTextInfos.doSwitch();
if (Gdx.input.isKeyJustPressed(Keys.F6)) settings.debugEntitiesPhysicVectors.doSwitch();
}
}

View File

@ -4,8 +4,10 @@ import com.badlogic.gdx.graphics.Texture;
public final class Textures {
public static Texture texbackground;
public static Texture cannon;
public static void init() {
texbackground = new Texture("level_background.png");
cannon = new Texture("cannon.png");
}
}

View File

@ -0,0 +1,63 @@
package de.samdev.cannonshooter.entities;
import de.samdev.absgdx.framework.entities.Entity;
import de.samdev.absgdx.framework.entities.colliosiondetection.CollisionGeometryOwner;
import de.samdev.absgdx.framework.entities.colliosiondetection.geometries.CollisionGeometry;
import de.samdev.absgdx.framework.layer.GameLayer;
import de.samdev.cannonshooter.Textures;
public class Cannon extends Entity {
public Cannon(float x, float y) {
super(Textures.cannon, 1, 1);
setPosition(x, y);
}
@Override
public void onActiveCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
// TODO Auto-generated method stub
}
@Override
public void onPassiveCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
// TODO Auto-generated method stub
}
@Override
public void onActiveMovementCollide(CollisionGeometryOwner passiveCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
// TODO Auto-generated method stub
}
@Override
public void onPassiveMovementCollide(CollisionGeometryOwner activeCollider, CollisionGeometry myGeo, CollisionGeometry otherGeo) {
// TODO Auto-generated method stub
}
@Override
public boolean canCollideWith(CollisionGeometryOwner other) {
return false;
}
@Override
public boolean canMoveCollideWith(CollisionGeometryOwner other) {
return false;
}
@Override
public void beforeUpdate(float delta) {
// TODO Auto-generated method stub
}
@Override
public void onLayerAdd(GameLayer layer) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,61 @@
package de.samdev.cannonshooter.framework;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import de.samdev.absgdx.framework.map.TileMap;
import de.samdev.absgdx.framework.map.background.MapBackground;
/**
* This is a repeating background.
*
* The texture will be repeated from the map-origin (0|0) to cover the whole map
* The texture tiles are aligned to the map tiles, with scale=1 the texture-tiles equal the map-tiles
* But the texture-tiles go on after the map borders and fill the whole viewport
*/
public class TileAlignedBackground extends MapBackground { //TODO Merge back to absGDX
private final TextureRegion texture;
private final float scale;
/**
* Create a new repeating Background aligned to the TiledMap
*
* @param tex the background texture
* @param scale size of a background tile (1 = Background equals MapTile) (2 = Background equals 4 MapTile) ...
*/
public TileAlignedBackground(TextureRegion tex, float scale) {
this.texture = tex;
this.scale = scale;
}
/**
* Create a new repeating Background aligned to the TiledMap
*
* @param tex the background texture
* @param scale size of a background tile (1 = Background equals MapTile) (2 = Background equals 4 MapTile) ...
*/
public TileAlignedBackground(Texture tex, float scale) {
this(new TextureRegion(tex), scale);
}
@Override
public void draw(SpriteBatch sbatch, Vector2 map_offset, TileMap map, Rectangle visible) {
int minX = (int)(visible.x + map_offset.x);
int minY = (int)(visible.y + map_offset.y);
int maxX = (int)(visible.x + visible.width - map_offset.x + 1);
int maxY = (int)(visible.y + visible.height - map_offset.y + 1);
for (int x = minX; x < maxX; x+=scale) {
for (int y = minY; y < maxY; y+=scale) {
sbatch.draw(texture, x , y, scale, scale);
}
}
}
}

View File

@ -1,23 +1,44 @@
package de.samdev.cannonshooter.level;
import java.util.Random;
import com.badlogic.gdx.math.Vector2;
import de.samdev.absgdx.framework.AgdxGame;
import de.samdev.absgdx.framework.layer.GameLayer;
import de.samdev.absgdx.framework.map.TileMap;
import de.samdev.absgdx.framework.map.background.RepeatingBackground;
import de.samdev.absgdx.framework.map.mapscaleresolver.ShowCompleteMapScaleResolver;
import de.samdev.cannonshooter.Textures;
import de.samdev.cannonshooter.entities.Cannon;
import de.samdev.cannonshooter.framework.TileAlignedBackground;
public class StandardLevel extends GameLayer {
public StandardLevel(AgdxGame owner) {
super(owner, TileMap.createEmptyMap(32, 20));
super(owner, TileMap.createEmptyMap(32, 32));
addBackground(new RepeatingBackground(Textures.texbackground, 32));
addBackground(new TileAlignedBackground(Textures.texbackground, 1));
setMapScaleResolver(new ShowCompleteMapScaleResolver());
setRawOffset(new Vector2(-(getVisibleMapBox().width - getMap().width)/2, -(getVisibleMapBox().height - getMap().height)/2));
Random R = new Random(80085);
for (int i = 0; i < 24; i++) {
addEntity(new Cannon(R.nextInt(15)*2+1, R.nextInt(15)*2+1));
}
}
@Override
public void onResize() {
super.onResize();
setRawOffset(new Vector2(-(getVisibleMapBox().width - getMap().width)/2, -(getVisibleMapBox().height - getMap().height)/2));
}
@Override
public void onUpdate(float arg0) {
// TODO Auto-generated method stub
//setRawOffset(new Vector2(-(getVisibleMapBox().width - getMap().width)/2, -(getVisibleMapBox().height - getMap().height)/2));
}
}

Binary file not shown.