Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Savegamesimulator #175

Merged
merged 5 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 17
targetSdkVersion 28
versionCode 21
versionName "0.5-1"
versionName "0.5-2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ch.logixisland.anuto.util.math.Intersections;
import ch.logixisland.anuto.util.math.Line;
import ch.logixisland.anuto.util.math.Vector2;
import ch.logixisland.anuto.view.game.GameActivity;

public class DefaultGameSimulator extends GameSimulator {

Expand All @@ -32,10 +33,13 @@ public class DefaultGameSimulator extends GameSimulator {
private final TowerTiers mTowerTiers;
private final Random mRandom = new Random();
private final TickTimer mSaveAndLoadTimer = TickTimer.createInterval(120f);
private final TickTimer mSGTimer = TickTimer.createInterval(30f);
private final TickTimer mSimulationTickTimer = TickTimer.createInterval(0.5f);

DefaultGameSimulator(GameFactory gameFactory) {
super(gameFactory);
private int mSGMode = 0;

DefaultGameSimulator(GameActivity gameActivity, GameFactory gameFactory) {
super(gameActivity, gameFactory);
mTowerTiers = new TowerTiers(gameFactory);
}

Expand All @@ -51,6 +55,24 @@ protected void tick() {
if (mSaveAndLoadTimer.tick()) {
saveAndLoad();
}

if (mSGTimer.tick()) {
switch (++mSGMode) {
default:
//for mSaveAndLoadTimer
mSGMode = 0;
break;
case 1:
saveSG();
break;
case 2:
loadSG();
break;
case 3:
deleteSG();
break;
}
}
}

private void tryUpgradeTower() {
Expand Down
57 changes: 55 additions & 2 deletions app/src/androidTest/java/ch/logixisland/anuto/GameSimulator.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package ch.logixisland.anuto;

import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.util.concurrent.CountDownLatch;

import ch.logixisland.anuto.business.game.GameState;
import ch.logixisland.anuto.business.game.SaveGameInfo;
import ch.logixisland.anuto.engine.logic.loop.TickListener;
import ch.logixisland.anuto.util.iterator.Predicate;
import ch.logixisland.anuto.util.iterator.StreamIterator;
import ch.logixisland.anuto.view.game.GameActivity;

public abstract class GameSimulator {

private final static String TAG = GameSimulator.class.getSimpleName();

private final GameActivity mGameActivity;
private final GameFactory mGameFactory;

private CountDownLatch mFinishedLatch;

GameSimulator(GameFactory gameFactory) {
private SaveGameInfo mLastSG = null;

GameSimulator(GameActivity gameActivity, GameFactory gameFactory) {
mGameActivity = gameActivity;
mGameFactory = gameFactory;
}

Expand All @@ -28,6 +38,7 @@ void startSimulation() {
void waitForFinished() {
try {
mFinishedLatch.await();
deleteSG();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -39,6 +50,49 @@ protected GameFactory getGameFactory() {
return mGameFactory;
}

protected void saveSG() {
deleteSG();
mLastSG = SaveGameInfo.createSGI(mGameFactory.getGameLoader(), mGameFactory.getGameLoader().makeNewSavegame());
Thread thread = new Thread() {
public void run() {
mGameActivity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(mGameActivity, mGameActivity.getString(ch.logixisland.anuto.R.string.saveGameSuccessful), Toast.LENGTH_LONG).show();
}
});
}
};
thread.start();
}


private static Predicate<SaveGameInfo> byFolder(final File folder) {
return new Predicate<SaveGameInfo>() {
@Override
public boolean apply(SaveGameInfo value) {
return value.getFolder().equals(folder);
}
};
}

protected void loadSG() {
if (mLastSG != null) {
if (!StreamIterator.fromIterable(mGameFactory.getGameLoader().getSaveGameRepository()
.getSavegameInfos()).filter(byFolder(mLastSG.getFolder())).isEmpty())
mGameFactory.getGameLoader().loadGameState(mLastSG.getSavegameState());
else
mLastSG = null;
installTickHandler();
}
}

protected void deleteSG() {
if (mLastSG != null) {
mGameFactory.getGameLoader().deleteSavegame(mLastSG.getFolder());
mLastSG = null;
}
}

protected void saveAndLoad() {
mGameFactory.getGameLoader().saveGame();
mGameFactory.getGameLoader().loadGame();
Expand Down Expand Up @@ -106,5 +160,4 @@ private void simulationFinished() {
mFinishedLatch.countDown();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class IntegrationTest {

@Test
public void integrationTest() {
GameSimulator simulator = new DefaultGameSimulator(AnutoApplication.getInstance().getGameFactory());
GameSimulator simulator = new DefaultGameSimulator(mActivityRule.getActivity(), AnutoApplication.getInstance().getGameFactory());
simulator.startSimulation();
simulator.waitForFinished();
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
android:icon="@drawable/settings"
android:theme="@android:style/Theme.DeviceDefault">
</activity>
<activity
android:name=".view.loadmenu.LoadMenuActivity"
android:label="@string/savemenu"
android:icon="@drawable/settings"
android:theme="@android:style/Theme.DeviceDefault">
</activity>
<activity
android:name=".view.faq.FAQActivity"
android:label="@string/faq"
android:icon="@drawable/settings"
android:theme="@android:style/Theme.DeviceDefault">
</activity>

</application>

</manifest>
7 changes: 7 additions & 0 deletions app/src/main/java/ch/logixisland/anuto/AnutoApplication.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package ch.logixisland.anuto;

import android.app.Application;
import android.content.Context;

public class AnutoApplication extends Application {

private static Context appContext;
private static AnutoApplication sInstance;
private GameFactory mGameFactory;

@Override
public void onCreate() {
super.onCreate();
appContext = getApplicationContext();
sInstance = this;
mGameFactory = new GameFactory(getApplicationContext());
}

public static Context getContext() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No usages. Probably a left over.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed unnecessary in this branch, currently only used in my local branch.

return appContext;
}

public static AnutoApplication getInstance() {
return sInstance;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ch/logixisland/anuto/GameFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void initializeBusiness(Context context) {
mMapRepository = new MapRepository();
mScoreBoard = new ScoreBoard(mGameEngine);
mTowerSelector = new TowerSelector(mGameEngine, mScoreBoard);
mGameLoader = new GameLoader(context, mGameEngine, mGamePersister, mViewport, mEntityRegistry, mMapRepository);
mGameLoader = new GameLoader(context, mGameEngine, mGamePersister, mViewport, mEntityRegistry, mMapRepository, mRenderer);
mHighScores = new HighScores(context, mGameEngine, mScoreBoard, mGameLoader);
mGameState = new GameState(mScoreBoard, mHighScores, mTowerSelector);
mTowerAging = new TowerAging(mGameEngine);
Expand Down
Loading