diff --git a/ChaseWhisply/src/main/java/fr/tvbarthel/games/chasewhisply/model/GameInformation.java b/ChaseWhisply/src/main/java/fr/tvbarthel/games/chasewhisply/model/GameInformation.java index a86f4b07..6028ebd8 100644 --- a/ChaseWhisply/src/main/java/fr/tvbarthel/games/chasewhisply/model/GameInformation.java +++ b/ChaseWhisply/src/main/java/fr/tvbarthel/games/chasewhisply/model/GameInformation.java @@ -7,7 +7,6 @@ import java.util.List; public class GameInformation implements Parcelable { - protected int mScore; protected long mRemainingTime; protected long mSpawningTime; protected Weapon mWeapon; @@ -15,15 +14,12 @@ public class GameInformation implements Parcelable { protected TargetableItem mCurrentTarget; protected List mTargetableItems; protected List mDisplayableItems; - protected int mTargetKilled; - protected int mBulletFired; - protected int mCurrentCombo; - protected int mMaxCombo; protected int mSceneWidth; protected int mSceneHeight; protected float mCurrentX; protected float mCurrentY; protected GameMode mGameMode; + protected ScoreInformation mScoreInformation; /** * Create a new GameInformation @@ -33,11 +29,7 @@ public class GameInformation implements Parcelable { * @param weapon weapon used for this game */ public GameInformation(long remainingTime, long spawningTime, Weapon weapon) { - mScore = 0; - mTargetKilled = 0; - mBulletFired = 0; - mCurrentCombo = 0; - mMaxCombo = 0; + mScoreInformation = new ScoreInformation(); mRemainingTime = remainingTime; mSpawningTime = spawningTime; mWeapon = weapon; @@ -57,11 +49,7 @@ public int describeContents() { } public void readFromParcel(Parcel in) { - mScore = in.readInt(); - mTargetKilled = in.readInt(); - mBulletFired = in.readInt(); - mCurrentCombo = in.readInt(); - mMaxCombo = in.readInt(); + mScoreInformation = in.readParcelable(ScoreInformation.class.getClassLoader()); mRemainingTime = in.readLong(); mSpawningTime = in.readLong(); mWeapon = in.readParcelable(Weapon.class.getClassLoader()); @@ -76,11 +64,7 @@ public void readFromParcel(Parcel in) { @Override public void writeToParcel(Parcel out, int i) { - out.writeInt(mScore); - out.writeInt(mTargetKilled); - out.writeInt(mBulletFired); - out.writeInt(mCurrentCombo); - out.writeInt(mMaxCombo); + out.writeParcelable(mScoreInformation, i); out.writeLong(mRemainingTime); out.writeLong(mSpawningTime); out.writeParcelable(mWeapon, i); @@ -195,7 +179,7 @@ public void removeTarget() { public void targetKilled() { mTargetableItems.remove(mCurrentTarget); mCurrentTarget = null; - mTargetKilled++; + mScoreInformation.increaseNumberOfTargetsKilled(); } /** @@ -204,14 +188,14 @@ public void targetKilled() { * @return number of frag */ public int getFragNumber() { - return mTargetKilled; + return mScoreInformation.getNumberOfTargetsKilled(); } /** * increase bullets fired number */ public void bulletFired() { - mBulletFired++; + mScoreInformation.increaseNumberOfBulletsFired(); } /** @@ -220,7 +204,7 @@ public void bulletFired() { * @return current combo */ public int getCurrentCombo() { - return mCurrentCombo; + return mScoreInformation.getCurrentCombo(); } /** @@ -229,36 +213,30 @@ public int getCurrentCombo() { * @return max combo number */ public int getMaxCombo() { - if (mCurrentCombo > mMaxCombo) mMaxCombo = mCurrentCombo; - return mMaxCombo; + return mScoreInformation.getMaxCombo(); } /** * increase combo if conditions are filled */ public void stackCombo() { - if (mTargetKilled > mCurrentCombo * mCurrentCombo) { - mCurrentCombo++; - } + mScoreInformation.increaseCurrentCombo(); } /** * reset current combo counter */ public void resetCombo() { - if (mCurrentCombo > mMaxCombo) mMaxCombo = mCurrentCombo; - mCurrentCombo = 0; + mScoreInformation.resetCurrentCombo(); } /** * increase score * - * @param ammount score you want to add to the current one + * @param amount score you want to add to the current one */ - public void increaseScore(int ammount) { - if (ammount > 0) { - mScore += ammount; - } + public void increaseScore(int amount) { + mScoreInformation.increaseScore(amount); } /** @@ -267,15 +245,12 @@ public void increaseScore(int ammount) { * @return current score */ public int getCurrentScore() { - return mScore; + return mScoreInformation.getScore(); } - public void setCurrentScore(int score) { - mScore = score; - } public int getBulletFired() { - return mBulletFired; + return mScoreInformation.getmNumberOfBulletsFired(); } public int getMaxTargetOnTheField() { @@ -290,6 +265,10 @@ public int getCurrentTargetsNumber() { return mTargetableItems.size(); } + public ScoreInformation getScoreInformation() { + return mScoreInformation; + } + public GameMode getGameMode() { return mGameMode; } @@ -317,4 +296,4 @@ public void setGameMode(GameMode gameMode) { mGameMode = gameMode; } -} +} \ No newline at end of file diff --git a/ChaseWhisply/src/main/java/fr/tvbarthel/games/chasewhisply/model/ScoreInformation.java b/ChaseWhisply/src/main/java/fr/tvbarthel/games/chasewhisply/model/ScoreInformation.java new file mode 100644 index 00000000..4e0a9872 --- /dev/null +++ b/ChaseWhisply/src/main/java/fr/tvbarthel/games/chasewhisply/model/ScoreInformation.java @@ -0,0 +1,145 @@ +package fr.tvbarthel.games.chasewhisply.model; + + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * ScoreInformation stores : + * the player score + * the number of targets killed + * the number of bullets fired + * the current combo + * the max combo reached by the player + */ +public class ScoreInformation implements Parcelable { + private int mScore; + private int mNumberOfTargetsKilled; + private int mNumberOfBulletsFired; + private int mCurrentCombo; + private int mMaxCombo; + + public ScoreInformation() { + mScore = 0; + mNumberOfBulletsFired = 0; + mNumberOfTargetsKilled = 0; + mCurrentCombo = 0; + mMaxCombo = 0; + } + + public ScoreInformation(Parcel in) { + readFromParcel(in); + } + + /** + * Increase the score by one. + */ + public void increaseScore() { + increaseScore(1); + } + + /** + * Increase the score by {@code amount}. + * + * @param amount + */ + public void increaseScore(int amount) { + mScore += amount; + } + + /** + * Increase the number of targets killed by one. + */ + public void increaseNumberOfTargetsKilled() { + mNumberOfTargetsKilled += 1; + } + + /** + * Increase the number of bullets fired by one. + */ + public void increaseNumberOfBulletsFired() { + mNumberOfBulletsFired += 1; + } + + /** + * If the number of targets killed is greater than + * the current Combo squared, increase the current combo by one. + * + * If the new current combo is higher than the max combo + * the max combo is set to the current combo. + */ + public void increaseCurrentCombo() { + if(mNumberOfTargetsKilled > mCurrentCombo * mCurrentCombo) { + mCurrentCombo += 1; + mMaxCombo = Math.max(mMaxCombo, mCurrentCombo); + } + } + + /** + * Set the current combo to 0. + */ + public void resetCurrentCombo() { + mCurrentCombo = 0; + } + + /* + Parcelable stuff + */ + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeInt(mScore); + out.writeInt(mNumberOfBulletsFired); + out.writeInt(mNumberOfTargetsKilled); + out.writeInt(mCurrentCombo); + out.writeInt(mMaxCombo); + } + + private void readFromParcel(Parcel in) { + mScore = in.readInt(); + mNumberOfBulletsFired = in.readInt(); + mNumberOfTargetsKilled = in.readInt(); + mCurrentCombo = in.readInt(); + mMaxCombo = in.readInt(); + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public ScoreInformation createFromParcel(Parcel in) { + return new ScoreInformation(in); + } + + public ScoreInformation[] newArray(int size) { + return new ScoreInformation[size]; + } + }; + + /* + Getters & Setters + */ + + public int getScore() { + return mScore; + } + + public int getNumberOfTargetsKilled() { + return mNumberOfTargetsKilled; + } + + public int getmNumberOfBulletsFired() { + return mNumberOfBulletsFired; + } + + public int getCurrentCombo() { + return mCurrentCombo; + } + + public int getMaxCombo() { + return mMaxCombo; + } + +} \ No newline at end of file