-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Mario-esque camera. It is a bit hacky, since it only takes into
account the size of the camera box, instead of the camera box's location. In any case, it's a start. Refs #1.
- Loading branch information
Showing
7 changed files
with
205 additions
and
41 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ca.kess.games.camera; | ||
|
||
import ca.kess.games.Constants; | ||
import ca.kess.games.entities.GameEntity; | ||
import ca.kess.games.world.WorldLevel; | ||
|
||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.graphics.OrthographicCamera; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
import com.badlogic.gdx.math.Matrix4; | ||
import com.badlogic.gdx.math.Vector2; | ||
|
||
public class FixedCamera extends GameCamera { | ||
private GameEntity mGameEntity; | ||
|
||
public FixedCamera(OrthographicCamera camera, GameEntity entity) { | ||
super(camera); | ||
mGameEntity = entity; | ||
} | ||
|
||
public final GameEntity getTarget() { | ||
return mGameEntity; | ||
} | ||
|
||
@Override | ||
public void update() { | ||
Vector2 pos = mGameEntity.getPosition(); | ||
getCamera().position.set(pos.x, pos.y, 0); | ||
getCamera().update(); | ||
|
||
} | ||
|
||
@Override | ||
public void render(SpriteBatch b, WorldLevel worldLevel) { | ||
float mx = getCamera().position.x; | ||
float my = getCamera().position.y; | ||
float width = Gdx.graphics.getWidth()/(8*Constants.ZOOM_FACTOR); | ||
float height = Gdx.graphics.getWidth()/(8*Constants.ZOOM_FACTOR); | ||
int x0 = Math.max((int) (mx - width/2), 0); | ||
int x1 = Math.min((int) (1+mx + width/2), worldLevel.getWidth()); | ||
int y0 = Math.max((int) (my - height/2), 0); | ||
int y1 = Math.min((int) (my + height/2), worldLevel.getHeight()); | ||
|
||
worldLevel.render(b, x0, y0, x1, y1); | ||
} | ||
|
||
@Override | ||
public void onResize(int width, int height) { | ||
getCamera().setToOrtho(false, width/(Constants.TILE_SIZE*Constants.ZOOM_FACTOR), height/(Constants.TILE_SIZE*Constants.ZOOM_FACTOR)); | ||
} | ||
|
||
@Override | ||
public Matrix4 getCombined() { | ||
return getCamera().combined; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ca.kess.games.camera; | ||
|
||
import ca.kess.games.world.WorldLevel; | ||
|
||
import com.badlogic.gdx.graphics.OrthographicCamera; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
import com.badlogic.gdx.math.Matrix4; | ||
|
||
public abstract class GameCamera { | ||
private final OrthographicCamera mCamera; | ||
|
||
protected final OrthographicCamera getCamera() { | ||
return mCamera; | ||
} | ||
|
||
public GameCamera(OrthographicCamera camera) { | ||
mCamera = camera; | ||
} | ||
|
||
/** | ||
* Do any pre-processing logic for the camera | ||
*/ | ||
public abstract void update(); | ||
|
||
/** | ||
* Get the MVP matrix for the camera | ||
*/ | ||
public abstract Matrix4 getCombined(); | ||
|
||
/** | ||
* Called when the screen is resized/reoriented. | ||
*/ | ||
public abstract void onResize(int width, int height); | ||
|
||
/** | ||
* Render pandas! | ||
*/ | ||
public abstract void render(SpriteBatch b, WorldLevel worldLevel); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,82 @@ | ||
package ca.kess.games.camera; | ||
|
||
public class MarioCamera { | ||
import ca.kess.games.Constants; | ||
import ca.kess.games.entities.GameEntity; | ||
|
||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.graphics.OrthographicCamera; | ||
import com.badlogic.gdx.math.Rectangle; | ||
import com.badlogic.gdx.math.Vector2; | ||
import com.badlogic.gdx.math.Vector3; | ||
|
||
public class MarioCamera extends FixedCamera { | ||
/** | ||
* The area of the camera, in screen space | ||
*/ | ||
private final Rectangle mArea; | ||
public MarioCamera(OrthographicCamera camera, GameEntity entity, Rectangle area) { | ||
super(camera, entity); | ||
mArea = area; | ||
} | ||
|
||
@Override | ||
public void update() { | ||
Vector3 cameraPos = getCamera().position; | ||
Vector2 entityPos = getTarget().getPosition(); | ||
|
||
float f = Constants.ZOOM_FACTOR * Constants.TILE_SIZE; | ||
//Transform entity position to screen space. | ||
float cameraX = cameraPos.x; //in world space (1 = 1 tile) | ||
float cameraY = cameraPos.y; | ||
|
||
//Convert to screen space. | ||
float offsetX = (entityPos.x - cameraPos.x)*f; | ||
float offsetY = (entityPos.y - cameraPos.y)*f; | ||
|
||
|
||
|
||
//Camera is always in the middle of the screen | ||
float mx = Gdx.graphics.getWidth()/2; | ||
float my = Gdx.graphics.getHeight()/2; | ||
|
||
Gdx.app.log(Constants.LOG, offsetX + " " + offsetY + " " + mx + " " + my); | ||
|
||
if(offsetX > mArea.width/2) { | ||
cameraX += (offsetX - (mArea.width/2))/f; | ||
} else if (offsetX < -mArea.width/2) { | ||
cameraX += (offsetX + (mArea.width/2))/f; | ||
} | ||
|
||
if(offsetY > mArea.height/2) { | ||
cameraY += (offsetY - (mArea.height/2))/f; | ||
} else if (offsetY < -mArea.height/2) { | ||
cameraY += (offsetY + (mArea.height/2))/f; | ||
} | ||
|
||
//only move the camera if the entity is outside of the area. | ||
/* | ||
if(targetY < mArea.y) { | ||
targetY = mArea.y; | ||
} else if(targetY > mArea.y + mArea.height) { | ||
targetY = mArea.y + mArea.height; | ||
} | ||
if(targetX < mArea.x) { | ||
targetX = mArea.x; | ||
} else if(targetX > mArea.x + mArea.width) { | ||
targetX = mArea.x + mArea.width; | ||
}*/ | ||
|
||
|
||
getCamera().position.set(cameraX, cameraY, 0); | ||
//getCamera().position.set(offsetX, offsetY, 0); | ||
//getCamera().position.set(entityPos.x, entityPos.y, 0); | ||
getCamera().update(); | ||
} | ||
|
||
@Override | ||
public void onResize(int width, int height) { | ||
super.onResize(width, height); | ||
} | ||
|
||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A simple 2D platformer! |
Really we should bake these into some texture instead, and just pick which larger textures to render. I think that would be a lot faster.