Add scrolling mechanics for GameScreen, show ShopScreen on new game.

This commit is contained in:
Jan Philipp Timme 2015-01-06 13:05:16 +01:00
parent 5a088729d6
commit 19baaed413
5 changed files with 117 additions and 21 deletions

View File

@ -16,6 +16,25 @@ public class ScrollingBackground extends Background {
*/
private int offset;
/**
* Internal counter for update ticks. Used as a measurement to stop/start scrolling.
*/
private int updateTickCount;
/**
* This guy is used as a speed divisor - it is used in a modulo operation.
* 1 - Scrolling at full speed, 101 - not scrolling at all
* 2 - Scrolling at half speed.
*/
private int updateFrameDivider;
/**
* Delta telling in which direction the scrolling speed will transition.
* (Basically, this is being added to the updateFrameDivider)
* Can be 0 (not changing), -1 or 1.
*/
private int updateFrameDelta;
/**
* Default constructor.
@ -23,6 +42,9 @@ public class ScrollingBackground extends Background {
public ScrollingBackground(int x, int y) {
super(x, y);
this.offset = 0;
this.updateTickCount = 1;
this.updateFrameDivider = 101;
this.updateFrameDelta = 0;
}
@ -33,16 +55,59 @@ public class ScrollingBackground extends Background {
this.scrollingSpeed = scrollingSpeed;
}
/**
*
*/
public void startScrolling() {
this.updateFrameDelta = -1;
this.updateFrameDivider = 10;
}
/**
*
*/
public void stopScrolling() {
this.updateFrameDelta = 1;
}
/**
* Tell whether the Background is currently scrolling.
*/
public boolean isScrolling() {
return this.updateFrameDivider == 1;
}
/**
* Tell whether the Background is currently changing the scrolling state.
*/
public boolean scrollingStateChanging() {
return this.updateFrameDelta != 0;
}
/**
* Apply the scrolling effect by updating an internal offset counter.
* Reset it when we scrolled for one image length.
*/
public void update() {
this.updateTickCount++;
if(this.updateTickCount == 100) this.updateTickCount = 1;
if(this.updateFrameDelta != 0 && this.updateTickCount % 10 == 0) {
this.updateFrameDivider += this.updateFrameDelta;
if(this.updateFrameDelta == -1 && this.updateFrameDivider == 1) {
this.updateFrameDelta = 0;
}
if(this.updateFrameDelta == 1 && this.updateFrameDivider == 10) {
this.updateFrameDelta = 0;
this.updateFrameDivider = 101;
}
}
if(this.updateTickCount % this.updateFrameDivider == 0) {
this.offset += this.scrollingSpeed;
if(Math.abs(this.offset) > this.getImage().getWidth()) {
this.offset += this.getImage().getWidth();
}
}
}
/**
* Paint the Background. Concatenate the image if necessary, but still try

View File

@ -266,7 +266,7 @@ public class PlayerSession {
PlayerSession.nextLevel = GameConfig.firstLevel;
PlayerSession.score = 0;
PlayerSession.secondaryWeapon = 1;
PlayerSession.credits = 0;
PlayerSession.credits = 10;
PlayerSession.baseHealthPoints = GameConfig.initialPlayerHealthPoints;
PlayerSession.baseShieldPoints = GameConfig.initialPlayerShieldPoints;
PlayerSession.baseShotDamage = GameConfig.initialPlayerShotDamage;

View File

@ -26,7 +26,7 @@ public class ItemNuke extends Item {
while (entityIterator.hasNext()) {
Entity entity = entityIterator.next();
if(entity instanceof Enemy) {
((LivingEntity) entity).takeDamage(20);
((LivingEntity) entity).takeDamage(9001);
}
}
}

View File

@ -3,6 +3,8 @@ package de.teamteamteam.spacescooter.level;
import java.awt.Point;
import java.util.ArrayList;
import de.teamteamteam.spacescooter.background.Background;
import de.teamteamteam.spacescooter.background.ScrollingBackground;
import de.teamteamteam.spacescooter.background.CloudBackground;
import de.teamteamteam.spacescooter.background.DogeBackground;
import de.teamteamteam.spacescooter.background.EarthBackground;
@ -71,6 +73,11 @@ public final class Level {
*/
private boolean playerWon;
/**
* Internal reference to the background. This is useful if the background needs to be influenced due to bossfights.
*/
private Background background;
/**
* EntityIterator to evaluate the state of all the existing Entities.
*/
@ -93,10 +100,14 @@ public final class Level {
* show the level name in a LevelHeadline.
*/
public void doBuildUp() {
this.spawnEntityByAvailableName(Entity.availableNames.valueOf(this.config.background), 0, 50, null);
this.background = (Background) this.spawnEntityByAvailableName(Entity.availableNames.valueOf(this.config.background), 0, 50, null);
GameScreen.setPlayer(new Player(200, 300));
this.backgroundMusic = SoundSystem.playSound(this.config.backgroundMusic);
new LevelHeadline(100,100, this.config.name);
//Have the background start scrolling in case it is supported.
if(this.background instanceof ScrollingBackground) {
((ScrollingBackground) this.background).startScrolling();
}
}
/**
@ -175,15 +186,30 @@ public final class Level {
this.isGameOver = true;
this.playerWon = false;
}
int bossEnemyCounter = 0;
int enemyCounter = 0;
int obstacleCounter = 0;
this.entityIterator.reset();
while(this.entityIterator.hasNext()) {
Entity e = this.entityIterator.next();
if(e instanceof EnemyBoss) bossEnemyCounter++;
if(e instanceof Enemy) enemyCounter++;
if(e instanceof Obstacle) obstacleCounter++;
}
//If there is a boss, make sure the background is not scrolling in case it is supported.
//Also resume scrolling when there is no more boss.
if(this.background instanceof ScrollingBackground) {
ScrollingBackground scrollingBackground = (ScrollingBackground) this.background;
if(scrollingBackground.scrollingStateChanging() == false) {
if(scrollingBackground.isScrolling() && bossEnemyCounter > 0) {
scrollingBackground.stopScrolling();
} else if(!scrollingBackground.isScrolling() && bossEnemyCounter == 0) {
scrollingBackground.startScrolling();
}
}
}
//use the currentIntervalIndex to determine whether there are things scheduled to spawn.
int currentIntervalIndex = this.config.getIntervalIndexByCurrentTime(this.levelClock);
if(enemyCounter == 0 && obstacleCounter == 0 && GameScreen.getPlayer().isAlive() && currentIntervalIndex == -1) {
@ -223,48 +249,51 @@ public final class Level {
/**
* Spawn an Entity by name on the given position.
* Uses Entity.availableNames to determine the actual Entity to spawn.
* Returns the spawned instance.
*/
private void spawnEntityByAvailableName(Entity.availableNames entity, int x, int y, ArrayList<Point> points) {
private Entity spawnEntityByAvailableName(Entity.availableNames entity, int x, int y, ArrayList<Point> points) {
Entity spawnedEntity = null;
switch(entity) {
case StarBackground:
new StarBackground(x, y);
spawnedEntity = new StarBackground(x, y);
break;
case CloudBackground:
new CloudBackground(x, y);
spawnedEntity = new CloudBackground(x, y);
break;
case DogeBackground:
new DogeBackground(x, y);
spawnedEntity = new DogeBackground(x, y);
break;
case EarthBackground:
new EarthBackground(x, y);
spawnedEntity = new EarthBackground(x, y);
break;
case EnemyOne:
new EnemyOne(x, y);
spawnedEntity = new EnemyOne(x, y);
break;
case EnemyTwo:
new EnemyTwo(x, y);
spawnedEntity = new EnemyTwo(x, y);
break;
case EnemyThree:
new EnemyThree(x, y);
spawnedEntity = new EnemyThree(x, y);
break;
case EnemyFour:
new EnemyFour(x, y, points);
spawnedEntity = new EnemyFour(x, y, points);
break;
case EnemyBoss:
new EnemyBoss(x, y);
spawnedEntity = new EnemyBoss(x, y);
break;
case StoneOne:
new StoneOne(x, y);
spawnedEntity = new StoneOne(x, y);
break;
case StoneTwo:
new StoneTwo(x, y);
spawnedEntity = new StoneTwo(x, y);
break;
case StoneThree:
new StoneThree(x, y);
spawnedEntity = new StoneThree(x, y);
break;
default:
System.err.println("I don't know how to spawn this: " + entity);
break;
}
return spawnedEntity;
}
}

View File

@ -23,10 +23,11 @@ public class MainMenuScreen extends Screen {
private int menuPoint = 0;
private boolean keyPressed = false;
private int animationStatus = 0; //0 = Animation noch nicht gestartet, 1 = Animation laeuft, 2 = Animation beendet
private StarBackground background;
public MainMenuScreen(Screen parent) {
super(parent);
new StarBackground(0, 0);
this.background = new StarBackground(0, 0);
new Button(GameConfig.windowWidth/2-125, 200);
new Button(GameConfig.windowWidth/2-125, 275);
new Button(GameConfig.windowWidth/2-125, 350);
@ -87,6 +88,7 @@ public class MainMenuScreen extends Screen {
// make a selection
if(Keyboard.isKeyDown(KeyEvent.VK_ENTER) || Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {
animationStatus = 1;
this.background.startScrolling();
}
if(animationStatus == 1) {
@ -97,7 +99,7 @@ public class MainMenuScreen extends Screen {
} else if(animationStatus == 2) {
switch (menuPoint) {
case 0:
this.parent.setOverlay(new GameScreen(this.parent));
this.parent.setOverlay(new ShopScreen(this.parent));
break;
case 1:
this.parent.setOverlay(new HelpScreen(this.parent));