Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. //Main.java
  2. package de.markus.rpg;
  3.  
  4. import java.awt.BorderLayout;
  5. import java.awt.Canvas;
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8. import java.awt.Graphics;
  9. import java.awt.Insets;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.image.BufferStrategy;
  12. import javax.swing.JFrame;
  13. import javax.swing.JOptionPane;
  14.  
  15. import de.markus.rpg.multiplayer.GameClient;
  16. import de.markus.rpg.multiplayer.GameServer;
  17.  
  18. public class Main extends Canvas implements Runnable{
  19.  
  20. static int width = 800;
  21. static int height = 600;
  22.  
  23. static int transx;
  24. static int transy;
  25.  
  26. public int tickCounter = 0;
  27. public boolean running = false;
  28. public static boolean fullhd = false;
  29.  
  30. private JFrame frame;
  31.  
  32. public static int gamestate = 0;
  33.  
  34. private Menu menu;
  35. private Background bg;
  36. private Options options;
  37. private Pause pause;
  38. private GameClient client;
  39. private GameServer server;
  40.  
  41. public Main() {
  42. //Player player = new Player(960, 540, 1920, 1080);
  43. // setMinimumSize(new Dimension(width*3, height*3));
  44. // setMaximumSize(new Dimension(width*3, height*3));
  45. // setPreferredSize(new Dimension(width*3, height*3));
  46.  
  47. frame = new JFrame();
  48. frame.setDefaultCloseOperation(3);
  49. frame.setLayout(new BorderLayout());
  50.  
  51. frame.add(this, BorderLayout.CENTER);
  52. frame.setLocationRelativeTo(null);
  53. frame.setResizable(false);
  54. frame.setVisible(true);
  55.  
  56.  
  57. KeyBoard kb = new KeyBoard();
  58. addKeyListener(kb);
  59. addMouseMotionListener(kb);
  60. addMouseListener(kb);
  61.  
  62.  
  63.  
  64. }
  65.  
  66. public void init() {
  67. menu = new Menu();
  68. bg = new Background();
  69. options = new Options();
  70. pause = new Pause();
  71. System.out.println("3");
  72. client.sendData("ping".getBytes());
  73. }
  74.  
  75.  
  76.  
  77.  
  78. public void run() {
  79. long lastTime = System.nanoTime();
  80. double nsPerTick = 1000000000D/60D;
  81.  
  82. int frames = 0;
  83. int ticks = 0;
  84.  
  85. long lastTimer = System.currentTimeMillis();
  86. long lastFrame = System.currentTimeMillis();
  87. double delta = 0;
  88.  
  89. System.out.println("2");
  90. init();
  91.  
  92. while(running) {
  93. long now = System.nanoTime();
  94. delta += (now - lastTime)/nsPerTick;
  95. lastTime = now;
  96. boolean shouldRender = true;
  97.  
  98. long thisFrame = System.currentTimeMillis();
  99. float tslf = ((float)(thisFrame-lastFrame))/1000f;
  100. lastFrame = thisFrame;
  101.  
  102. if(fullhd) {
  103. frame.setSize(width, height);
  104. frame.setLocation(0, 0);
  105. }else {
  106. frame.setSize(width, height);
  107. frame.setLocationRelativeTo(null);
  108. }
  109.  
  110. while(delta >= 1) {
  111. ticks++;
  112. tick();
  113. delta -= 1;
  114. shouldRender = true;
  115. }
  116. try {
  117. Thread.sleep(2);
  118. } catch (InterruptedException e) {
  119. e.printStackTrace();
  120. }
  121. if(shouldRender) {
  122. frames++;
  123. render();
  124. update(tslf);
  125. }
  126.  
  127.  
  128. if(System.currentTimeMillis() - lastTimer > 1000) {
  129. lastTimer += 1000;
  130. frame.setTitle(frames + " frames, " + ticks + " ticks");
  131. frames = 0;
  132. ticks = 0;
  133. }
  134.  
  135. }
  136. }
  137. public void tick() {
  138. tickCounter++;
  139. }
  140. public void render() {
  141. BufferStrategy bs = getBufferStrategy();
  142. if(bs != null) {
  143. Graphics g = bs.getDrawGraphics();
  144. draw(g);
  145. g.dispose();
  146. bs.show();
  147. }else {
  148. createBufferStrategy(3);
  149. return;
  150. }
  151. }
  152.  
  153. public void draw(Graphics g) {
  154. g.setColor(Color.BLACK);
  155. g.fillRect(0, 0, width, height);
  156. switch(gamestate) {
  157. case 0:
  158. menu.draw(g);
  159. break;
  160.  
  161. case 1:
  162. bg.draw(g);
  163. break;
  164.  
  165. case 2:
  166. options.draw(g);
  167. break;
  168.  
  169. case 3:
  170. pause.draw(g);
  171. break;
  172.  
  173. default:
  174. break;
  175. }
  176. }
  177.  
  178. public void update(float tslf) {
  179. switch(gamestate) {
  180. case 0:
  181. menu.update(tslf);
  182. break;
  183.  
  184. case 1:
  185. bg.update(tslf);
  186. if(KeyBoard.iskeyDown(KeyEvent.VK_ESCAPE)) {
  187. gamestate = 3;
  188. }
  189. break;
  190.  
  191. case 2:
  192. options.update(tslf);
  193. if(KeyBoard.iskeyDown(KeyEvent.VK_ESCAPE)) {
  194. gamestate = 0;
  195. }
  196. break;
  197.  
  198. case 3:
  199. break;
  200.  
  201. default:
  202. break;
  203. }
  204. }
  205.  
  206. public synchronized void start() {
  207. running = true;
  208. new Thread(this).start();
  209.  
  210. System.out.println("1");
  211.  
  212. client = new GameClient(this, "localhost");
  213. client.start();
  214.  
  215. if(JOptionPane.showConfirmDialog(this, "Do you want to run a server?") == 0) {
  216. System.out.println("4");
  217. server = new GameServer(this);
  218. server.start();
  219. }
  220.  
  221. }
  222.  
  223. public synchronized void stop() {
  224. running = false;
  225. }
  226.  
  227. public static void main(String[] args) {
  228. new Main().start();
  229. }
  230.  
  231. public static void setWidth(int width) {
  232. Main.width = width;
  233. }
  234. public static void setHeight(int height) {
  235. Main.height = height;
  236. }
  237. public static void setFullHD(boolean fullhd) {
  238. Main.fullhd = fullhd;
  239. }
  240.  
  241.  
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement