Package org.nrs.jant

Source Code of org.nrs.jant.JACMain

package org.nrs.jant;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.nrs.jant.res.JACConstants;
import org.nrs.jant.sprite.Player;

import processing.core.PApplet;
import processing.core.PImage;

public class JACMain extends PApplet {

  public static Logger logger = Logger.getLogger(JACMain.class.getName());
 
  static {
    Layout layout = new PatternLayout("%-5p [%t]: %C%n%m%n");
    Appender appender = new ConsoleAppender(layout);
    Logger.getRootLogger().addAppender(appender);
  }
  public static final int LEFT = 1;
  public static final int UP = 2;
  public static final int RIGHT = 4;
  public static final int DOWN = 8;
 
  private int control = 0;
 
  private Player player;
  int locx = 0;
  int locy = 0;
 
  PImage bg;
 
  public static void main(String [] args) {
    final JACMain main = new JACMain();

    JFrame frame = new JFrame("Test");
    frame.setSize(new Dimension(500,500));
   
    frame.setLayout(new BorderLayout());
   
    frame.add(main, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   
    main.addComponentListener(new ComponentListener(){

      public void componentHidden(ComponentEvent e) {
      }

      public void componentMoved(ComponentEvent e) {
      }

      public void componentResized(ComponentEvent e) {
       
        main.setSize(e.getComponent().getSize());
      }

      public void componentShown(ComponentEvent e) {
      }
    });
   
    main.init();
    main.start();

  }

  @Override
  public void setup() {
    bg = loadImage(JACConstants.BACKGROUND);
    player = new Player(loadImage(JACConstants.ANT));
    this.frameRate(45);
   
  }
 
  @Override
  public void draw() {
    image(bg, locx, locy, bg.width*4, bg.height*4);
    player.draw(this);
    move();
  }

  @Override
  public void setSize(Dimension d) {
    int width = (d.width - this.width)/2;
    int height = (d.height - this.height)/2;
   
    locx += width;
    locy += height;
    super.setSize(d);
  }
 
  @Override
  public void keyPressed() {
    switch(keyCode) {
      case 37:
        control |= LEFT;
      break;
      case 38:
        control |= UP;
      break;
      case 39:
        control |= RIGHT;
      break;
      case 40:
        control |= DOWN;
      break;
    }
  }
 
  @Override
  public void keyReleased() {
    switch(keyCode) {
      case 37:
        control ^= LEFT;
      break;
      case 38:
        control ^= UP;
      break;
      case 39:
        control ^= RIGHT;
      break;
      case 40:
        control ^= DOWN;
      break;
    }
  }
   
  public void move() {
   
    float dir = player.getDirection();
    float xVec = 0.0f;
    float yVec = 0.0f;
    boolean move = false;
    if( (control & UP) == UP ) {
      dir = 0;
      if( (control & RIGHT) == RIGHT ) {
        dir = PI/4;
      } else if( (control & LEFT) == LEFT ) {
        dir = -PI/4;
      }
      move = true;
    } else if( (control & DOWN) == DOWN ) {
      dir = PI;
      if( (control & RIGHT) == RIGHT ) {
        dir = (3*PI)/4;
      } else if( (control & LEFT) == LEFT ) {
        dir = -(3*PI)/4;
      }
      move = true;
    } else if( (control & RIGHT) == RIGHT ) {
      dir = PI/2;
      move = true;
    } else if( (control & LEFT) == LEFT ) {
      dir = -PI/2;
      move = true;
    }

    if( move ) {
      int roundFactor = 100000;
      yVec = (float)((int)(Math.cos(dir)*roundFactor));
      yVec = (float)(yVec/roundFactor);
      xVec = (float)((int)(Math.sin(dir)*roundFactor));
      xVec = (float)(xVec/roundFactor);
      locx -= player.getSpeed()*xVec;
      locy += player.getSpeed()*yVec;
    }
    player.setDirection(dir);
  }
 
}
 
TOP

Related Classes of org.nrs.jant.JACMain

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.