diff --git a/src/sig/JavaProjectTemplate.java b/src/sig/JavaProjectTemplate.java index 885aba8..6525e9e 100644 --- a/src/sig/JavaProjectTemplate.java +++ b/src/sig/JavaProjectTemplate.java @@ -19,7 +19,7 @@ class Player{ double x=200,y=200; double animationFrame=0; - double animationSpd=2; + double animationSpd=2; //Animation Speed will be 2 frames per second } public class JavaProjectTemplate { @@ -38,26 +38,32 @@ public class JavaProjectTemplate { public void updateGame(double fElapsedTime) { //Put game update logic in here. + //Player sprite will animate. 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; } - if (Key.isKeyHeld(KeyEvent.VK_D)) { + if (Key.isHeld(KeyEvent.VK_D)) { pl.x+=200*fElapsedTime; } - if (Key.isKeyHeld(KeyEvent.VK_W)) { + if (Key.isHeld(KeyEvent.VK_W)) { pl.y-=200*fElapsedTime; } - if (Key.isKeyHeld(KeyEvent.VK_S)) { + if (Key.isHeld(KeyEvent.VK_S)) { 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() { //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_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_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( new Point(600d,400d), new Point(600d,550d), diff --git a/src/sig/engine/Key.java b/src/sig/engine/Key.java index 0851781..6fe0237 100644 --- a/src/sig/engine/Key.java +++ b/src/sig/engine/Key.java @@ -5,6 +5,8 @@ import java.util.HashMap; public class Key{ static HashMap KEYS = new HashMap<>(); + static HashMap KEYS_PRESS = new HashMap<>(); + static HashMap KEYS_RELEASE = new HashMap<>(); protected Key(int keycode) { this.keycode=keycode; @@ -20,10 +22,18 @@ public class Key{ //System.out.println(KEYS); } - public static boolean isKeyHeld(int keycode) { + public static boolean isHeld(int keycode) { 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() { return KEYS.getOrDefault(keycode,false); } diff --git a/src/sig/engine/Mouse.java b/src/sig/engine/Mouse.java index 21d0b0b..baaf780 100644 --- a/src/sig/engine/Mouse.java +++ b/src/sig/engine/Mouse.java @@ -1,10 +1,27 @@ package sig.engine; +import java.util.HashMap; + public class Mouse { public static int x; public static int y; + public static HashMap clickMap=new HashMap<>(); + public static HashMap pressMap=new HashMap<>(); + public static HashMap releaseMap=new HashMap<>(); public static Point mousePosition = new Point(0,0); public static Point GetPos(){ return new Point(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); + } } diff --git a/src/sig/engine/Panel.java b/src/sig/engine/Panel.java index 13393ac..fee3dda 100644 --- a/src/sig/engine/Panel.java +++ b/src/sig/engine/Panel.java @@ -49,7 +49,6 @@ public class Panel extends JPanel implements Runnable,KeyListener { public double nanaX = 0; public double nanaY = 0; public int button = 0; - public HashMap MOUSE = new HashMap<>(); private MouseScrollValue scrollWheel=null; public static final int UPDATE_LOOP_FRAMERATE = 244; 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(); while (dt >= UPDATE_LOOP_NANOTIME) { gameInstance.updateGame(UPDATE_LOOP_NANOTIME/1000000000d); + Mouse.pressMap.clear(); + Mouse.releaseMap.clear(); + Key.KEYS_PRESS.clear(); + Key.KEYS_RELEASE.clear(); dt -= UPDATE_LOOP_NANOTIME; TIME += UPDATE_LOOP_NANOTIME; } @@ -135,12 +138,14 @@ public class Panel extends JPanel implements Runnable,KeyListener { @Override public void mousePressed(MouseEvent e) { - MOUSE.put(e.getButton(),true); + Mouse.clickMap.put(e.getButton(),true); + Mouse.pressMap.put(e.getButton(),true); } @Override public void mouseReleased(MouseEvent e) { - MOUSE.put(e.getButton(),false); + Mouse.clickMap.put(e.getButton(),false); + Mouse.releaseMap.put(e.getButton(),true); } @Override @@ -379,6 +384,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { } void Draw(int index, int col) { + if (((col>>>24)&0xff)==0) return; if (((col>>>24)&0xff)!=255) { pixel[index]=ColorLerpNoAlpha(new Color(pixel[index]),new Color(col),((col>>>24)&0xff)/255f).getColor(); } else { @@ -441,8 +447,9 @@ public class Panel extends JPanel implements Runnable,KeyListener { @Override public void keyPressed(KeyEvent e) { - if (!Key.isKeyHeld(e.getKeyCode())) { + if (!Key.isHeld(e.getKeyCode())) { Key.setKeyHeld(e.getKeyCode(), true); + Key.KEYS_PRESS.put(e.getKeyCode(),true); } //System.out.println("Key List: "+KEYS); } @@ -450,6 +457,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { @Override public void keyReleased(KeyEvent e) { Key.setKeyHeld(e.getKeyCode(), false); + Key.KEYS_RELEASE.put(e.getKeyCode(),true); //System.out.println("Key List: "+KEYS); }