Update all key press checks to use events. Clean up warnings

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
Nic0Nic0Nii 3 years ago
parent 83f35ac2f8
commit 6db59a796e
  1. BIN
      RabiClone_0.0a.zip
  2. BIN
      bin/RabiClone.jar
  3. 2
      manifest
  4. 6
      src/sig/RabiClone.java
  5. 5
      src/sig/engine/Action.java
  6. 17
      src/sig/engine/KeyBind.java
  7. 8
      src/sig/engine/Object.java
  8. 6
      src/sig/engine/Panel.java
  9. 23
      src/sig/objects/EditorRenderer.java
  10. 151
      src/sig/objects/Player.java

Binary file not shown.

Binary file not shown.

@ -3,3 +3,5 @@ Main-Class: sig.RabiClone
Main-Class: sig.RabiClone Main-Class: sig.RabiClone
Main-Class: sig.RabiClone Main-Class: sig.RabiClone
Main-Class: sig.RabiClone Main-Class: sig.RabiClone
Main-Class: sig.RabiClone
Main-Class: sig.RabiClone

@ -82,10 +82,10 @@ public class RabiClone{
if (CONTROLLERS[i].poll()) { if (CONTROLLERS[i].poll()) {
Component[] components = CONTROLLERS[i].getComponents(); Component[] components = CONTROLLERS[i].getComponents();
for (int j=0;j<components.length;j++) { for (int j=0;j<components.length;j++) {
Component c = components[j]; //Component c = components[j];
System.out.println(c.getName()+","+c.getIdentifier()+": "+c.getPollData()); //System.out.println(c.getName()+","+c.getIdentifier()+": "+c.getPollData());
} }
System.out.println("--------"); //System.out.println("--------");
} }
/*EventQueue queue = controller_list[i].getEventQueue(); /*EventQueue queue = controller_list[i].getEventQueue();

@ -13,7 +13,10 @@ public enum Action {
FALL(new Key(KeyEvent.VK_DOWN),new Key(KeyEvent.VK_S)), FALL(new Key(KeyEvent.VK_DOWN),new Key(KeyEvent.VK_S)),
SLIDE(new Key(KeyEvent.VK_CONTROL)), SLIDE(new Key(KeyEvent.VK_CONTROL)),
LEVEL_EDITOR(new Key(KeyEvent.VK_F2)), LEVEL_EDITOR(new Key(KeyEvent.VK_F2)),
PLAY_GAME(new Key(KeyEvent.VK_F1)),; PLAY_GAME(new Key(KeyEvent.VK_F1)),
EDITOR_SET_VIEW(new Key(KeyEvent.VK_F3)),
EDITOR_SET_TYPE(new Key(KeyEvent.VK_F4)),
EDITOR_SET_BACKGROUND(new Key(KeyEvent.VK_F5)),;
float val; float val;
Key controllingKey; Key controllingKey;

@ -5,6 +5,7 @@ import java.util.List;
import net.java.games.input.Component; import net.java.games.input.Component;
import net.java.games.input.Component.Identifier; import net.java.games.input.Component.Identifier;
import sig.RabiClone;
public class KeyBind { public class KeyBind {
public static HashMap<Action,List<Component>> KEYBINDS = new HashMap<>(); public static HashMap<Action,List<Component>> KEYBINDS = new HashMap<>();
@ -22,7 +23,7 @@ public class KeyBind {
this.val=val; this.val=val;
} }
public static boolean IsKeyHeld(Action action) { public static boolean isKeyHeld(Action action) {
return KEYS.getOrDefault(action,false); return KEYS.getOrDefault(action,false);
} }
@ -77,19 +78,25 @@ public class KeyBind {
} }
private static void actionEventCheck(Action a, boolean held) { private static void actionEventCheck(Action a, boolean held) {
if (KeyBind.IsKeyHeld(a)&&!held) { if (KeyBind.isKeyHeld(a)&&!held) {
emitReleaseEvent(a); emitReleaseEvent(a);
} else } else
if (!KeyBind.IsKeyHeld(a)&&held) { if (!KeyBind.isKeyHeld(a)&&held) {
emitPressEvent(a); emitPressEvent(a);
} }
KeyBind.setKeyPressed(a, held); KeyBind.setKeyPressed(a, held);
} }
private static void emitReleaseEvent(Action a) { private static void emitReleaseEvent(Action a) {
//System.out.println("Release for "+a); System.out.println("Release for "+a);
for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyReleased(a);
}
} }
private static void emitPressEvent(Action a) { private static void emitPressEvent(Action a) {
//System.out.println("Press for "+a); System.out.println("Press for "+a);
for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyPressed(a);
}
} }
} }

@ -89,14 +89,14 @@ public abstract class Object implements GameEntity{
DrawLoop.Draw_Sprite_Partial_Ext(x,y,xOffset,yOffset,w,h,sprite,frame_index,alpha,col,transform); DrawLoop.Draw_Sprite_Partial_Ext(x,y,xOffset,yOffset,w,h,sprite,frame_index,alpha,col,transform);
} }
protected boolean KeyHeld(int key) { protected boolean KeyHeld(Action a) {
return Key.isKeyHeld(key); return KeyBind.isKeyHeld(a);
} }
protected void KeyPressed(int key) { protected void KeyPressed(Action a) {
} }
protected void KeyReleased(int key) { protected void KeyReleased(Action a) {
} }
protected boolean MouseHeld(int mb) { protected boolean MouseHeld(int mb) {

@ -403,9 +403,6 @@ public class Panel extends JPanel implements Runnable,KeyListener {
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (!Key.isKeyHeld(e.getKeyCode())) { if (!Key.isKeyHeld(e.getKeyCode())) {
Key.setKeyHeld(e.getKeyCode(), true); Key.setKeyHeld(e.getKeyCode(), true);
for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyPressed(e.getKeyCode());
}
} }
//System.out.println("Key List: "+KEYS); //System.out.println("Key List: "+KEYS);
} }
@ -413,9 +410,6 @@ 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);
for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyReleased(e.getKeyCode());
}
//System.out.println("Key List: "+KEYS); //System.out.println("Key List: "+KEYS);
} }
} }

@ -1,10 +1,10 @@
package sig.objects; package sig.objects;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.IOException; import java.io.IOException;
import sig.RabiClone; import sig.RabiClone;
import sig.engine.Action;
import sig.engine.Alpha; import sig.engine.Alpha;
import sig.engine.Font; import sig.engine.Font;
import sig.engine.MouseScrollValue; import sig.engine.MouseScrollValue;
@ -49,10 +49,10 @@ public class EditorRenderer extends LevelRenderer{
@Override @Override
public void update(double updateMult) { public void update(double updateMult) {
int right = KeyHeld(KeyEvent.VK_RIGHT)||KeyHeld(KeyEvent.VK_D)?1:0; int right = KeyHeld(Action.MOVE_RIGHT)?1:0;
int left = KeyHeld(KeyEvent.VK_LEFT)||KeyHeld(KeyEvent.VK_A)?1:0; int left = KeyHeld(Action.MOVE_LEFT)?1:0;
int up = KeyHeld(KeyEvent.VK_UP)||KeyHeld(KeyEvent.VK_W)?1:0; int up = KeyHeld(Action.JUMP)?1:0;
int down = KeyHeld(KeyEvent.VK_DOWN)||KeyHeld(KeyEvent.VK_S)?1:0; int down = KeyHeld(Action.FALL)?1:0;
if (right-left!=0) { if (right-left!=0) {
setX(Math.max(0,getX()+(right-left)*CAMERA_SPD*updateMult)); setX(Math.max(0,getX()+(right-left)*CAMERA_SPD*updateMult));
} }
@ -68,7 +68,7 @@ public class EditorRenderer extends LevelRenderer{
int tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT; int tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT;
RabiClone.CURRENT_MAP.ModifyTile(tileX, tileY, selectedTile); RabiClone.CURRENT_MAP.ModifyTile(tileX, tileY, selectedTile);
} }
if(KeyHeld(KeyEvent.VK_CONTROL)&&KeyHeld(KeyEvent.VK_S)){ if(KeyHeld(Action.SLIDE)||KeyHeld(Action.FALL)){
AddMessage("Saving map..."); AddMessage("Saving map...");
try { try {
Map.SaveMap(RabiClone.CURRENT_MAP); Map.SaveMap(RabiClone.CURRENT_MAP);
@ -177,17 +177,18 @@ public class EditorRenderer extends LevelRenderer{
} }
@Override @Override
protected void KeyPressed(int key) { @SuppressWarnings("incomplete-switch")
protected void KeyPressed(Action a) {
int tileX = (int)(RabiClone.MOUSE_POS.getX()+getX())/Tile.TILE_WIDTH; int tileX = (int)(RabiClone.MOUSE_POS.getX()+getX())/Tile.TILE_WIDTH;
int tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT; int tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT;
switch (key) { switch (a) {
case KeyEvent.VK_F3:{ case EDITOR_SET_VIEW:{
RabiClone.CURRENT_MAP.setView(tileX,tileY,View.values()[(RabiClone.CURRENT_MAP.getView(tileX, tileY).ordinal()+1)%View.values().length]); RabiClone.CURRENT_MAP.setView(tileX,tileY,View.values()[(RabiClone.CURRENT_MAP.getView(tileX, tileY).ordinal()+1)%View.values().length]);
}break; }break;
case KeyEvent.VK_F4:{ case EDITOR_SET_TYPE:{
RabiClone.CURRENT_MAP.setType(tileX,tileY,Type.values()[(RabiClone.CURRENT_MAP.getType(tileX, tileY).ordinal()+1)%Type.values().length]); RabiClone.CURRENT_MAP.setType(tileX,tileY,Type.values()[(RabiClone.CURRENT_MAP.getType(tileX, tileY).ordinal()+1)%Type.values().length]);
}break; }break;
case KeyEvent.VK_F5:{ case EDITOR_SET_BACKGROUND:{
RabiClone.CURRENT_MAP.setBackground(tileX,tileY,Background.values()[(RabiClone.CURRENT_MAP.getBackground(tileX, tileY).ordinal()+1)%Background.values().length]); RabiClone.CURRENT_MAP.setBackground(tileX,tileY,Background.values()[(RabiClone.CURRENT_MAP.getBackground(tileX, tileY).ordinal()+1)%Background.values().length]);
}break; }break;
} }

@ -1,6 +1,7 @@
package sig.objects; package sig.objects;
import sig.RabiClone; import sig.RabiClone;
import sig.engine.Action;
import sig.engine.AnimatedObject; import sig.engine.AnimatedObject;
import sig.engine.Panel; import sig.engine.Panel;
import sig.engine.Sprite; import sig.engine.Sprite;
@ -10,8 +11,6 @@ import sig.map.Tile;
import sig.map.View; import sig.map.View;
import sig.objects.actor.State; import sig.objects.actor.State;
import java.awt.event.KeyEvent;
public class Player extends AnimatedObject{ public class Player extends AnimatedObject{
final double GRAVITY = 1300; final double GRAVITY = 1300;
final double NORMAL_FRICTION = 6400; final double NORMAL_FRICTION = 6400;
@ -69,74 +68,62 @@ public class Player extends AnimatedObject{
super.update(updateMult); super.update(updateMult);
handleMovementPhysics(updateMult); handleMovementPhysics(updateMult);
handleCameraRoomMovement(); handleCameraRoomMovement();
int right = (KeyHeld(KeyEvent.VK_RIGHT))||(KeyHeld(KeyEvent.VK_D))?1:0;
int left = (KeyHeld(KeyEvent.VK_LEFT))||(KeyHeld(KeyEvent.VK_A))?1:0;
switch(state){ switch(state){
case ATTACK: case ATTACK:
break; break;
case FALLING: case FALLING:
if(prvState!=State.FALLING){ if(prvState!=State.FALLING){
jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); jump_slide_fall_StartAnimationTimer = System.currentTimeMillis();
setAnimatedSpr(Sprite.ERINA_JUMP_FALL1); setAnimatedSpr(Sprite.ERINA_JUMP_FALL1);
} }
if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){ if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){
setAnimatedSpr(Sprite.ERINA_JUMP_FALL); setAnimatedSpr(Sprite.ERINA_JUMP_FALL);
jump_slide_fall_StartAnimationTimer=-1; jump_slide_fall_StartAnimationTimer=-1;
} }
break; break;
case IDLE: case IDLE:
jump_velocity = NORMAL_JUMP_VELOCITY; jump_velocity = NORMAL_JUMP_VELOCITY;
horizontal_friction = NORMAL_FRICTION; horizontal_friction = NORMAL_FRICTION;
jump_slide_fall_StartAnimationTimer=-1; jump_slide_fall_StartAnimationTimer=-1;
if(KeyHeld(KeyEvent.VK_A)||KeyHeld(KeyEvent.VK_LEFT)){ if(x_velocity!=0){
setAnimatedSpr(Sprite.ERINA_WALK); setAnimatedSpr(Sprite.ERINA_WALK);
} }
if(KeyHeld(KeyEvent.VK_D)||KeyHeld(KeyEvent.VK_RIGHT)){ else{
setAnimatedSpr(Sprite.ERINA_WALK); setAnimatedSpr(Sprite.ERINA);
} }
if(right-left==0){
setAnimatedSpr(Sprite.ERINA);
}
if(x_velocity!=0){
setAnimatedSpr(Sprite.ERINA_WALK);
}
else{
setAnimatedSpr(Sprite.ERINA);
}
break; break;
case JUMP: case JUMP:
if(prvState==State.SLIDE){ if(prvState==State.SLIDE){
//jump_velocity=-500; //jump_velocity=-500;
} }
if(jump_slide_fall_StartAnimationTimer==-1){ if(jump_slide_fall_StartAnimationTimer==-1){
jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); jump_slide_fall_StartAnimationTimer = System.currentTimeMillis();
setAnimatedSpr(Sprite.ERINA_JUMP_RISE1); setAnimatedSpr(Sprite.ERINA_JUMP_RISE1);
} }
if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){ if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){
setAnimatedSpr(Sprite.ERINA_JUMP_RISE); setAnimatedSpr(Sprite.ERINA_JUMP_RISE);
} }
break; break;
case SLIDE: case SLIDE:
horizontal_friction=0; horizontal_friction=0;
if(jump_slide_fall_StartAnimationTimer==-1){ if(jump_slide_fall_StartAnimationTimer==-1){
jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); jump_slide_fall_StartAnimationTimer = System.currentTimeMillis();
setAnimatedSpr(Sprite.ERINA_SLIDE1); setAnimatedSpr(Sprite.ERINA_SLIDE1);
}
if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>slide_AnimationWaitTime){
setAnimatedSpr(Sprite.ERINA_SLIDE);
}
if(System.currentTimeMillis()-slide_time>slide_duration){
if(KeyHeld(KeyEvent.VK_A)||KeyHeld(KeyEvent.VK_LEFT)){
facing_direction=LEFT;
} }
if(KeyHeld(KeyEvent.VK_D)||KeyHeld(KeyEvent.VK_RIGHT)){ if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>slide_AnimationWaitTime){
facing_direction=RIGHT; setAnimatedSpr(Sprite.ERINA_SLIDE);
}
if(System.currentTimeMillis()-slide_time>slide_duration){
if(KeyHeld(Action.MOVE_LEFT)){
facing_direction=LEFT;
}
if(KeyHeld(Action.MOVE_RIGHT)){
facing_direction=RIGHT;
}
state=State.IDLE;
} }
state=State.IDLE;
}
break; break;
case STAGGER: case STAGGER:
break; break;
@ -146,7 +133,7 @@ public class Player extends AnimatedObject{
break; break;
} }
prvState = state; prvState = state;
if ((KeyHeld(KeyEvent.VK_SPACE)||KeyHeld(KeyEvent.VK_W))&&System.currentTimeMillis()-spacebarPressed<jumpHoldTime) { if (KeyHeld(Action.JUMP)&&System.currentTimeMillis()-spacebarPressed<jumpHoldTime) {
y_velocity=jump_velocity; y_velocity=jump_velocity;
} }
//System.out.println(state); //System.out.println(state);
@ -154,42 +141,42 @@ public class Player extends AnimatedObject{
@Override @Override
protected void KeyReleased(int key) { protected void KeyReleased(Action a) {
if (key==KeyEvent.VK_SPACE||key==KeyEvent.VK_W) { if (a==Action.JUMP) {
spacebarPressed=0; spacebarPressed=0;
spacebarReleased=true; spacebarReleased=true;
} }
if(state!=State.SLIDE){ if(state!=State.SLIDE){
if((key==KeyEvent.VK_A||key==KeyEvent.VK_LEFT)&&(KeyHeld(KeyEvent.VK_D)||KeyHeld(KeyEvent.VK_RIGHT))){ if((a==Action.MOVE_LEFT)&&(KeyHeld(Action.MOVE_RIGHT))){
facing_direction=RIGHT; facing_direction=RIGHT;
} } else
if((key==KeyEvent.VK_D||key==KeyEvent.VK_RIGHT)&&(KeyHeld(KeyEvent.VK_A)||KeyHeld(KeyEvent.VK_LEFT))){ if((a==Action.MOVE_RIGHT)&&(KeyHeld(Action.MOVE_LEFT))){
facing_direction=LEFT; facing_direction=LEFT;
} }
} }
} }
@Override @Override
protected void KeyPressed(int key) { @SuppressWarnings("incomplete-switch")
protected void KeyPressed(Action a) {
switch(state){ switch(state){
case ATTACK: case ATTACK:
break; break;
case IDLE: case IDLE:
if(key==KeyEvent.VK_CONTROL){ if(a==Action.SLIDE||a==Action.FALL){
slide_time = System.currentTimeMillis(); slide_time = System.currentTimeMillis();
if(facing_direction){ if(facing_direction){
x_velocity=sliding_velocity; x_velocity=sliding_velocity;
} }
else{ else{
x_velocity=-sliding_velocity; x_velocity=-sliding_velocity;
}
state=State.SLIDE;
} }
state=State.SLIDE;
}
break; break;
case FALLING: case FALLING:
case JUMP: case JUMP:
if(jumpCount>0 && spacebarReleased && (key == KeyEvent.VK_SPACE || key == KeyEvent.VK_W)){ if(jumpCount>0 && spacebarReleased && (a==Action.JUMP)){
jumpCount=0; jumpCount=0;
y_velocity = jump_velocity; y_velocity = jump_velocity;
spacebarReleased=false; spacebarReleased=false;
@ -206,7 +193,7 @@ public class Player extends AnimatedObject{
break; break;
} }
if (groundCollision) { if (groundCollision) {
if (spacebarReleased&&(key==KeyEvent.VK_SPACE||key==KeyEvent.VK_W)&&jumpCount>0) { if (spacebarReleased&&(a==Action.JUMP)&&jumpCount>0) {
state = State.JUMP; state = State.JUMP;
jumpCount--; jumpCount--;
y_velocity = jump_velocity; y_velocity = jump_velocity;
@ -216,12 +203,12 @@ public class Player extends AnimatedObject{
} }
} }
if(state!=State.SLIDE){ if(state!=State.SLIDE){
switch(key){ switch(a){
case KeyEvent.VK_LEFT: case KeyEvent.VK_A: case MOVE_LEFT:
facing_direction=LEFT; facing_direction=LEFT;
break; break;
case KeyEvent.VK_RIGHT:case KeyEvent.VK_D: case MOVE_RIGHT:
facing_direction=RIGHT; facing_direction=RIGHT;
break; break;
} }
} }
@ -318,8 +305,8 @@ public class Player extends AnimatedObject{
private void handleMovementPhysics(double updateMult) { private void handleMovementPhysics(double updateMult) {
int right = (KeyHeld(KeyEvent.VK_RIGHT))||(KeyHeld(KeyEvent.VK_D))?1:0; int right = KeyHeld(Action.MOVE_RIGHT)?1:0;
int left = (KeyHeld(KeyEvent.VK_LEFT))||(KeyHeld(KeyEvent.VK_A))?1:0; int left = KeyHeld(Action.MOVE_LEFT)?1:0;
if(state==State.SLIDE){ if(state==State.SLIDE){
right=0; right=0;
left=0; left=0;

Loading…
Cancel
Save