Skip to content

Commit

Permalink
Release Natty Engine 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
iamoscarliang committed Nov 20, 2023
1 parent 6662cf0 commit e617dca
Show file tree
Hide file tree
Showing 153 changed files with 4,254 additions and 2,444 deletions.
17 changes: 17 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nattyengine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'org.jbox2d:jbox2d-library:2.2.1.1'
}
3 changes: 1 addition & 2 deletions nattyengine/src/main/AndroidManifest.xml
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>
47 changes: 16 additions & 31 deletions nattyengine/src/main/java/com/nativegame/nattyengine/Game.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.nativegame.nattyengine;

import com.nativegame.nattyengine.engine.Engine;
import com.nativegame.nattyengine.input.sensor.AccelerationController;
import com.nativegame.nattyengine.input.sensor.OrientationController;
import com.nativegame.nattyengine.ui.GameActivity;
import com.nativegame.nattyengine.ui.GameView;

Expand Down Expand Up @@ -36,15 +34,18 @@

public class Game {

protected final GameActivity mActivity;
protected final Engine mEngine;
private final GameActivity mActivity;
private final GameView mGameView;
private final Engine mEngine;

//--------------------------------------------------------
// Constructors
//--------------------------------------------------------
public Game(GameActivity activity, GameView gameView) {
public Game(GameActivity activity, GameView gameView, Engine engine) {
mActivity = activity;
mEngine = new Engine(gameView);
mGameView = gameView;
mEngine = engine;
mEngine.setGameView(gameView);
}
//========================================================

Expand All @@ -55,6 +56,10 @@ public GameActivity getActivity() {
return mActivity;
}

public GameView getGameView() {
return mGameView;
}

public Engine getEngine() {
return mEngine;
}
Expand All @@ -64,14 +69,16 @@ public Engine getEngine() {
// Methods
//--------------------------------------------------------
public final void start() {
mEngine.startGame();
onStart();
if (!mEngine.isRunning()) {
mEngine.startGame();
onStart();
}
}

public final void stop() {
if (mEngine.isRunning()) {
mEngine.stopGame();
mEngine.disposeGame();
mEngine.releaseGame();
onStop();
}
}
Expand Down Expand Up @@ -101,28 +108,6 @@ protected void onPause() {

protected void onResume() {
}

public void enableAccelerationSensor() {
mEngine.setAccelerationController(new AccelerationController(mActivity));
}

public void disableAccelerationSensor() {
if (mEngine.getAccelerationController() != null) {
mEngine.getAccelerationController().stop();
}
mEngine.setAccelerationController(null);
}

public void enableOrientationSensor() {
mEngine.setOrientationController(new OrientationController(mActivity));
}

public void disableOrientationSensor() {
if (mEngine.getOrientationController() != null) {
mEngine.getOrientationController().stop();
}
mEngine.setOrientationController(null);
}
//========================================================

}
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;
}
//========================================================

}
Loading

0 comments on commit e617dca

Please sign in to comment.