Guest User

rpg

a guest
May 29th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. package rpg;
  2.  
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. import org.lwjgl.*;
  7. import org.lwjgl.input.Keyboard;
  8. import org.lwjgl.opengl.*;
  9. import static org.lwjgl.opengl.GL11.*;
  10.  
  11. import entities.*;
  12.  
  13. public class RPG {
  14.  
  15. public static final int WIDTH = 640;
  16. public static final int HEIGHT = 480;
  17. private boolean isRunning = true;
  18. private boolean inField = true;
  19. private boolean playerCanControl = true;
  20. static Player player;
  21. //static Enemy enemy;
  22.  
  23. public RPG() {
  24. setUpDisplay();
  25. setUpOpenGL();
  26. setUpEntites();
  27. setUpTimer();
  28. while (isRunning) {
  29. render();
  30. input();
  31. logic(getDelta());
  32. Display.update();
  33. Display.sync(60);
  34. if (Display.isCloseRequested()) {
  35. isRunning = false;
  36. }
  37. }
  38. }
  39.  
  40. private void input() {
  41. if(player.getX() % 32 == 0 && player.getY() % 32 == 0) {
  42. player.setDX(0);
  43. player.setDY(0);
  44. if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) player.setDX(-.5);
  45. else if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) player.setDX(.5);
  46. else if(Keyboard.isKeyDown(Keyboard.KEY_UP)) player.setDY(-.5);
  47. else if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) player.setDY(.5);
  48. }
  49.  
  50.  
  51. /*if (inField == true) {
  52. if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
  53. Keyboard.destroy();
  54. for (int x = 0; x < 32; x += 4) { player.setY(player.getY() - 4); }
  55. try {
  56. Keyboard.create();
  57. } catch (LWJGLException e) {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. }
  61. } else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
  62. player.setY(player.getY() + 4);
  63. } else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
  64. player.setX(player.getX() + 4);
  65. } else if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
  66. player.setX(player.getX() - 4);
  67. }
  68. }*/
  69. }
  70.  
  71. private long lastFrame;
  72.  
  73. private long getTime() {
  74. return (Sys.getTime() * 1000 / Sys.getTimerResolution());
  75. }
  76.  
  77. private int getDelta() {
  78. long currentTime = getTime();
  79. int delta = (int) (currentTime - lastFrame);
  80. lastFrame = getTime();
  81. return delta;
  82. }
  83.  
  84. private void setUpDisplay() {
  85. try {
  86. Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  87. Display.setTitle("RPG");
  88. Display.create();
  89. } catch (LWJGLException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93.  
  94. private void setUpOpenGL() {
  95. glMatrixMode(GL_PROJECTION);
  96. glLoadIdentity();
  97. glOrtho(0, 640, 480, 0, 1, -1);
  98. glMatrixMode(GL_MODELVIEW);
  99. }
  100.  
  101. private void setUpEntites() {
  102. player = new Player(32, 32, 32, 32);
  103. //enemy = new Enemy(50,50,32,32);
  104. }
  105.  
  106. private void setUpTimer() {
  107. lastFrame = getTime();
  108. }
  109.  
  110. private void render() {
  111. glClear(GL_COLOR_BUFFER_BIT);
  112. player.draw();
  113. //enemy.draw();
  114. }
  115.  
  116. private void logic(int delta) {
  117. player.update(delta);
  118.  
  119. //enemy.update(delta);
  120. /*if (player.intersects(enemy)) {
  121. changeToBattleScene(player,enemy);
  122. }*/
  123. }
  124.  
  125. private void changeToBattleScene(Entity player, Entity enemy) {
  126. inField = false;
  127.  
  128. double x = player.getX();
  129. double y = player.getY();
  130.  
  131. player.setX(WIDTH - WIDTH + 50);
  132. player.setY(HEIGHT - 50 - player.getHeight());
  133.  
  134. enemy.setX(WIDTH - 50 - enemy.getWidth());
  135. enemy.setY(HEIGHT - HEIGHT + 50);
  136.  
  137. glRectf(100,100,100,100);
  138.  
  139. //playerMovement = true;
  140. }
  141.  
  142. private static class Player extends AbstractMovableEntity {
  143.  
  144. public Player(float x, float y, float width, float height) {
  145. super(x, y, width, height);
  146. }
  147.  
  148. @Override
  149. public void draw() {
  150. glRectf(x, y, x + width, y + height);
  151. }
  152.  
  153. }
  154.  
  155. private static class Enemy extends AbstractMovableEntity {
  156.  
  157. public Enemy(float x, float y, float width, float height) {
  158. super(x, y, width, height);
  159. }
  160.  
  161. @Override
  162. public void draw() {
  163. glRectf(x, y, x + width, y + height);
  164. }
  165.  
  166. }
  167.  
  168. public static void main(String args[]) {
  169. new RPG();
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment