Package multididdy_view

Source Code of multididdy_view.View

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package multididdy_view;

import multididdy_model.SpriteModel;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;


/**
*
* @author chris
*/
public class View extends BasicGame{
    public Gui gui;
    Sprite player;
    SortedTiledMap map;
    InputListener listener;
   
    public View(){
        super("MultiDiddy");
    }

    @Override
    public void init(GameContainer gc) throws SlickException {
        gui=new Gui(gc);
       
        map=new SortedTiledMap("data/maps/small.tmx");
      
       
        player=addSprite("data/gfx/ogre.png",256,256,new SpriteModel(140,160,100,100,map));
        addSprite("data/gfx/ogre.png",256,256,new SpriteModel(190,230,100,100,map));
    }
   
    public Sprite addSprite(String path, int w, int h,SpriteModel model){
        Sprite sprite=new Sprite();
       
        try{
            SpriteSheet sheet=new SpriteSheet(path,w,h);
           
            sprite.model=model;
            sprite.addAnimation(sheet, 100);
           
           
            map.sprites.add(sprite);
           
        }catch(SlickException ex){
            ex.printStackTrace();
        }
       
            return sprite;
    }
   
    public void addInputListener( InputListener listener){
       
            this.listener=listener;
    }
   
   
   

    @Override
    public void update(GameContainer gc, int d) throws SlickException {
        Input input = gui.guiContext.getInput();
        listener.handleInput(input);
       
            float dx=0,dy=0;
        if(gc.getInput().isKeyDown(gc.getInput().KEY_LEFT)){
            dx=-0.2f;
        }
        if(gc.getInput().isKeyDown(gc.getInput().KEY_RIGHT)){
            dx=0.2f;
        }
        if(gc.getInput().isKeyDown(gc.getInput().KEY_UP)){
            dy=-0.2f;
        }
        if(gc.getInput().isKeyDown(gc.getInput().KEY_DOWN)){
            dy=0.2f;
        }
        if(dx!=0 || dy!=0){
           player.startAnimation();
           player.model.move(dx,dy);
        }else{
           player.setFrame(0);
           player.stopAnimation();
        }
           
        /*for(int i=0;i<sprites.size();i++){
            sprites.get(i).model.update();
        }*/
    }
   
     @Override
    public void render(GameContainer gc, Graphics grphcs) throws SlickException {
      // gui.render(gc, grphcs);
       //ogre.draw();//0,0,ogre.getWidth(),ogre.getHeight());
      
        for(int y=0;y<map.getHeight();y++){
            //Render ground layer
            map.render(100, 100+y*map.getTileHeight(), 0,y,map.getWidth(),1,0,true);
        
            //Render first layer
            map.render(100, 88+y*map.getTileHeight(), 0,y,map.getWidth(),1,1,false);
           
        }
        
        
        
        
    }
   

  
   
}
TOP

Related Classes of multididdy_view.View

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.