Permalink
Browse files

Lesson 22: unlocking / levelling up characters

  • Loading branch information...
vladimirslav committed Dec 22, 2016
1 parent 6eb7930 commit 564990aeed19a887e568666bf85d9b52865c7466
@@ -2,6 +2,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
+import com.coldwild.dodginghero.logic.objects.CharacterRecord;
/**
* Created by comrad_gremlin on 10/20/2016.
@@ -16,13 +17,17 @@
public static int currentCharacter = 0;
public static int currentGold = 0;
+ public static final int CHARACTER_PRICE = 1000;
+ public static int levels[]; // level of each character, 0 = locked
+
private static final String PROGRESS_SAVE_NAME = "progress";
private static final String SAVE_KEY_LIVES = "lives";
private static final String SAVE_KEY_LIVES_MAX = "livesmax";
private static final String SAVE_KEY_CURRENT_LEVEL = "currentlevel";
private static final String SAVE_KEY_PLAYER_DAMAGE = "playerdamage";
private static final String SAVE_KEY_PLAYER_GOLD = "playergold";
+ private static final String SAVE_KEY_PLAYER_LEVEL = "playerlevel";
public static int getEnemyLives()
{
@@ -38,17 +43,30 @@ public static void Save()
prefs.putInteger(SAVE_KEY_PLAYER_DAMAGE, playerDamage);
prefs.putInteger(SAVE_KEY_PLAYER_GOLD, currentGold);
+
+ for (int i = 0; i < CharacterRecord.CHARACTERS.length; i++)
+ {
+ prefs.putInteger(SAVE_KEY_PLAYER_LEVEL + i, levels[i]);
+ }
+
prefs.flush();
}
public static void Load()
{
+ levels = new int[CharacterRecord.CHARACTERS.length];
+
Preferences prefs = Gdx.app.getPreferences(PROGRESS_SAVE_NAME);
playerLives = prefs.getInteger(SAVE_KEY_LIVES, 3);
maxPlayerLives = prefs.getInteger(SAVE_KEY_LIVES_MAX, 3);
currentLevel = prefs.getInteger(SAVE_KEY_CURRENT_LEVEL, 0);
playerDamage = prefs.getInteger(SAVE_KEY_PLAYER_DAMAGE, 1);
currentGold = prefs.getInteger(SAVE_KEY_PLAYER_GOLD, 0);
+
+ for (int i = 0; i < CharacterRecord.CHARACTERS.length; i++)
+ {
+ levels[i] = prefs.getInteger(SAVE_KEY_PLAYER_LEVEL + i, i == 0 ? 1 : 0);
+ }
}
public static void Reset() {
@@ -57,4 +75,8 @@ public static void Reset() {
currentLevel = 0;
playerDamage = 1;
}
+
+ public static int getNextUpgradeCost(int currentCharacter) {
+ return levels[currentCharacter] * 2;
+ }
}
@@ -3,6 +3,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
+import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
@@ -24,23 +25,67 @@
void prepareUi()
{
+ uiStage.clear();
+
+ Label.LabelStyle textStyle = new Label.LabelStyle(game.res.gamefont, Color.WHITE);
TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.font = game.res.gamefont;
buttonStyle.fontColor = Color.WHITE;
- TextButton startButton = new TextButton("START", buttonStyle);
- startButton.setPosition((uiStage.getWidth() - startButton.getWidth()) / 2,
- uiStage.getHeight() / 6);
- startButton.addListener(new ClickListener()
+ if (GameProgress.levels[GameProgress.currentCharacter] == 0) // char is locked
{
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- dispose();
- game.setScreen(new GameScreen(game));
- }
- });
+ TextButton upgradeBtn = new TextButton("Unlock(1000 Gold)", buttonStyle);
+ upgradeBtn.setPosition((uiStage.getWidth() - upgradeBtn.getWidth()) / 2,
+ uiStage.getHeight() / 6);
+ upgradeBtn.addListener(new ClickListener()
+ {
+ @Override
+ public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
+ if (GameProgress.currentGold >= GameProgress.CHARACTER_PRICE)
+ {
+ GameProgress.currentGold -= GameProgress.CHARACTER_PRICE;
+ GameProgress.levels[GameProgress.currentCharacter] = 1;
+ prepareUi();
+ }
+ }
+ });
- uiStage.addActor(startButton);
+ uiStage.addActor(upgradeBtn);
+ }
+ else
+ {
+ TextButton startButton = new TextButton("START", buttonStyle);
+ startButton.setPosition((uiStage.getWidth() - startButton.getWidth()) / 2,
+ uiStage.getHeight() * 5 / 6);
+ startButton.addListener(new ClickListener()
+ {
+ @Override
+ public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
+ dispose();
+ game.setScreen(new GameScreen(game));
+ }
+ });
+ uiStage.addActor(startButton);
+
+ TextButton upgradeBtn = new TextButton("LvlUp(" +
+ GameProgress.getNextUpgradeCost(GameProgress.currentCharacter) + ")",
+ buttonStyle);
+ upgradeBtn.setPosition((uiStage.getWidth() - upgradeBtn.getWidth()) / 2,
+ uiStage.getHeight() / 6);
+ upgradeBtn.addListener(new ClickListener()
+ {
+ @Override
+ public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
+ if (GameProgress.currentGold >= GameProgress.getNextUpgradeCost(GameProgress.currentCharacter))
+ {
+ GameProgress.currentGold -= GameProgress.getNextUpgradeCost(GameProgress.currentCharacter);
+ GameProgress.levels[GameProgress.currentCharacter] += 1;
+ prepareUi();
+ }
+ }
+ });
+ uiStage.addActor(upgradeBtn);
+ }
Image heroSprite = new Image(
game.res.playerSprites.get(CharacterRecord.CHARACTERS[GameProgress.currentCharacter].name)
@@ -50,6 +95,12 @@ public void touchUp (InputEvent event, float x, float y, int pointer, int button
(uiStage.getHeight() - heroSprite.getHeight()) / 2);
uiStage.addActor(heroSprite);
+ int lvl = GameProgress.levels[GameProgress.currentCharacter];
+ Label statusText = new Label(lvl > 0 ? "LVL: " + lvl : "LOCKED", textStyle);
+ statusText.setPosition(heroSprite.getX() + (heroSprite.getWidth() - statusText.getWidth()) / 2,
+ heroSprite.getY() - statusText.getHeight() - 5);
+ uiStage.addActor(statusText);
+
TextButton nextButton = new TextButton(">>>", buttonStyle);
nextButton.addListener(new ClickListener()
{
@@ -61,7 +112,6 @@ public void touchUp (InputEvent event, float x, float y, int pointer, int button
GameProgress.currentCharacter = 0;
}
- uiStage.clear();
prepareUi();
}
});
@@ -80,7 +130,6 @@ public void touchUp (InputEvent event, float x, float y, int pointer, int button
GameProgress.currentCharacter = CharacterRecord.CHARACTERS.length - 1;
}
- uiStage.clear();
prepareUi();
}
});
@@ -93,7 +142,6 @@ public void touchUp (InputEvent event, float x, float y, int pointer, int button
uiStage.addActor(coinImage);
// amount of coins
- Label.LabelStyle textStyle = new Label.LabelStyle(game.res.gamefont, Color.WHITE);
Label coinAmntLbl = new Label("" + GameProgress.currentGold, textStyle);
coinAmntLbl.setPosition(coinImage.getX() + coinImage.getWidth() + 3,
@@ -126,4 +174,11 @@ public void dispose()
uiStage.dispose();
super.dispose();
}
+
+ @Override
+ public void resize(int w, int h)
+ {
+ super.resize(w, h);
+ uiStage.getViewport().update(w, h, true);
+ }
}

0 comments on commit 564990a

Please sign in to comment.