generated from sigonasr2/JavaProjectTemplate
	Implement mouse and keyboard handling
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
		
							parent
							
								
									519ce6d10a
								
							
						
					
					
						commit
						f17d0e93d4
					
				| @ -19,7 +19,7 @@ class Player{ | |||||||
| 	double x=200,y=200; | 	double x=200,y=200; | ||||||
| 
 | 
 | ||||||
| 	double animationFrame=0; | 	double animationFrame=0; | ||||||
| 	double animationSpd=2; | 	double animationSpd=2; //Animation Speed will be 2 frames per second | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| public class JavaProjectTemplate { | public class JavaProjectTemplate { | ||||||
| @ -38,26 +38,32 @@ public class JavaProjectTemplate { | |||||||
| 	public void updateGame(double fElapsedTime) { | 	public void updateGame(double fElapsedTime) { | ||||||
| 		//Put game update logic in here. | 		//Put game update logic in here. | ||||||
| 
 | 
 | ||||||
|  | 		//Player sprite will animate. | ||||||
| 		pl.animationFrame+=pl.animationSpd*fElapsedTime; | 		pl.animationFrame+=pl.animationSpd*fElapsedTime; | ||||||
| 
 | 
 | ||||||
| 		if (Key.isKeyHeld(KeyEvent.VK_A)) { | 		//Allow player movement using WASD keys. | ||||||
|  | 		if (Key.isHeld(KeyEvent.VK_A)) { | ||||||
| 			pl.x-=200*fElapsedTime; | 			pl.x-=200*fElapsedTime; | ||||||
| 		} | 		} | ||||||
| 		if (Key.isKeyHeld(KeyEvent.VK_D)) { | 		if (Key.isHeld(KeyEvent.VK_D)) { | ||||||
| 			pl.x+=200*fElapsedTime; | 			pl.x+=200*fElapsedTime; | ||||||
| 		} | 		} | ||||||
| 		if (Key.isKeyHeld(KeyEvent.VK_W)) { | 		if (Key.isHeld(KeyEvent.VK_W)) { | ||||||
| 			pl.y-=200*fElapsedTime; | 			pl.y-=200*fElapsedTime; | ||||||
| 		} | 		} | ||||||
| 		if (Key.isKeyHeld(KeyEvent.VK_S)) { | 		if (Key.isHeld(KeyEvent.VK_S)) { | ||||||
| 			pl.y+=200*fElapsedTime; | 			pl.y+=200*fElapsedTime; | ||||||
| 		} | 		} | ||||||
|  | 
 | ||||||
|  | 		if (Mouse.isPressed(2)) { //If middle click is pressed, reset the player position. | ||||||
|  | 			pl.x=pl.y=200; | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public void drawGame() { | 	public void drawGame() { | ||||||
| 		//Put rendering logic in here. | 		//Put rendering logic in here. | ||||||
| 
 | 
 | ||||||
| 		game.Clear(Color.BRIGHT_BLUE); | 		game.Clear(Color.BRIGHT_BLUE); //Clear the screen so all pixels from the previous frame are removed. | ||||||
| 
 | 
 | ||||||
| 		game.Draw(100,20,Color.BLACK); //That's not a dead pixel, it's a black one! | 		game.Draw(100,20,Color.BLACK); //That's not a dead pixel, it's a black one! | ||||||
| 		game.Draw_Line(10,10,35,35,Color.BLACK); //Line Drawing | 		game.Draw_Line(10,10,35,35,Color.BLACK); //Line Drawing | ||||||
| @ -70,10 +76,10 @@ public class JavaProjectTemplate { | |||||||
| 		game.Draw_Text(10,40,"Mouse X: "+Mouse.x+"  Mouse Y:"+Mouse.y,Font.PROFONT_12); //Draw Mouse coordinates in tiny font | 		game.Draw_Text(10,40,"Mouse X: "+Mouse.x+"  Mouse Y:"+Mouse.y,Font.PROFONT_12); //Draw Mouse coordinates in tiny font | ||||||
| 		game.Draw_Text_Ext(10,52,"Hello World 2!",Font.PROFONT_36,Color.MAGENTA); //Draw in larger font | 		game.Draw_Text_Ext(10,52,"Hello World 2!",Font.PROFONT_36,Color.MAGENTA); //Draw in larger font | ||||||
| 
 | 
 | ||||||
| 		//game.Fill_Triangle(160,160,190,190,50,250,new Color(255,0,0,150)); //Draw a colored triangle | 		game.Fill_Triangle(160,160,190,190,50,250,new Color(255,0,0,150)); //Draw a translucent colored triangle | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 		game.FillTexturedPolygon( //Define the uv tex coords of a triangle and a sprite and draw a texture onto it | 		game.FillTexturedPolygon( //Define the uv tex coords of a polygon and a sprite and draw a texture onto it | ||||||
| 			List.of( | 			List.of( | ||||||
| 				new Point<Double>(600d,400d), | 				new Point<Double>(600d,400d), | ||||||
| 				new Point<Double>(600d,550d), | 				new Point<Double>(600d,550d), | ||||||
|  | |||||||
| @ -5,6 +5,8 @@ import java.util.HashMap; | |||||||
| public class Key{ | public class Key{ | ||||||
| 
 | 
 | ||||||
| 	static HashMap<Integer,Boolean> KEYS = new HashMap<>(); | 	static HashMap<Integer,Boolean> KEYS = new HashMap<>(); | ||||||
|  | 	static HashMap<Integer,Boolean> KEYS_PRESS = new HashMap<>(); | ||||||
|  | 	static HashMap<Integer,Boolean> KEYS_RELEASE = new HashMap<>(); | ||||||
| 
 | 
 | ||||||
|     protected Key(int keycode) { |     protected Key(int keycode) { | ||||||
| 		this.keycode=keycode; | 		this.keycode=keycode; | ||||||
| @ -20,10 +22,18 @@ public class Key{ | |||||||
| 		//System.out.println(KEYS); | 		//System.out.println(KEYS); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static boolean isKeyHeld(int keycode) { |     public static boolean isHeld(int keycode) { | ||||||
|         return KEYS.getOrDefault(keycode,false); |         return KEYS.getOrDefault(keycode,false); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public static boolean isPressed(int keycode) { | ||||||
|  |         return KEYS_PRESS.getOrDefault(keycode,false); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static boolean isReleased(int keycode) { | ||||||
|  |         return KEYS_RELEASE.getOrDefault(keycode,false); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public boolean isKeyHeld() { |     public boolean isKeyHeld() { | ||||||
|         return KEYS.getOrDefault(keycode,false); |         return KEYS.getOrDefault(keycode,false); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -1,10 +1,27 @@ | |||||||
| package sig.engine; | package sig.engine; | ||||||
| 
 | 
 | ||||||
|  | import java.util.HashMap; | ||||||
|  | 
 | ||||||
| public class Mouse { | public class Mouse { | ||||||
|     public static int x; |     public static int x; | ||||||
|     public static int y; |     public static int y; | ||||||
|  |     public static HashMap<Integer,Boolean> clickMap=new HashMap<>(); | ||||||
|  |     public static HashMap<Integer,Boolean> pressMap=new HashMap<>(); | ||||||
|  |     public static HashMap<Integer,Boolean> releaseMap=new HashMap<>(); | ||||||
| 	public static Point<Integer> mousePosition = new Point<Integer>(0,0); | 	public static Point<Integer> mousePosition = new Point<Integer>(0,0); | ||||||
|     public static Point<Integer> GetPos(){ |     public static Point<Integer> GetPos(){ | ||||||
|         return new Point<Integer>(x,y); |         return new Point<Integer>(x,y); | ||||||
|     } |     } | ||||||
|  |     //0=Left click, 1=Right click, 2=Middle, 3=Button 4, 4=Button 5 | ||||||
|  |     public static boolean isHeld(Integer button){ | ||||||
|  |         return clickMap.getOrDefault(button,false); | ||||||
|  |     } | ||||||
|  |     //0=Left click, 1=Right click, 2=Middle, 3=Button 4, 4=Button 5 | ||||||
|  |     public static boolean isPressed(Integer button){ | ||||||
|  |         return pressMap.getOrDefault(button,false); | ||||||
|  |     } | ||||||
|  |     //0=Left click, 1=Right click, 2=Middle, 3=Button 4, 4=Button 5 | ||||||
|  |     public static boolean isReleased(Integer button){ | ||||||
|  |         return releaseMap.getOrDefault(button,false); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -49,7 +49,6 @@ public class Panel extends JPanel implements Runnable,KeyListener { | |||||||
| 	public double nanaX = 0; | 	public double nanaX = 0; | ||||||
| 	public double nanaY = 0; | 	public double nanaY = 0; | ||||||
| 	public int button = 0; | 	public int button = 0; | ||||||
| 	public HashMap<Integer,Boolean> MOUSE = new HashMap<>(); |  | ||||||
| 	private MouseScrollValue scrollWheel=null; | 	private MouseScrollValue scrollWheel=null; | ||||||
| 	public static final int UPDATE_LOOP_FRAMERATE = 244; | 	public static final int UPDATE_LOOP_FRAMERATE = 244; | ||||||
| 	public static final long UPDATE_LOOP_NANOTIME = (long)((1d/UPDATE_LOOP_FRAMERATE)*1000000000l); | 	public static final long UPDATE_LOOP_NANOTIME = (long)((1d/UPDATE_LOOP_FRAMERATE)*1000000000l); | ||||||
| @ -100,6 +99,10 @@ public class Panel extends JPanel implements Runnable,KeyListener { | |||||||
| 			lastGameTime = System.nanoTime(); | 			lastGameTime = System.nanoTime(); | ||||||
| 			while (dt >= UPDATE_LOOP_NANOTIME) { | 			while (dt >= UPDATE_LOOP_NANOTIME) { | ||||||
| 				gameInstance.updateGame(UPDATE_LOOP_NANOTIME/1000000000d); | 				gameInstance.updateGame(UPDATE_LOOP_NANOTIME/1000000000d); | ||||||
|  | 				Mouse.pressMap.clear(); | ||||||
|  | 				Mouse.releaseMap.clear(); | ||||||
|  | 				Key.KEYS_PRESS.clear(); | ||||||
|  | 				Key.KEYS_RELEASE.clear(); | ||||||
| 				dt -= UPDATE_LOOP_NANOTIME; | 				dt -= UPDATE_LOOP_NANOTIME; | ||||||
| 				TIME += UPDATE_LOOP_NANOTIME; | 				TIME += UPDATE_LOOP_NANOTIME; | ||||||
| 			} | 			} | ||||||
| @ -135,12 +138,14 @@ public class Panel extends JPanel implements Runnable,KeyListener { | |||||||
| 		 | 		 | ||||||
| 			@Override | 			@Override | ||||||
| 			public void mousePressed(MouseEvent e) { | 			public void mousePressed(MouseEvent e) { | ||||||
| 				MOUSE.put(e.getButton(),true); | 				Mouse.clickMap.put(e.getButton(),true); | ||||||
|  | 				Mouse.pressMap.put(e.getButton(),true); | ||||||
| 			} | 			} | ||||||
| 		 | 		 | ||||||
| 			@Override | 			@Override | ||||||
| 			public void mouseReleased(MouseEvent e) { | 			public void mouseReleased(MouseEvent e) { | ||||||
| 				MOUSE.put(e.getButton(),false); | 				Mouse.clickMap.put(e.getButton(),false); | ||||||
|  | 				Mouse.releaseMap.put(e.getButton(),true); | ||||||
| 			} | 			} | ||||||
| 		 | 		 | ||||||
| 			@Override | 			@Override | ||||||
| @ -379,6 +384,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void Draw(int index, int col) { | 	void Draw(int index, int col) { | ||||||
|  | 		if (((col>>>24)&0xff)==0) return; | ||||||
| 		if (((col>>>24)&0xff)!=255) { | 		if (((col>>>24)&0xff)!=255) { | ||||||
| 			pixel[index]=ColorLerpNoAlpha(new Color(pixel[index]),new Color(col),((col>>>24)&0xff)/255f).getColor(); | 			pixel[index]=ColorLerpNoAlpha(new Color(pixel[index]),new Color(col),((col>>>24)&0xff)/255f).getColor(); | ||||||
| 		} else { | 		} else { | ||||||
| @ -441,8 +447,9 @@ public class Panel extends JPanel implements Runnable,KeyListener { | |||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public void keyPressed(KeyEvent e) { | 	public void keyPressed(KeyEvent e) { | ||||||
| 		if (!Key.isKeyHeld(e.getKeyCode())) { | 		if (!Key.isHeld(e.getKeyCode())) { | ||||||
| 			Key.setKeyHeld(e.getKeyCode(), true); | 			Key.setKeyHeld(e.getKeyCode(), true); | ||||||
|  | 			Key.KEYS_PRESS.put(e.getKeyCode(),true); | ||||||
| 		} | 		} | ||||||
| 		//System.out.println("Key List: "+KEYS); | 		//System.out.println("Key List: "+KEYS); | ||||||
| 	} | 	} | ||||||
| @ -450,6 +457,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { | |||||||
| 	@Override | 	@Override | ||||||
| 	public void keyReleased(KeyEvent e) { | 	public void keyReleased(KeyEvent e) { | ||||||
| 		Key.setKeyHeld(e.getKeyCode(), false); | 		Key.setKeyHeld(e.getKeyCode(), false); | ||||||
|  | 		Key.KEYS_RELEASE.put(e.getKeyCode(),true); | ||||||
| 		//System.out.println("Key List: "+KEYS); | 		//System.out.println("Key List: "+KEYS); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user