Created
November 16, 2015 13:40
-
-
Save picopicolab/db4cc2f6438e99e7034c 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.github.picopicolab.games.demo; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.FPSLogger; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | |
import com.badlogic.gdx.maps.MapObject; | |
import com.badlogic.gdx.maps.objects.RectangleMapObject; | |
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.Circle; | |
import com.badlogic.gdx.math.Intersector; | |
import com.badlogic.gdx.math.Rectangle; | |
import com.badlogic.gdx.utils.Array; | |
/** | |
* タイルマップ衝突判定サンプル実装 | |
*/ | |
public class DemoGame extends ApplicationAdapter { | |
private OrthographicCamera camera; | |
private ShapeRenderer renderer; | |
private TiledMap tiledMap; | |
private OrthogonalTiledMapRenderer tiledMapRenderer; | |
private Array<Rectangle> objects; | |
private Rectangle rectangle; | |
private Circle circle; | |
private Direction circleDirection; | |
private Direction rectangleDirection; | |
private float speed; | |
private FPSLogger logger; | |
@Override | |
public void create() { | |
camera = new OrthographicCamera(); | |
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // カメラを画面サイズに合わせる | |
renderer = new ShapeRenderer(); | |
renderer.setAutoShapeType(true); | |
tiledMap = new TmxMapLoader().load("tilemap.tmx"); // マップファイル読込 | |
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap); | |
objects = new Array<Rectangle>(); // タイルマップ配列オブジェクト生成 | |
for (MapObject mapObject : tiledMap.getLayers().get("objects").getObjects()) { | |
if (mapObject instanceof RectangleMapObject) { | |
// RectangleMapObject クラスのインスタンスの場合 | |
objects.add(((RectangleMapObject)mapObject).getRectangle()); | |
} | |
} | |
rectangle = new Rectangle(20.0f, Gdx.graphics.getHeight() - 15.0f, 15.0f, 15.0f); | |
rectangleDirection = Direction.Down; // 進行方向を下に設定 | |
circle = new Circle(Gdx.graphics.getWidth() - 30.0f, 7.5f, 7.5f); | |
circleDirection = Direction.Up; // 進行方向を上に設定 | |
speed = 50.0f; // 図形移動速度 | |
logger = new FPSLogger(); | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
camera.update(); // カメラを更新 | |
// 図形移動 | |
float deltaTime = Gdx.graphics.getDeltaTime(); | |
if (Direction.Down == rectangleDirection) { | |
// 進行方向が下の場合 | |
rectangle.y -= speed * deltaTime; | |
if (rectangle.y <= 0.0f) { | |
// 画面下部まで来たら反転 | |
rectangle.y = 0.0f; | |
rectangleDirection = Direction.Up; | |
} | |
} else { | |
// 進行方向が上の場合 | |
rectangle.y += speed * deltaTime; | |
if (rectangle.y >= Gdx.graphics.getHeight() - rectangle.height) { | |
// 画面下部まで来たら反転 | |
rectangle.y = Gdx.graphics.getHeight() - rectangle.height; | |
rectangleDirection = Direction.Down; | |
} | |
} | |
if (Direction.Down == circleDirection) { | |
// 進行方向が下の場合 | |
circle.y -= speed * deltaTime; | |
if (circle.y <= circle.radius) { | |
// 画面下部まで来たら反転 | |
circle.y = circle.radius; | |
circleDirection = Direction.Up; | |
} | |
} else { | |
// 進行方向が上の場合 | |
circle.y += speed * deltaTime; | |
if (circle.y >= Gdx.graphics.getHeight() - circle.radius) { | |
// 画面下部まで来たら反転 | |
circle.y = Gdx.graphics.getHeight() - circle.radius; | |
circleDirection = Direction.Down; | |
} | |
} | |
tiledMapRenderer.setView(camera); // カメラを設定 | |
tiledMapRenderer.render(); | |
// 画像描画 | |
renderer.begin(); | |
Color rectangleColor = Color.BLUE; // 四角形の色に青を設定 | |
Color circleColor = Color.YELLOW; // 円の色に黄色を設定 | |
renderer.set(ShapeRenderer.ShapeType.Line); // 線にて描画 | |
renderer.setColor(Color.RED); // 色を赤に指定 | |
for(Rectangle object : objects) { | |
renderer.rect(object.x, object.y, object.width, object.height); | |
if (Intersector.overlaps(rectangle, object)) { | |
// マップオブジェクトと四角形が衝突している場合 | |
rectangleColor = Color.PURPLE; // 四角形の色に紫色を設定 | |
} | |
if (Intersector.overlaps(circle, object)) { | |
// マップオブジェクトと円が衝突している場合 | |
circleColor = Color.GREEN; // 円の色に緑色を設定 | |
} | |
} | |
renderer.setColor(rectangleColor); | |
renderer.set(ShapeRenderer.ShapeType.Filled); // 塗りつぶしにて描画 | |
renderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); | |
renderer.setColor(circleColor); | |
renderer.set(ShapeRenderer.ShapeType.Filled); // 塗りつぶしにて描画 | |
renderer.circle(circle.x, circle.y, circle.radius); | |
renderer.end(); | |
// FPS 出力 | |
logger.log(); | |
} | |
public enum Direction { | |
Up, Down; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment