Skip to content

Commit

Permalink
Added new main map, edited old maps, automatic level switching, intro…
Browse files Browse the repository at this point in the history
… plays just once, fixed entity draw order (enemies are rendered above gems)
  • Loading branch information
TomasKimer committed May 13, 2012
1 parent e952ca6 commit 777bb28
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 65 deletions.
25 changes: 15 additions & 10 deletions src/entities/Player.java
Expand Up @@ -24,6 +24,7 @@ public class Player extends AbstractEntity {
private int gemCount;
private long lastHurtMs = 0;
private long endTimeMs = 0;
private long deathTimeMs = 0;

public Player(int locX, int locY, MapLibrary map, Image avatar, RenderWindow window, int gemCount){
super(locX, locY, map, avatar, window);
Expand Down Expand Up @@ -91,11 +92,10 @@ public void move(int direction){
locTile[0]--;
nextTile[0]--;
}
}

}

if(moveTile){
System.out.println("Actual tile: " + map.getTile(locTile[0], locTile[1]) + " " + locTile[0] + " " + locTile[1]);
if (moveTile){
//System.out.println("Actual tile: " + map.getTile(locTile[0], locTile[1]) + " " + locTile[0] + " " + locTile[1]);
window.followPlayer(nextTile[0],nextTile[1]);

if (map.getTile(locTile[0], locTile[1]) == 16)
Expand All @@ -107,8 +107,7 @@ public void move(int direction){
SoundLibrary.play(SoundLibrary.SoundNames.EXIT, 0);
}
else
SoundLibrary.play(SoundLibrary.SoundNames.LOCKED, 0);

SoundLibrary.play(SoundLibrary.SoundNames.LOCKED, 0);
}

AbstractEntity todel = null;
Expand All @@ -120,9 +119,7 @@ public void move(int direction){
{
SoundLibrary.play(SoundLibrary.SoundNames.GEM, 0);
todel = a;
System.out.println("gem");
collectedGemCount++;

collectedGemCount++;
}
}
}
Expand Down Expand Up @@ -156,7 +153,10 @@ public void hurt()
{
health--;
if (health == 0)
{
SoundLibrary.play(SoundLibrary.SoundNames.DEATH, 300);
deathTimeMs = System.currentTimeMillis();
}
else
SoundLibrary.play(SoundLibrary.SoundNames.LIVELOST, 300);

Expand All @@ -168,8 +168,13 @@ public long getEndTime() {
return endTimeMs;
}

public void resetEndTime() {
public long getDeathTime() {
return deathTimeMs;
}

public void resetTimes() {
endTimeMs = 0;
deathTimeMs = 0;
}


Expand Down
15 changes: 12 additions & 3 deletions src/game/GamePanel.java
Expand Up @@ -95,15 +95,24 @@ public void paintComponent(Graphics g){

g2.drawImage(input, 0, 0, this);

g2.setColor(Color.white);
if (refPlayer.getCollectedGemCount() == mLib.getGemCount())
g2.setColor(Color.green);
else
g2.setColor(Color.white);
g2.drawString("GEMS: "+refPlayer.getCollectedGemCount()+"/"+mLib.getGemCount(), 0, 30);
g2.drawString("HEALTH: "+refPlayer.getHealth()+"/"+Defaults.getMaxHealth(), 0, 15);


if (refPlayer.getHealth() <= 1)
g2.setColor(Color.red);
else
g2.setColor(Color.white);

g2.drawString("HEALTH: "+refPlayer.getHealth()+"/"+Defaults.getMaxHealth(), 0, 15);

g2.setColor(Color.lightGray);
g2.drawString("Ticks: "+ticksToShow, Defaults.getAppResolutionX() - 60, 15);
g2.drawString("FPS: "+fpsToShow, Defaults.getAppResolutionX() - 60, 30);

g2.setColor(Color.white);
if (refPlayer.getHealth() <= 0 || refPlayer.exited())
{
Font f = new Font(g2.getFont().getName(), g2.getFont().getStyle(), g2.getFont().getSize()+20);
Expand Down
40 changes: 21 additions & 19 deletions src/game/GameStart.java
Expand Up @@ -43,6 +43,7 @@ public class GameStart implements ActionListener{
private ArrayList<AbstractEntity> ent = new ArrayList<AbstractEntity>(); // other entities in game (like enemies)
private TreeSet<GameKeys> keysPressed = new TreeSet<GameKeys>();
private TreeSet<GameKeys> keyReleased = new TreeSet<GameKeys>(); // the same as typed keys
private boolean introPlayed = false;

// specify part of the game
public enum GamePart {
Expand All @@ -58,8 +59,13 @@ public GameStart(){
@Override
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().contains("Start New Level")){
setScreen(GamePart.INTRO);
//setScreen(GamePart.GAME);
if (!introPlayed)
{
setScreen(GamePart.INTRO);
introPlayed = true;
}
else
setScreen(GamePart.GAME);
ent.clear();
game.restartGame(menu.selectedLevel());
loadEntities();
Expand Down Expand Up @@ -239,6 +245,7 @@ private void playVideo(){

/**
* Main game loop
* Inspired by Minicraft (https://s3.amazonaws.com/ld48/ld22/index.html)
*/
public void gameLoop(){
init();
Expand Down Expand Up @@ -303,21 +310,7 @@ public void gameLoop(){
frames = 0;
ticks = 0;
}
/*
waitHere(10);
processEvents();
gameLogic();
if(screenChange){
checkScreenChange();
if(wantPlay)
playVideo();
}
game.render();*/
}

}

/**
Expand Down Expand Up @@ -367,11 +360,20 @@ else if(k == GameKeys.D)
if (!ent.isEmpty())
ent.clear();

if (player.getEndTime() != 0 && System.currentTimeMillis() - player.getEndTime() > 2000)
if (player.exited() && player.getEndTime() != 0 && System.currentTimeMillis() - player.getEndTime() > Defaults.getSwitchLevelInterval())
{
// next level
//setScreen(GamePart.GAME);
menu.selectNextLevel();
game.restartGame(menu.selectedLevel());
loadEntities();
}
else if (player.getHealth() <= 0 && player.getDeathTime() != 0 && System.currentTimeMillis() - player.getDeathTime() > Defaults.getGameOverToMenuInterval())
{
// return to menu
player.resetTimes();
menu.resumeGameVisibility(true);
setScreen(GamePart.MENU);
player.resetEndTime();
setScreen(GamePart.MENU);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/game/MainMenu.java
Expand Up @@ -46,6 +46,7 @@ public MainMenu(GameStart startObject) {
cboxLevel = new JComboBox<String>();
cboxLevel.addItem("1");
cboxLevel.addItem("2");
cboxLevel.addItem("3");

GridBagConstraints gbc_cboxLevel = new GridBagConstraints();
gbc_btnStartNewGame.fill = GridBagConstraints.HORIZONTAL;
Expand Down Expand Up @@ -90,6 +91,9 @@ public int selectedLevel() {
return cboxLevel.getSelectedIndex();
}

public void selectNextLevel() {
cboxLevel.setSelectedIndex((cboxLevel.getSelectedIndex() + 1) % cboxLevel.getItemCount());
}

public void resumeGameVisibility(boolean flag){
btnResumeGame.setVisible(flag);
Expand Down
22 changes: 20 additions & 2 deletions src/game/RenderWindow.java
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;

import entities.AbstractEntity;
import entities.GemEntity;
import entities.Player;

import library.Defaults;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void loadScreen(){
* @param y +/- change in y axis
*/
public void followPlayer(int x, int y){
System.out.println("move tile: " + x + " " + y);
//System.out.println("move tile: " + x + " " + y);
short[][] newMap = new short[tiles[1]][tiles[0]];

// move two directions at once
Expand Down Expand Up @@ -210,14 +211,31 @@ public void renderEntities(){
Graphics2D g2 = finalImg.createGraphics();
g2.drawImage(background, 0, 0, null);

// first render player
for(AbstractEntity e : ent){
if(e instanceof Player){
locP = e.getTileLocation();
pixP = e.getPixelLocation();
g2.drawImage(e.getAvatarImage(), ((locP[0] - minmaxX[0])*tileRes)+pixP[0],
((locP[1] - minmaxY[0])*tileRes)+pixP[1], null);
break;
}
else
}

// then render gems
for(AbstractEntity e : ent){
if (e instanceof GemEntity)
{
int[] loc = e.getTileLocation();
int[] pix = e.getPixelLocation();
g2.drawImage(e.getAvatarImage(), ((loc[0] - minmaxX[0])*tileRes)+pix[0],
((loc[1] - minmaxY[0])*tileRes)+pix[1], null);
}
}

// finally render others
for(AbstractEntity e : ent){
if (!(e instanceof GemEntity) && !(e instanceof Player))
{
int[] loc = e.getTileLocation();
int[] pix = e.getPixelLocation();
Expand Down
12 changes: 11 additions & 1 deletion src/library/Defaults.java
Expand Up @@ -17,7 +17,7 @@
public class Defaults {
private static final int appResolutionX = 544;
private static final int appResolutionY = 544;
private static final String[] mapFile = {"resources/level0.map","resources/level1.map"};
private static final String[] mapFile = {"resources/level0.map","resources/level1.map", "resources/level2.map"};
private static final String imageFile = "resources/terrain.png";
private static final int imageResTile = 32;
private static final String freeTile = "dirt";
Expand All @@ -31,6 +31,8 @@ public class Defaults {
private static final int maxHealth = 5;
private static final int playerHurtIntervalMs = 1500;
private static final boolean renderAlways = true;
private static final int switchLevelIntervalMs = 2000;
private static final int gameOverToMenuIntervalMs = 3000;


static {
Expand Down Expand Up @@ -136,4 +138,12 @@ public static int getHurtInterval() {
public static boolean getRenderAlways() {
return renderAlways;
}

public static int getSwitchLevelInterval() {
return switchLevelIntervalMs;
}

public static int getGameOverToMenuInterval() {
return gameOverToMenuIntervalMs;
}
}
44 changes: 22 additions & 22 deletions src/library/resources/level0.map
@@ -1,22 +1,22 @@
01010101000001000000010000
01010101000000000000101110
01010101000111111100000000
01010101000100000100111110
01010101000100100110010000
01010101000100000101101110
00000101000111011101010000
01010101000000000000101110
01010101001110001110001110
01000001001111000011110000
01010101001111000001111110
01010101001010101010101010
01010000000000000000000000
01010101001011111111111110
01010101000811111111111111
01010101007000088891111111
01010101700088111111111111
01010101001110111111111111
01010101001110000000111111
01010101000111110110111111
01010101001116101000111111
01010101001110000011111111
61117770000000000000000007
08117700000000001111110001
00017800111111111000000000
10011000170707000001111008
11001100111111111111000001
71101100710000001717000011
01101000710010000017000000
01001000110011110011111100
00011001100000010011087100
00110001801110010010000100
00711001001910010010100100
00010001101000010010110000
10018011001111110810710000
00110017001000000010011111
10010017000001000010000000
00011011111111100110000000
10001000010001000011111100
00001001010101007000000000
00001111000101001000000000
11000011111101101001111001
77000000000001771771880007
11000000000071771771708008
30 changes: 22 additions & 8 deletions src/library/resources/level1.map
@@ -1,8 +1,22 @@
000100000
000000000
011111110
010000010
010090011
010000010
011101110
000000000
01010101000001000000010000
01010101080000000000101110
01010101000111111100000700
01010171000100008100111110
01010101000100100110010000
01710101000100000101101110
00000101000111011101010000
01010101000000000000101110
01010101001110001110001110
01000001001111000011110070
01010101701111000001111110
01010101001010101010101010
01010000000000000000000800
01010101001011111111111110
01010101000811111111111111
01710101007000000091111111
01010101700080111111111111
01010101001110111111111111
01010171001110000000111111
01010000000111110110111111
00000101001116101000111111
01010101001110000011111111
8 changes: 8 additions & 0 deletions src/library/resources/level2.map
@@ -0,0 +1,8 @@
800170077
000000007
011111110
010000017
010090011
010000016
011101110
000000000

0 comments on commit 777bb28

Please sign in to comment.