-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6662cf0
commit e617dca
Showing
153 changed files
with
4,254 additions
and
2,444 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.nativegame.nattyengine"> | ||
<manifest package="com.nativegame.nattyengine"> | ||
|
||
</manifest> |
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
238 changes: 238 additions & 0 deletions
238
nattyengine/src/main/java/com/nativegame/nattyengine/camera/BaseCamera.java
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,238 @@ | ||
package com.nativegame.nattyengine.camera; | ||
|
||
import com.nativegame.nattyengine.util.exception.EngineRuntimeException; | ||
import com.nativegame.nattyengine.util.math.ResolutionUtils; | ||
|
||
/** | ||
* Created by Oscar Liang on 2022/12/11 | ||
*/ | ||
|
||
public abstract class BaseCamera implements Camera { | ||
|
||
private final int mCameraWidth; | ||
private final int mCameraHeight; | ||
private final int mWorldWidth; | ||
private final int mWorldHeight; | ||
private final int mProjectWorldWidth; | ||
private final int mProjectWorldHeight; | ||
private final float mPixelFactor; | ||
|
||
private float mX; | ||
private float mY; | ||
private float mZoom = 1.0f; | ||
|
||
//-------------------------------------------------------- | ||
// Constructors | ||
//-------------------------------------------------------- | ||
protected BaseCamera(int cameraWidth, int cameraHeight) { | ||
this(cameraWidth, cameraHeight, cameraWidth, cameraHeight); | ||
} | ||
|
||
protected BaseCamera(int cameraWidth, int cameraHeight, | ||
int worldWidth, int worldHeight) { | ||
this(cameraWidth, cameraHeight, worldWidth, worldHeight, | ||
ResolutionUtils.getResolutionWidth(cameraWidth, cameraHeight, worldWidth, worldHeight), | ||
ResolutionUtils.getResolutionHeight(cameraWidth, cameraHeight, worldWidth, worldHeight)); | ||
} | ||
|
||
protected BaseCamera(int cameraWidth, int cameraHeight, | ||
int worldWidth, int worldHeight, | ||
int projectWorldWidth, int projectWorldHeight) { | ||
mPixelFactor = projectWorldWidth * 1f / worldWidth; | ||
mCameraWidth = (int) (cameraWidth / mPixelFactor); | ||
mCameraHeight = (int) (cameraHeight / mPixelFactor); | ||
mWorldWidth = worldWidth; | ||
mWorldHeight = worldHeight; | ||
mProjectWorldWidth = projectWorldWidth; | ||
mProjectWorldHeight = projectWorldHeight; | ||
} | ||
//======================================================== | ||
|
||
//-------------------------------------------------------- | ||
// Overriding methods | ||
//-------------------------------------------------------- | ||
@Override | ||
public int getCameraWidth() { | ||
return (int) (mCameraWidth / mZoom); | ||
} | ||
|
||
@Override | ||
public int getCameraHeight() { | ||
return (int) (mCameraHeight / mZoom); | ||
} | ||
|
||
@Override | ||
public int getWorldWidth() { | ||
return mWorldWidth; | ||
} | ||
|
||
@Override | ||
public int getWorldHeight() { | ||
return mWorldHeight; | ||
} | ||
|
||
@Override | ||
public float getPixelFactor() { | ||
return mPixelFactor; | ||
} | ||
|
||
@Override | ||
public float getX() { | ||
return getCenterX() - (mCameraWidth / 2f) / mZoom; | ||
} | ||
|
||
@Override | ||
public void setX(float x) { | ||
mX = x; | ||
if (mX < 0) { | ||
mX = 0; | ||
} | ||
if (mX > mWorldWidth - mCameraWidth) { | ||
mX = mWorldWidth - mCameraWidth; | ||
} | ||
} | ||
|
||
@Override | ||
public float getY() { | ||
return getCenterY() - (mCameraHeight / 2f) / mZoom; | ||
} | ||
|
||
@Override | ||
public void setY(float y) { | ||
mY = y; | ||
if (mY < 0) { | ||
mY = 0; | ||
} | ||
if (mY > mWorldHeight - mCameraHeight) { | ||
mY = mWorldHeight - mCameraHeight; | ||
} | ||
} | ||
|
||
@Override | ||
public float getCenterX() { | ||
return mX + mCameraWidth / 2f; | ||
} | ||
|
||
@Override | ||
public void setCenterX(float centerX) { | ||
setX(centerX - mCameraWidth / 2f); | ||
} | ||
|
||
@Override | ||
public float getCenterY() { | ||
return mY + mCameraHeight / 2f; | ||
} | ||
|
||
@Override | ||
public void setCenterY(float centerY) { | ||
setY(centerY - mCameraHeight / 2f); | ||
} | ||
|
||
@Override | ||
public float getZoom() { | ||
return mZoom; | ||
} | ||
|
||
@Override | ||
public void setZoom(float zoom) { | ||
mZoom = zoom; | ||
} | ||
|
||
@Override | ||
public float getWorldToScreenZoom(CoordinateType type) { | ||
switch (type) { | ||
case WORLD: | ||
return mZoom; | ||
case CAMERA: | ||
return 1; | ||
default: | ||
throw new EngineRuntimeException("CoordinateType not found!"); | ||
} | ||
} | ||
|
||
@Override | ||
public float getWorldToScreenX(float worldX, CoordinateType type) { | ||
switch (type) { | ||
case WORLD: | ||
return getProjectX(worldX) - getScreenMarginX(); | ||
case CAMERA: | ||
return getScreenWidth() / 2f - (mCameraWidth / 2f - worldX) * mPixelFactor; | ||
default: | ||
throw new EngineRuntimeException("CoordinateType not found!"); | ||
} | ||
} | ||
|
||
@Override | ||
public float getWorldToScreenY(float worldY, CoordinateType type) { | ||
switch (type) { | ||
case WORLD: | ||
return getProjectY(worldY) - getScreenMarginY(); | ||
case CAMERA: | ||
return getScreenHeight() / 2f - (mCameraHeight / 2f - worldY) * mPixelFactor; | ||
default: | ||
throw new EngineRuntimeException("CoordinateType not found!"); | ||
} | ||
} | ||
|
||
@Override | ||
public float getScreenToWorldX(float screenX, CoordinateType type) { | ||
switch (type) { | ||
case WORLD: | ||
return getWorldX(screenX + getScreenMarginX()); | ||
case CAMERA: | ||
return mCameraWidth / 2f - (getScreenWidth() / 2f - screenX) / mPixelFactor; | ||
default: | ||
throw new EngineRuntimeException("CoordinateType not found!"); | ||
} | ||
} | ||
|
||
@Override | ||
public float getScreenToWorldY(float screenY, CoordinateType type) { | ||
switch (type) { | ||
case WORLD: | ||
return getWorldY(screenY + getScreenMarginY()); | ||
case CAMERA: | ||
return mCameraHeight / 2f - (getScreenHeight() / 2f - screenY) / mPixelFactor; | ||
default: | ||
throw new EngineRuntimeException("CoordinateType not found!"); | ||
} | ||
} | ||
//======================================================== | ||
|
||
//-------------------------------------------------------- | ||
// Methods | ||
//-------------------------------------------------------- | ||
private float getProjectX(float worldX) { | ||
return mProjectWorldWidth / 2f - (mWorldWidth / 2f - worldX) * mPixelFactor * mZoom; | ||
} | ||
|
||
private float getProjectY(float worldY) { | ||
return mProjectWorldHeight / 2f - (mWorldHeight / 2f - worldY) * mPixelFactor * mZoom; | ||
} | ||
|
||
private float getWorldX(float projectX) { | ||
return mWorldWidth / 2f - (mProjectWorldWidth / 2f - projectX) / mPixelFactor * mZoom; | ||
} | ||
|
||
private float getWorldY(float projectY) { | ||
return mWorldHeight / 2f - (mProjectWorldHeight / 2f - projectY) / mPixelFactor * mZoom; | ||
} | ||
|
||
private float getScreenWidth() { | ||
return mCameraWidth * mPixelFactor; | ||
} | ||
|
||
private float getScreenHeight() { | ||
return mCameraHeight * mPixelFactor; | ||
} | ||
|
||
private float getScreenMarginX() { | ||
return mX * mPixelFactor; | ||
} | ||
|
||
private float getScreenMarginY() { | ||
return mY * mPixelFactor; | ||
} | ||
//======================================================== | ||
|
||
} |
Oops, something went wrong.