Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import org.lwjgl.opengl.Display;
  5. import static data.Screen.*;
  6. import static data.Clock.*;
  7. import static data.Player.*;
  8.  
  9. public class Main{
  10.  
  11.  
  12.  
  13.  
  14. public static void main(String[] args) {
  15.  
  16. ScreenInit();
  17. System.out.println("ScreenInit ready!");
  18. Screen();
  19.  
  20.  
  21. }
  22.  
  23.  
  24. public static void Screen(){
  25.  
  26. new Player("test", 10, 10, 64, 64);
  27.  
  28. while(!Display.isCloseRequested()){
  29.  
  30. Clock.update();
  31. Player.handlePlayer();
  32.  
  33. Display.update();
  34. Display.sync(60);
  35. }
  36. Display.destroy();
  37. System.exit(0);
  38.  
  39. }
  40. }
  41.  
  42. -----------------------------------------------------
  43.  
  44. package data;
  45.  
  46. import org.lwjgl.LWJGLException;
  47. import org.lwjgl.opengl.Display;
  48. import org.lwjgl.opengl.DisplayMode;
  49. import org.newdawn.slick.opengl.Texture;
  50. import org.newdawn.slick.opengl.TextureLoader;
  51. import org.newdawn.slick.util.ResourceLoader;
  52.  
  53. import static data.Main.*;
  54. import static org.lwjgl.opengl.GL11.*;
  55.  
  56. import java.io.IOException;
  57. import java.io.InputStream;
  58.  
  59. public class Screen {
  60. public static final int WIDTH = 1080, HEIGHT = 720;
  61.  
  62. public static void ScreenInit(){
  63. try {
  64. SystemInit();
  65. System.out.println("SysInit ready!");
  66. Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  67. Display.setTitle("Test");
  68. Display.create();
  69. } catch (LWJGLException e) {
  70. e.printStackTrace();
  71. Display.destroy();
  72. System.exit(1);
  73. }
  74. glMatrixMode(GL_PROJECTION);
  75. glLoadIdentity();
  76. glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
  77. glMatrixMode(GL_MODELVIEW);
  78. glEnable(GL_TEXTURE_2D);
  79. glEnable(GL_BLEND);
  80. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  81. }
  82. public static void DrawQuadTex(Texture tex, float x, float y, float width, float height) {
  83. tex.bind();
  84. glTranslatef(x, y, 0);
  85. glBegin(GL_QUADS);
  86. glTexCoord2f(0, 0);
  87. glVertex2f(0, 0);
  88. glTexCoord2f(1, 0);
  89. glVertex2f(width, 0);
  90. glTexCoord2f(1, 1);
  91. glVertex2f(width, height);
  92. glTexCoord2f(0, 1);
  93. glVertex2f(0, height);
  94. glEnd();
  95. glLoadIdentity();
  96.  
  97. }
  98. public static Texture loadTexture(String path, String fileType) {
  99. Texture tex = null;
  100. InputStream in = ResourceLoader.getResourceAsStream(path);
  101. try {
  102. tex = TextureLoader.getTexture(fileType, in);
  103. } catch (IOException e) {
  104.  
  105. e.printStackTrace();
  106. }
  107. return tex;
  108. }
  109.  
  110. public static Texture QuickLoad(String name) {
  111. Texture tex = null;
  112. tex = loadTexture("resources/textures/" + name
  113. + ".png", "PNG");
  114. return tex;
  115.  
  116. }
  117. }
  118. ----------------------------------------------------------------------
  119. package data;
  120.  
  121. import org.lwjgl.Sys;
  122.  
  123. public class Clock {
  124. private static boolean paused = false;
  125. public static long lastframe, totaltime;
  126. public static float d = 0, multiplier = 1;
  127.  
  128. public static long getTime() {
  129. return Sys.getTime() * 1000 / Sys.getTimerResolution();
  130.  
  131. }
  132.  
  133. public static float getDelta() {
  134. long currenttime = getTime();
  135. int delta = (int) (currenttime - lastframe);
  136. lastframe = getTime();
  137. if(delta * 0.001f > 0.05f)
  138. return 0.05f;
  139. return delta * 0.001f;
  140. }
  141.  
  142. public static float Delta() {
  143. if (paused)
  144. return 0;
  145. else
  146.  
  147. return d * multiplier;
  148. }
  149.  
  150. public static float Totaltime() {
  151. return totaltime;
  152.  
  153. }
  154.  
  155. public static float Multiplier() {
  156. return multiplier;
  157. }
  158.  
  159. public static void update() {
  160.  
  161. d = getDelta();
  162. totaltime += d;
  163. }
  164.  
  165. public static void changemultiplier(float change) {
  166. if (multiplier + change < -1 && multiplier + change > 7) {
  167.  
  168. } else {
  169. multiplier += change;
  170. }
  171. }
  172.  
  173. public static void pause() {
  174. if (paused)
  175. paused = false;
  176. else
  177.  
  178. paused = true;
  179. }
  180.  
  181. }
  182.  
  183. -----------------------------------------------------------------
  184. package data;
  185.  
  186. import org.lwjgl.input.Keyboard;
  187.  
  188. import static data.Screen.*;
  189. import static data.Clock.*;
  190.  
  191. import java.util.ArrayList;
  192. import java.util.List;
  193.  
  194. public class Player {
  195. private static String tex;
  196. private static float x;
  197. private static float y;
  198. private static float width;
  199. private static float height;
  200. private static ArrayList<Player> Players;
  201.  
  202.  
  203.  
  204. public Player (String tex, float x, float y, float width, float height){
  205.  
  206. Player.tex = tex;
  207. Player.x = x;
  208. Player.y = y;
  209. Player.width = width;
  210. Player.height = height;
  211. this.setPlayers(new ArrayList<Player>());
  212. Players.add(this);
  213.  
  214.  
  215.  
  216. }
  217.  
  218.  
  219. public static int Direction = 2;
  220. public static boolean Moving = false;
  221. public static boolean start = false;
  222.  
  223. public static void handlePlayer(){
  224. for(Player p: Players){
  225. p.Update();
  226. p.draw();
  227. }
  228. }
  229.  
  230. public static void Walk(){
  231. if(start == false){
  232. start = true;
  233. Moving = false;
  234. }
  235. if(Direction == 0 && Moving == true ){
  236. System.out.println("Walking North");
  237. getPlayers().get(0).setY(y -= 30 * Delta());
  238. }
  239. if(Direction == 1 && Moving == true ){
  240. System.out.println("Walking East");
  241. getPlayers().get(0).setX(x += 30 * Delta());
  242.  
  243. }
  244. if(Direction == 2 && Moving == true ){
  245. System.out.println("Walking South");
  246. getPlayers().get(0).setY(y += 30 * Delta());
  247. }
  248. if(Direction == 3 && Moving == true ){
  249. System.out.println("Walking West");
  250. getPlayers().get(0).setX(x -= 30 * Delta());
  251. }
  252. if(Direction == 0 && Moving == false ){
  253. System.out.println("Standing North");
  254. }
  255. if(Direction == 1 && Moving == false ){
  256. System.out.println("Standing East");
  257. }
  258. if(Direction == 2 && Moving == false ){
  259. System.out.println("Standing South");
  260. }
  261. if(Direction == 3 && Moving == false ){
  262. System.out.println("Standing West");
  263. }
  264. }
  265. public static void Inputs(){
  266. if(Keyboard.isKeyDown(Keyboard.KEY_W)){
  267. Direction = 0;
  268. Moving = true;
  269. }
  270. if(Keyboard.isKeyDown(Keyboard.KEY_D)){
  271. Direction = 1;
  272. Moving = true;
  273. }
  274. if(Keyboard.isKeyDown(Keyboard.KEY_S)){
  275. Direction = 2;
  276. Moving = true;
  277. }
  278. if(Keyboard.isKeyDown(Keyboard.KEY_A)){
  279. Direction = 3;
  280. Moving = true;
  281. }
  282. if(!Keyboard.isKeyDown(Keyboard.KEY_W) && !Keyboard.isKeyDown(Keyboard.KEY_D) && !Keyboard.isKeyDown(Keyboard.KEY_S) && !Keyboard.isKeyDown(Keyboard.KEY_A)){
  283. Moving = false;
  284. }
  285. }
  286.  
  287. public static void draw(){
  288. DrawQuadTex(QuickLoad(tex), x, y, width, height);
  289. }
  290.  
  291.  
  292.  
  293. public static void Update(){
  294.  
  295. Inputs();
  296. Walk();
  297. }
  298. public String getTex() {
  299. return tex;
  300. }
  301. public void setTex(String tex) {
  302. Player.tex = tex;
  303. }
  304. public float getX() {
  305. return x;
  306. }
  307. public void setX(float x) {
  308. Player.x = x;
  309. }
  310. public float getY() {
  311. return y;
  312. }
  313. public void setY(float y) {
  314. Player.y = y;
  315. }
  316. public float getWidth() {
  317. return width;
  318. }
  319. public void setWidth(float width) {
  320. Player.width = width;
  321. }
  322. public float getHeight() {
  323. return height;
  324. }
  325. public void setHeight(float height) {
  326. Player.height = height;
  327. }
  328. public static ArrayList<Player> getPlayers() {
  329. return Players;
  330. }
  331. public void setPlayers(ArrayList<Player> players) {
  332. Players = players;
  333. }
  334.  
  335.  
  336.  
  337.  
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement