From 777bb28a4db72f4a6bd1c91e088a1e2a7db47c15 Mon Sep 17 00:00:00 2001 From: Tomas Kimer Date: Sun, 13 May 2012 23:22:15 +0200 Subject: [PATCH] Added new main map, edited old maps, automatic level switching, intro plays just once, fixed entity draw order (enemies are rendered above gems) --- src/entities/Player.java | 25 ++++++++++-------- src/game/GamePanel.java | 15 ++++++++--- src/game/GameStart.java | 40 +++++++++++++++-------------- src/game/MainMenu.java | 4 +++ src/game/RenderWindow.java | 22 ++++++++++++++-- src/library/Defaults.java | 12 ++++++++- src/library/resources/level0.map | 44 ++++++++++++++++---------------- src/library/resources/level1.map | 30 ++++++++++++++++------ src/library/resources/level2.map | 8 ++++++ 9 files changed, 135 insertions(+), 65 deletions(-) create mode 100644 src/library/resources/level2.map diff --git a/src/entities/Player.java b/src/entities/Player.java index 723f0d4..fe58f2b 100644 --- a/src/entities/Player.java +++ b/src/entities/Player.java @@ -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); @@ -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) @@ -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; @@ -120,9 +119,7 @@ public void move(int direction){ { SoundLibrary.play(SoundLibrary.SoundNames.GEM, 0); todel = a; - System.out.println("gem"); - collectedGemCount++; - + collectedGemCount++; } } } @@ -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); @@ -168,8 +168,13 @@ public long getEndTime() { return endTimeMs; } - public void resetEndTime() { + public long getDeathTime() { + return deathTimeMs; + } + + public void resetTimes() { endTimeMs = 0; + deathTimeMs = 0; } diff --git a/src/game/GamePanel.java b/src/game/GamePanel.java index 7182ff0..3287baa 100644 --- a/src/game/GamePanel.java +++ b/src/game/GamePanel.java @@ -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); diff --git a/src/game/GameStart.java b/src/game/GameStart.java index d6077d9..879a199 100644 --- a/src/game/GameStart.java +++ b/src/game/GameStart.java @@ -43,6 +43,7 @@ public class GameStart implements ActionListener{ private ArrayList ent = new ArrayList(); // other entities in game (like enemies) private TreeSet keysPressed = new TreeSet(); private TreeSet keyReleased = new TreeSet(); // the same as typed keys + private boolean introPlayed = false; // specify part of the game public enum GamePart { @@ -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(); @@ -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(); @@ -303,21 +310,7 @@ public void gameLoop(){ frames = 0; ticks = 0; } - /* - waitHere(10); - - processEvents(); - gameLogic(); - - if(screenChange){ - checkScreenChange(); - if(wantPlay) - playVideo(); - } - - game.render();*/ } - } /** @@ -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); } } } diff --git a/src/game/MainMenu.java b/src/game/MainMenu.java index 89235be..8b79930 100644 --- a/src/game/MainMenu.java +++ b/src/game/MainMenu.java @@ -46,6 +46,7 @@ public MainMenu(GameStart startObject) { cboxLevel = new JComboBox(); cboxLevel.addItem("1"); cboxLevel.addItem("2"); + cboxLevel.addItem("3"); GridBagConstraints gbc_cboxLevel = new GridBagConstraints(); gbc_btnStartNewGame.fill = GridBagConstraints.HORIZONTAL; @@ -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); diff --git a/src/game/RenderWindow.java b/src/game/RenderWindow.java index 10bee08..98f9398 100644 --- a/src/game/RenderWindow.java +++ b/src/game/RenderWindow.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import entities.AbstractEntity; +import entities.GemEntity; import entities.Player; import library.Defaults; @@ -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 @@ -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(); diff --git a/src/library/Defaults.java b/src/library/Defaults.java index 3a6bb9f..6b3a669 100644 --- a/src/library/Defaults.java +++ b/src/library/Defaults.java @@ -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"; @@ -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 { @@ -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; + } } diff --git a/src/library/resources/level0.map b/src/library/resources/level0.map index 9eafe19..ab15a0f 100644 --- a/src/library/resources/level0.map +++ b/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 \ No newline at end of file +61117770000000000000000007 +08117700000000001111110001 +00017800111111111000000000 +10011000170707000001111008 +11001100111111111111000001 +71101100710000001717000011 +01101000710010000017000000 +01001000110011110011111100 +00011001100000010011087100 +00110001801110010010000100 +00711001001910010010100100 +00010001101000010010110000 +10018011001111110810710000 +00110017001000000010011111 +10010017000001000010000000 +00011011111111100110000000 +10001000010001000011111100 +00001001010101007000000000 +00001111000101001000000000 +11000011111101101001111001 +77000000000001771771880007 +11000000000071771771708008 \ No newline at end of file diff --git a/src/library/resources/level1.map b/src/library/resources/level1.map index 08f5ea3..f6cba28 100644 --- a/src/library/resources/level1.map +++ b/src/library/resources/level1.map @@ -1,8 +1,22 @@ -000100000 -000000000 -011111110 -010000010 -010090011 -010000010 -011101110 -000000000 \ No newline at end of file +01010101000001000000010000 +01010101080000000000101110 +01010101000111111100000700 +01010171000100008100111110 +01010101000100100110010000 +01710101000100000101101110 +00000101000111011101010000 +01010101000000000000101110 +01010101001110001110001110 +01000001001111000011110070 +01010101701111000001111110 +01010101001010101010101010 +01010000000000000000000800 +01010101001011111111111110 +01010101000811111111111111 +01710101007000000091111111 +01010101700080111111111111 +01010101001110111111111111 +01010171001110000000111111 +01010000000111110110111111 +00000101001116101000111111 +01010101001110000011111111 \ No newline at end of file diff --git a/src/library/resources/level2.map b/src/library/resources/level2.map new file mode 100644 index 0000000..388188f --- /dev/null +++ b/src/library/resources/level2.map @@ -0,0 +1,8 @@ +800170077 +000000007 +011111110 +010000017 +010090011 +010000016 +011101110 +000000000 \ No newline at end of file