Skip to content

Instantly share code, notes, and snippets.

@rymate1234
Created January 7, 2014 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rymate1234/8302172 to your computer and use it in GitHub Desktop.
Save rymate1234/8302172 to your computer and use it in GitHub Desktop.
package net.rymate.VirtuaCity;
import java.util.Random;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.SpriteCache;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Plane;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.Ray;
import com.badlogic.gdx.utils.TimeUtils;
public class VirtuaCityMain implements ApplicationListener, InputProcessor {
Texture texture;
OrthographicCamera cam;
SpriteBatch batch;
final Sprite[][] sprites = new Sprite[100][100];
final Matrix4 matrix = new Matrix4();
private boolean dragging = true;
@Override
public void create() {
texture = new Texture(Gdx.files.internal("data/grass.png"));
cam = new OrthographicCamera(10,
10 * (Gdx.graphics.getHeight() / (float) Gdx.graphics
.getWidth()));
cam.position.set(5, 5, 10);
cam.direction.set(-1, -1, -1);
cam.zoom += 1f;
matrix.setToRotation(new Vector3(1, 0, 0), 90);
for (int z = 0; z < 100; z++) {
for (int x = 0; x < 100; x++) {
sprites[x][z] = new Sprite(texture);
sprites[x][z].setPosition(x, z);
sprites[x][z].setSize(1, 1);
}
}
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
}
@Override
public void dispose() {
texture.dispose();
batch.dispose();
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.setTransformMatrix(matrix);
batch.begin();
for (int z = 0; z < 100; z++) {
for (int x = 0; x < 100; x++) {
sprites[x][z].draw(batch);
}
}
batch.end();
if (road)
checkTileTouched();
}
final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);
final Vector3 intersection = new Vector3();
Sprite lastSelectedTile = null;
private boolean mouseDown;
private boolean road;
private void checkTileTouched() {
if (Gdx.input.justTouched()) {
Ray pickRay = cam.getPickRay(Gdx.input.getX(), Gdx.input.getY());
Intersector.intersectRayPlane(pickRay, xzPlane, intersection);
int x = (int) intersection.x;
int z = (int) intersection.z;
if (x >= 0 && x < 100 && z >= 0 && z < 100) {
if (lastSelectedTile != null)
lastSelectedTile.setColor(1, 1, 1, 1);
Sprite sprite = sprites[x][z];
sprite.setColor(1, 0, 0, 1);
sprite.setTexture(new Texture(Gdx.files
.internal("data/road.png")));
lastSelectedTile = sprite;
}
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public boolean keyDown(int keycode) {
float rotationSpeed = 1f;
if(keycode == Input.Keys.LEFT) {
cam.rotate(-rotationSpeed , 0, 1, 0);
}
if(keycode == Input.Keys.RIGHT) {
cam.rotate(rotationSpeed, 0, 1, 0);
}
return false;
}
@Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyTyped(char character) {
if (character == 'm') {
road = false;
dragging = true;
}
if (character == 'r') {
dragging = false;
road = true;
}
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
final Vector3 curr = new Vector3();
final Vector3 last = new Vector3(-1, -1, -1);
final Vector3 delta = new Vector3();
@Override
public boolean touchDragged(int x, int y, int pointer) {
if (dragging ) {
Ray pickRay = cam.getPickRay(x, y);
Intersector.intersectRayPlane(pickRay, xzPlane, curr);
if (!(last.x == -1 && last.y == -1 && last.z == -1)) {
pickRay = cam.getPickRay(last.x, last.y);
Intersector.intersectRayPlane(pickRay, xzPlane, delta);
delta.sub(curr);
cam.position.add(delta.x, delta.y, delta.z);
}
last.set(x, y, 0);
}
return false;
}
@Override
public boolean touchUp(int x, int y, int pointer, int button) {
if (dragging) {
last.set(-1, -1, -1);
}
return false;
}
@Override
public boolean mouseMoved(int x, int y) {
return false;
}
@Override
public boolean scrolled(int amount) {
if(amount > 0) {
cam.zoom += 0.1;
}
if(amount < 0) {
cam.zoom -= 0.1;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment