Advertisement
acrogenesis

main

May 6th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.98 KB | None | 0 0
  1. package com.brackeen.javagamebook.tilegame;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import java.awt.event.*;
  7. import java.util.Iterator;
  8.  
  9. import javax.sound.midi.Sequence;
  10. import javax.sound.midi.Sequencer;
  11. import javax.sound.sampled.AudioFormat;
  12.  
  13. import com.brackeen.javagamebook.graphics.*;
  14. import com.brackeen.javagamebook.sound.*;
  15. import com.brackeen.javagamebook.input.*;
  16. import com.brackeen.javagamebook.test.GameCore;
  17. import com.brackeen.javagamebook.tilegame.sprites.*;
  18. import com.brackeen.javagamebook.grabar.*;
  19.  
  20. import java.io.IOException;
  21. /**
  22.     GameManager manages all parts of the game.
  23. */
  24. public class GameManager extends GameCore {
  25.    
  26.     public static void main(String[] args) {
  27.         new GameManager().run();
  28.       }
  29.  
  30.     // uncompressed, 44100Hz, 16-bit, mono, signed, little-endian
  31.     private static final AudioFormat PLAYBACK_FORMAT =
  32.         new AudioFormat(44100, 16, 1, true, false);
  33.  
  34.     private static final int DRUM_TRACK = 1;
  35.    
  36.     public float GRAVITY = .002f;
  37.     public double peso = 6000;
  38.  
  39.  
  40.     private Point pointCache = new Point();
  41.     private TileMap map;
  42.     private MidiPlayer midiPlayer;
  43.     private SoundManager soundManager;
  44.     private ResourceManager resourceManager;
  45.     private Sound prizeSound;
  46.     private Sound boopSound;
  47.     private InputManager inputManager;
  48.     private TileMapRenderer renderer;
  49.     private int die;
  50.     private boolean key;
  51.     private GameAction moveLeft;
  52.     private GameAction moveRight;
  53.     private GameAction jump;
  54.     private GameAction exit;
  55.     private GameAction enter;
  56.     private GameAction upMenu;
  57.     private GameAction downMenu;
  58.     private boolean menusec = false;
  59.     private boolean agarrar=false;
  60.   private Grabar grabar;
  61.  
  62.   public void init() {
  63.         super.init();
  64.         die=0;
  65.         key=false;
  66.         // set up input manager
  67.         initInput();
  68.         // start resource manager
  69.         resourceManager = new ResourceManager(
  70.         screen.getFullScreenWindow().getGraphicsConfiguration());
  71.  
  72.         // load resources
  73.         renderer = new TileMapRenderer();
  74.         renderer.setBackground(resourceManager.loadImage("background.png"));
  75.            
  76.             renderer.setMenuImage(resourceManager.loadImage("menuprincipal1.png"));
  77.  
  78.         // load first map
  79.         map = resourceManager.loadNextMap();
  80.        
  81.         // load sounds
  82.         soundManager = new SoundManager(PLAYBACK_FORMAT);
  83.         prizeSound = soundManager.getSound("sounds/prize.wav");
  84.         boopSound = soundManager.getSound("sounds/boop2.wav");
  85.        
  86.         // start music
  87.         midiPlayer = new MidiPlayer();
  88.         Sequence sequence = midiPlayer.getSequence("sounds/music.midi");
  89.         midiPlayer.play(sequence, true);
  90.         toggleDrumPlayback();
  91.         try{
  92.           grabar.touchFile();
  93.         }catch(IOException er){
  94.           System.out.println("no pude crear!! ");
  95.           er.printStackTrace();
  96.         }
  97.            
  98.     }
  99.  
  100.  
  101.     /**
  102.         Closes any resurces used by the GameManager.
  103.     */
  104.     public void stop() {
  105.         super.stop();
  106.         midiPlayer.close();
  107.         soundManager.close();
  108.     }
  109.  
  110.  
  111.     private void initInput() {
  112.         moveLeft = new GameAction("moveLeft");
  113.         moveRight = new GameAction("moveRight");
  114.         enter = new GameAction ("Enter");
  115.         upMenu = new GameAction("upMenu");
  116.         downMenu = new GameAction("downMenu");
  117.         jump = new GameAction("jump",
  118.             GameAction.DETECT_INITAL_PRESS_ONLY);
  119.         exit = new GameAction("exit",
  120.             GameAction.DETECT_INITAL_PRESS_ONLY);
  121.  
  122.         inputManager = new InputManager(
  123.             screen.getFullScreenWindow());
  124.         inputManager.setCursor(InputManager.INVISIBLE_CURSOR);
  125.  
  126.         inputManager.mapToKey(enter, KeyEvent.VK_ENTER);
  127.         inputManager.mapToKey(upMenu, KeyEvent.VK_UP);
  128.         inputManager.mapToKey(downMenu, KeyEvent.VK_DOWN);
  129.         inputManager.mapToKey(moveLeft, KeyEvent.VK_LEFT);
  130.         inputManager.mapToKey(moveRight, KeyEvent.VK_RIGHT);
  131.         inputManager.mapToKey(jump, KeyEvent.VK_SPACE);
  132.         inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
  133.     }
  134.  
  135.     /**
  136.         Checks the Input at the menu, to select the appropiate screen.
  137.     */
  138.     private void checkInput(long elapsedTime) {
  139.        
  140.         if(!renderer.engine){   //menu browsing
  141.             if(!menusec){
  142.                 if(inputManager.getEnter()){
  143.                     renderer.pantalla += 5;
  144.                     inputManager.setEnter(false);
  145.                 }
  146.                 if(inputManager.getUp()){
  147.                     if(renderer.pantalla - 1 == 0){
  148.                         renderer.pantalla = 5;
  149.                         inputManager.setUp(false);
  150.                     }else{
  151.                         renderer.pantalla -= 1;
  152.                         inputManager.setUp(false);
  153.                     }
  154.                 }
  155.                 if(inputManager.getDown()){
  156.                     if(renderer.pantalla + 1 == 6){
  157.                         renderer.pantalla = 1;
  158.                         inputManager.setDown(false);
  159.                     }else{
  160.                         renderer.pantalla += 1;
  161.                         inputManager.setDown(false);
  162.                     }
  163.                 }
  164.            
  165.                 if(renderer.pantalla == 1){ //selecciona nuevo juego
  166.                     renderer.setMenuImage(resourceManager.loadImage("menuprincipal1.png"));
  167.                 }else if(renderer.pantalla == 2) //selecciona continuar
  168.                     renderer.setMenuImage(resourceManager.loadImage("menuprincipal2.png"));
  169.                 else if(renderer.pantalla == 3)//selecciona instrucciones
  170.                     renderer.setMenuImage(resourceManager.loadImage("menuprincipal3.png"));
  171.                 else if(renderer.pantalla == 4) //selecciona creditos
  172.                     renderer.setMenuImage(resourceManager.loadImage("menuprincipal4.png"));
  173.                 else if(renderer.pantalla == 5) //selecciona atras
  174.                     renderer.setMenuImage(resourceManager.loadImage("menuprincipal5.png"));
  175.                 //pantallas
  176.                 else if(renderer.pantalla == 7){ //pantalla continuar
  177.                     //leer del archivo el currentMap y hacer un setCurrentMap
  178.           resourceManager.setContinuar(true);
  179.           try {
  180.             resourceManager.loadMap("maps/map" + resourceManager.getCurrentMap() + ".txt");
  181.           }catch(Exception er){}
  182.                     renderer.pantalla = 6; //te manda al engine
  183.                 }
  184.                 else if(renderer.pantalla == 8){ //pantalla instrucciones
  185.                     renderer.setMenuImage(resourceManager.loadImage("instrucciones.png"));
  186.                     menusec = true;
  187.                 }
  188.                 else if(renderer.pantalla == 9){ //pantalla creditos
  189.                     renderer.setMenuImage(resourceManager.loadImage("creditosm.png"));
  190.                     menusec = true;
  191.                 }
  192.                 else if(renderer.pantalla == 10){ //pantalla atras
  193.                     renderer.pantalla = 5;
  194.                     menusec = true;
  195.                 }
  196.             }  
  197.             if(menusec){ //se entro a una de las pantallas secundarias del menu (instrucciones, creditos, continuar)
  198.                 if(inputManager.getEnter()){        //se presiona el boton "Atras" (boton unico) y te regresa la pantalla 1...
  199.                     renderer.pantalla -= 5;
  200.                     inputManager.setEnter(false);
  201.                     menusec = false;
  202.                 }
  203.             }          
  204.            
  205.         }
  206.        
  207.         if (exit.isPressed()) {
  208.                 stop();
  209.             }
  210.        
  211.         if(renderer.engine){
  212.             Player player = (Player)map.getPlayer();
  213.             if (player.isAlive()) {
  214.                 float velocityX = 0;
  215.                 if (moveLeft.isPressed()) {
  216.                     velocityX-=player.getMaxSpeed();
  217.                 }
  218.                 if (moveRight.isPressed()) {
  219.                     velocityX+=player.getMaxSpeed();
  220.                 }
  221.                 if (jump.isPressed()) {
  222.                     player.jump(false);
  223.                 }
  224.                 player.setVelocityX(velocityX);
  225.             }
  226.         }    
  227.     }
  228.  
  229.     public void draw(Graphics2D g) {
  230.             renderer.draw(g, map,
  231.                 screen.getWidth(), screen.getHeight());
  232.     }
  233.  
  234.  
  235.     /**
  236.         Gets the current map.
  237.     */
  238.     public TileMap getMap() {
  239.         return map;
  240.     }
  241.  
  242.  
  243.     /**
  244.         Turns on/off drum playback in the midi music (track 1).
  245.     */
  246.     public void toggleDrumPlayback() {
  247.         Sequencer sequencer = midiPlayer.getSequencer();
  248.         if (sequencer != null) {
  249.             sequencer.setTrackMute(DRUM_TRACK,
  250.                 !sequencer.getTrackMute(DRUM_TRACK));
  251.         }
  252.     }
  253.  
  254.  
  255.     /**
  256.         Gets the tile that a Sprites collides with. Only the
  257.         Sprite's X or Y should be changed, not both. Returns null
  258.         if no collision is detected.
  259.     */
  260.     public Point getTileCollision(Sprite sprite,
  261.         float newX, float newY)
  262.     {
  263.         float fromX = Math.min(sprite.getX(), newX);
  264.         float fromY = Math.min(sprite.getY(), newY);
  265.         float toX = Math.max(sprite.getX(), newX);
  266.         float toY = Math.max(sprite.getY(), newY);
  267.  
  268.         // get the tile locations
  269.         int fromTileX = TileMapRenderer.pixelsToTiles(fromX);
  270.         int fromTileY = TileMapRenderer.pixelsToTiles(fromY);
  271.         int toTileX = TileMapRenderer.pixelsToTiles(
  272.             toX + sprite.getWidth() - 1);
  273.         int toTileY = TileMapRenderer.pixelsToTiles(
  274.             toY + sprite.getHeight() - 1);
  275.  
  276.         // check each tile for a collision
  277.         for (int x=fromTileX; x<=toTileX; x++) {
  278.             for (int y=fromTileY; y<=toTileY; y++) {
  279.                 if (x < 0 || x >= map.getWidth() ||
  280.                     map.getTile(x, y) != null)
  281.                 {
  282.                     // collision found, return the tile
  283.                     pointCache.setLocation(x, y);
  284.                     return pointCache;
  285.                 }
  286.             }
  287.         }
  288.  
  289.         // no collision found
  290.         return null;
  291.     }
  292.  
  293.  
  294.     /**
  295.         Checks if two Sprites collide with one another. Returns
  296.         false if the two Sprites are the same. Returns false if
  297.         one of the Sprites is a Creature that is not alive.
  298.     */
  299.     public boolean isCollision(Sprite s1, Sprite s2) {
  300.         // if the Sprites are the same, return false
  301.         if (s1 == s2) {
  302.             return false;
  303.         }
  304.  
  305.         // if one of the Sprites is a dead Creature, return false
  306.         if (s1 instanceof Creature && !((Creature)s1).isAlive()) {
  307.             return false;
  308.         }
  309.         if (s2 instanceof Creature && !((Creature)s2).isAlive()) {
  310.             return false;
  311.         }
  312.  
  313.         // get the pixel location of the Sprites
  314.         int s1x = Math.round(s1.getX());
  315.         int s1y = Math.round(s1.getY());
  316.         int s2x = Math.round(s2.getX());
  317.         int s2y = Math.round(s2.getY());
  318.  
  319.         // check if the two sprites' boundaries intersect
  320.         return (s1x < s2x + s2.getWidth() &&
  321.             s2x < s1x + s1.getWidth() &&
  322.             s1y < s2y + s2.getHeight() &&
  323.             s2y < s1y + s1.getHeight());
  324.     }
  325.  
  326.  
  327.     /**
  328.         Gets the Sprite that collides with the specified Sprite,
  329.         or null if no Sprite collides with the specified Sprite.
  330.     */
  331.     public Sprite getSpriteCollision(Sprite sprite) {
  332.  
  333.         // run through the list of Sprites
  334.         Iterator i = map.getSprites();
  335.         while (i.hasNext()) {
  336.             Sprite otherSprite = (Sprite)i.next();
  337.             if (isCollision(sprite, otherSprite)) {
  338.                 // collision found, return the Sprite
  339.                 return otherSprite;
  340.             }
  341.         }
  342.  
  343.         // no collision found
  344.         return null;
  345.     }
  346.  
  347.  
  348.     /**
  349.         Updates Animation, position, and velocity of all Sprites
  350.         in the current map.
  351.     */
  352.     public void update(long elapsedTime) {
  353.         Creature player = (Creature)map.getPlayer();
  354.             renderer.setWeight(GRAVITY*peso*3);
  355.         renderer.setNivel(resourceManager.getCurrentMap());
  356.  
  357.  
  358.         // player is dead! start map over
  359.         if (player.getState() == Creature.STATE_DEAD) {
  360.             map = resourceManager.reloadMap();
  361.             return;
  362.         }
  363.  
  364.         // get keyboard/mouse input
  365.         checkInput(elapsedTime);
  366.  
  367.         // update player
  368.         updateCreature(player, elapsedTime);
  369.         player.update(elapsedTime);
  370.  
  371.         // update other sprites
  372.         Iterator i = map.getSprites();
  373.         while (i.hasNext()) {
  374.             Sprite sprite = (Sprite)i.next();
  375.             if (sprite instanceof Creature) {
  376.                 Creature creature = (Creature)sprite;
  377.                 if (creature.getState() == Creature.STATE_DEAD) {
  378.                     i.remove();
  379.                 }
  380.                 else {
  381.                     updateCreature(creature, elapsedTime);
  382.                 }
  383.             }
  384.             else if(sprite instanceof PowerUp.gate && agarrar){
  385.                 i.remove();
  386.             }
  387.             // normal update
  388.             sprite.update(elapsedTime);
  389.         }
  390.     }
  391.  
  392.  
  393.     /**
  394.         Updates the creature, applying gravity for creatures that
  395.         aren't flying, and checks collisions.
  396.     */
  397.     private void updateCreature(Creature creature,
  398.         long elapsedTime)
  399.     {
  400.  
  401.         // apply gravity
  402.         if (!creature.isFlying()) {
  403.             creature.setVelocityY(creature.getVelocityY() +
  404.                 GRAVITY * elapsedTime);
  405.         }
  406.  
  407.         // change x
  408.         float dx = creature.getVelocityX();
  409.         float oldX = creature.getX();
  410.         float newX = oldX + dx * elapsedTime;
  411.         Point tile =
  412.             getTileCollision(creature, newX, creature.getY());
  413.         if (tile == null) {
  414.             creature.setX(newX);
  415.         }
  416.         else {
  417.             // line up with the tile boundary
  418.             if (dx > 0) {
  419.                 creature.setX(
  420.                     TileMapRenderer.tilesToPixels(tile.x) -
  421.                     creature.getWidth());
  422.             }
  423.             else if (dx < 0) {
  424.                 creature.setX(
  425.                     TileMapRenderer.tilesToPixels(tile.x + 1));
  426.             }
  427.             creature.collideHorizontal();
  428.         }
  429.         if (creature instanceof Player) {
  430.             checkPlayerCollision((Player)creature, false);
  431.         }
  432.  
  433.         // change y
  434.         float dy = creature.getVelocityY();
  435.         float oldY = creature.getY();
  436.         float newY = oldY + dy * elapsedTime;
  437.         tile = getTileCollision(creature, creature.getX(), newY);
  438.         if (tile == null) {
  439.             creature.setY(newY);
  440.         }
  441.         else {
  442.             // line up with the tile boundary
  443.             if (dy > 0) {
  444.                 creature.setY(
  445.                     TileMapRenderer.tilesToPixels(tile.y) -
  446.                     creature.getHeight());
  447.             }
  448.             else if (dy < 0) {
  449.                 creature.setY(
  450.                     TileMapRenderer.tilesToPixels(tile.y + 1));
  451.             }
  452.             creature.collideVertical();
  453.         }
  454.         if (creature instanceof Player) {
  455.             boolean canKill = (oldY < creature.getY());
  456.             checkPlayerCollision((Player)creature, canKill);
  457.         }
  458.  
  459.     }
  460.  
  461.  
  462.     /**
  463.         Checks for Player collision with other Sprites. If
  464.         canKill is true, collisions with Creatures will kill
  465.         them.
  466.     */
  467.     public void checkPlayerCollision(Player player,
  468.         boolean canKill)
  469.     {
  470.         if (!player.isAlive()) {
  471.             return;
  472.         }
  473.  
  474.         // check for player collision with other sprites
  475.         Sprite collisionSprite = getSpriteCollision(player);
  476.         if (collisionSprite instanceof PowerUp) {
  477.             acquirePowerUp((PowerUp)collisionSprite);
  478.             if (die==1){
  479.             player.setState(Creature.STATE_DYING);
  480.                 die=0;
  481.             }
  482.         }
  483.         else if (collisionSprite instanceof Creature) {
  484.             Creature badguy = (Creature)collisionSprite;
  485.             if (canKill) {
  486.                 // kill the badguy and make player bounce
  487.                 soundManager.play(boopSound);
  488.                 badguy.setState(Creature.STATE_DYING);
  489.                 player.setY(badguy.getY() - player.getHeight());
  490.                 player.jump(true);
  491.             }
  492.             else {
  493.                 // player dies!
  494.                 player.setState(Creature.STATE_DYING);
  495.                
  496.             }
  497.         }
  498.     }
  499.  
  500.     /**
  501.         Gives the player the speicifed power up and removes it
  502.         from the map.
  503.     */
  504.     public void acquirePowerUp(PowerUp powerUp) {
  505.         // remove it from the map
  506.         if(powerUp instanceof PowerUp.Music && agarrar){
  507.             map.removeSprite(powerUp);
  508.             key=true;
  509.                 }
  510.                
  511.                
  512.        
  513.         if (powerUp instanceof PowerUp.Star) {
  514.             // reduce gravedad
  515.             soundManager.play(prizeSound);
  516.             if(GRAVITY > .0011){
  517.                 GRAVITY-=.000005;
  518.                 }
  519.         }
  520.         else if (powerUp instanceof PowerUp.Music) {
  521.             // change the music
  522.             soundManager.play(prizeSound);
  523.             toggleDrumPlayback();
  524.         }
  525.         else if (powerUp instanceof PowerUp.Goal) {
  526.             // advance to next map
  527.                 if(key){
  528.               soundManager.play(prizeSound,
  529.                   new EchoFilter(2000, .7f), false);
  530.               try{
  531.                     grabar.writeFile(resourceManager.getCurrentMap()+1);
  532.                 }catch (IOException er){
  533.                   System.out.println("no pude guardar!! ");
  534.                 er.printStackTrace();
  535.                 }
  536.               map = resourceManager.loadNextMap();
  537.                     GRAVITY = 0.002f;
  538.                     key=false;
  539.                     agarrar=false;
  540.                     resourceManager.jaula=true;
  541.              
  542.                 }
  543.         }
  544.         else if (powerUp instanceof PowerUp.plus){
  545.             // increase gravity
  546.             if(GRAVITY < .006){
  547.                 GRAVITY+=.000005;
  548.             }
  549.         }
  550.         else if (powerUp instanceof PowerUp.button){
  551.             // compara peso
  552.             if(peso*GRAVITY >=20){
  553.               agarrar=true;
  554.                 resourceManager.jaula=false;
  555.       }
  556.         }
  557.         else if (powerUp instanceof PowerUp.pico){
  558.         die=1;
  559.        
  560.              
  561.         }
  562.     }
  563.  
  564. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement