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

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

@ -13,7 +13,10 @@ public enum Action {
FALL(new Key(KeyEvent.VK_DOWN),new Key(KeyEvent.VK_S)),
SLIDE(new Key(KeyEvent.VK_CONTROL)),
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;
Key controllingKey;

@ -5,6 +5,7 @@ import java.util.List;
import net.java.games.input.Component;
import net.java.games.input.Component.Identifier;
import sig.RabiClone;
public class KeyBind {
public static HashMap<Action,List<Component>> KEYBINDS = new HashMap<>();
@ -22,7 +23,7 @@ public class KeyBind {
this.val=val;
}
public static boolean IsKeyHeld(Action action) {
public static boolean isKeyHeld(Action action) {
return KEYS.getOrDefault(action,false);
}
@ -77,19 +78,25 @@ public class KeyBind {
}
private static void actionEventCheck(Action a, boolean held) {
if (KeyBind.IsKeyHeld(a)&&!held) {
if (KeyBind.isKeyHeld(a)&&!held) {
emitReleaseEvent(a);
} else
if (!KeyBind.IsKeyHeld(a)&&held) {
if (!KeyBind.isKeyHeld(a)&&held) {
emitPressEvent(a);
}
KeyBind.setKeyPressed(a, held);
}
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) {
//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);
}
protected boolean KeyHeld(int key) {
return Key.isKeyHeld(key);
protected boolean KeyHeld(Action a) {
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) {

@ -403,9 +403,6 @@ public class Panel extends JPanel implements Runnable,KeyListener {
public void keyPressed(KeyEvent e) {
if (!Key.isKeyHeld(e.getKeyCode())) {
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);
}
@ -413,9 +410,6 @@ public class Panel extends JPanel implements Runnable,KeyListener {
@Override
public void keyReleased(KeyEvent e) {
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);
}
}

@ -1,10 +1,10 @@
package sig.objects;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import sig.RabiClone;
import sig.engine.Action;
import sig.engine.Alpha;
import sig.engine.Font;
import sig.engine.MouseScrollValue;
@ -49,10 +49,10 @@ public class EditorRenderer extends LevelRenderer{
@Override
public void update(double updateMult) {
int right = KeyHeld(KeyEvent.VK_RIGHT)||KeyHeld(KeyEvent.VK_D)?1:0;
int left = KeyHeld(KeyEvent.VK_LEFT)||KeyHeld(KeyEvent.VK_A)?1:0;
int up = KeyHeld(KeyEvent.VK_UP)||KeyHeld(KeyEvent.VK_W)?1:0;
int down = KeyHeld(KeyEvent.VK_DOWN)||KeyHeld(KeyEvent.VK_S)?1:0;
int right = KeyHeld(Action.MOVE_RIGHT)?1:0;
int left = KeyHeld(Action.MOVE_LEFT)?1:0;
int up = KeyHeld(Action.JUMP)?1:0;
int down = KeyHeld(Action.FALL)?1:0;
if (right-left!=0) {
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;
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...");
try {
Map.SaveMap(RabiClone.CURRENT_MAP);
@ -177,17 +177,18 @@ public class EditorRenderer extends LevelRenderer{
}
@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 tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT;
switch (key) {
case KeyEvent.VK_F3:{
switch (a) {
case EDITOR_SET_VIEW:{
RabiClone.CURRENT_MAP.setView(tileX,tileY,View.values()[(RabiClone.CURRENT_MAP.getView(tileX, tileY).ordinal()+1)%View.values().length]);
}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]);
}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]);
}break;
}

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

Loading…
Cancel
Save