Added lots of parameters to Entity and Character so it woul be easier to handle actions during gameplay.

This commit is contained in:
no 2021-06-07 15:55:04 +02:00
parent 9ea45e793a
commit 67d4c9be53
12 changed files with 239 additions and 52 deletions

View File

@ -2,7 +2,7 @@ package engine.input;
public class InputConst { public class InputConst {
/* /*
Button ID ButtonIG ID
*/ */
public final static int buttonA=0, buttonB=1, buttonX=2, buttonY=3, LB = 4, RB = 5, select = 6, start = 7, public final static int buttonA=0, buttonB=1, buttonX=2, buttonY=3, LB = 4, RB = 5, select = 6, start = 7,
L_JoystickClick = 8, R_JoystickClick =9, up = 10, right = 11, down = 12, left = 13; L_JoystickClick = 8, R_JoystickClick =9, up = 10, right = 11, down = 12, left = 13;

View File

@ -1,10 +1,13 @@
package gameplay.actions; package gameplay.actions;
import gameplay.frames.*; import gameplay.frames.*;
import gameplay.input.ButtonIG;
import java.util.ArrayList; import java.util.ArrayList;
public interface Action { public interface Action {
ArrayList<Frame> getFrame(); ArrayList<Frame> getFrame();
abstract ButtonIG[][] getCommand();
} }

View File

@ -16,7 +16,7 @@ public class Attack implements Action {
* For example, a classic fireball would be something like * For example, a classic fireball would be something like
* {{DOWN},{DOWN,RIGHT},{RIGHT},{A}} * {{DOWN},{DOWN,RIGHT},{RIGHT},{A}}
*/ */
private static Button[][] command; private static ButtonIG[][] command;
/** /**
@ -42,4 +42,29 @@ public class Attack implements Action {
} }
return res; return res;
} }
public static boolean isIsSpecial() {
return isSpecial;
}
public static void setIsSpecial(boolean isSpecial) {
Attack.isSpecial = isSpecial;
}
@Override
public ButtonIG[][] getCommand() {
return command;
}
public static void setCommand(ButtonIG[][] command) {
Attack.command = command;
}
public attackPart[] getParts() {
return parts;
}
public void setParts(attackPart[] parts) {
this.parts = parts;
}
} }

View File

@ -3,15 +3,15 @@ package gameplay.actions;
import java.util.ArrayList; import java.util.ArrayList;
import gameplay.frames.Frame; import gameplay.frames.Frame;
import gameplay.input.Button; import gameplay.input.ButtonIG;
public class Dash implements Action { public class Dash implements Action {
/** /**
* The suite of Inputs to have the move come out. * The suite of Inputs to have the move come out.
* For example, a Front Dash would be something like * For example, a Front Dash would be something like
* {{RIGHT},{RIGHT}} * {{FORWARD},{FORWARD}}
*/ */
private static Button[][] command; private static ButtonIG[][] command;
private Frame[] frames; private Frame[] frames;
@ -25,4 +25,9 @@ public class Dash implements Action {
return res; return res;
} }
@Override
public ButtonIG[][] getCommand() {
return command;
}
} }

View File

@ -3,16 +3,16 @@ package gameplay.actions;
import java.util.ArrayList; import java.util.ArrayList;
import gameplay.frames.Frame; import gameplay.frames.Frame;
import gameplay.input.Button; import gameplay.input.ButtonIG;
public class Jump implements Action { public class Jump implements Action {
/** /**
* The Input to have the move come out. * The Input to have the move come out.
* For example, a Front Jump would be something like * For example, a Front Jump would be something like
* {UP,RIGHT} * {{UP,RIGHT}}
*/ */
private static Button[] command; private static ButtonIG[][] command;
private Frame[] frames; private Frame[] frames;
@ -25,4 +25,9 @@ public class Jump implements Action {
return res; return res;
} }
@Override
public ButtonIG[][] getCommand() {
return command;
}
} }

View File

@ -3,7 +3,7 @@ package gameplay.actions;
import java.util.ArrayList; import java.util.ArrayList;
import gameplay.frames.Frame; import gameplay.frames.Frame;
import gameplay.input.Button; import gameplay.input.ButtonIG;
public class Throw implements Action { public class Throw implements Action {
@ -17,7 +17,7 @@ public class Throw implements Action {
* For example, a Moonsault Press would be something like * For example, a Moonsault Press would be something like
* {{LEFT},{DOWN,LEFT},{DOWN},{DOWN,RIGHT},{RIGHT},{RIGHT,UP},{UP},{A}} * {{LEFT},{DOWN,LEFT},{DOWN},{DOWN,RIGHT},{RIGHT},{RIGHT,UP},{UP},{A}}
*/ */
private static Button[][] command; private static ButtonIG[][] command;
/** /**
* The different sections of the throw * The different sections of the throw
@ -50,4 +50,9 @@ public class Throw implements Action {
return res; return res;
} }
@Override
public ButtonIG[][] getCommand() {
return command;
}
} }

View File

@ -1,6 +1,8 @@
package gameplay.entities; package gameplay.entities;
import gameplay.actions.*;
import gameplay.frames.Frame; import gameplay.frames.Frame;
import gameplay.frames.nextFrameBuffer;
/** /**
* Character class, which is a sub-class of an entity * Character class, which is a sub-class of an entity
@ -9,6 +11,23 @@ import gameplay.frames.Frame;
*/ */
public class Character extends Entity { public class Character extends Entity {
private static int maxHP; private static int maxHP;
/**
* All of the attacks, both special and normal, of the character
* They should be put in the order of priority (attacks[0] being the highest priority)
* For example, you should have something like :
* {Rising Punch, FireBall, Jump D/C/B/A, Crouch D/C/B/A, Stand D/C/B/A}
*/
private static Attack[] attacks;
private static Jump forwardJump;
private static Jump neutralJump;
private static Jump backJump;
private static Dash forwardDash;
private static Dash backDash;
private static Throw normalthrow;
/** /**
* Main constructor for a character. By default its max health is 1000 if not specified * Main constructor for a character. By default its max health is 1000 if not specified
@ -28,4 +47,62 @@ public class Character extends Entity {
} }
public int getMaxHP() { return this.maxHP;} public int getMaxHP() { return this.maxHP;}
public static Attack[] getAttacks() {
return attacks;
}
public static void setAttacks(Attack[] attacks) {
Character.attacks = attacks;
}
public static Jump getForwardJump() {
return forwardJump;
}
public static void setForwardJump(Jump forwardJump) {
Character.forwardJump = forwardJump;
}
public static Jump getNeutralJump() {
return neutralJump;
}
public static void setNeutralJump(Jump neutralJump) {
Character.neutralJump = neutralJump;
}
public static Jump getBackJump() {
return backJump;
}
public static void setBackJump(Jump backJump) {
Character.backJump = backJump;
}
public static Dash getForwardDash() {
return forwardDash;
}
public static void setForwardDash(Dash forwardDash) {
Character.forwardDash = forwardDash;
}
public static Dash getBackDash() {
return backDash;
}
public static void setBackDash(Dash backDash) {
Character.backDash = backDash;
}
public static Throw getNormalthrow() {
return normalthrow;
}
public static void setNormalthrow(Throw normalthrow) {
Character.normalthrow = normalthrow;
}
} }

View File

@ -1,6 +1,9 @@
package gameplay.entities; package gameplay.entities;
import gameplay.frames.Frame; import gameplay.frames.Frame;
import gameplay.frames.nextFrameBuffer;
import java.util.ArrayList;
/** /**
* Entity class, which is the main class regrouping characters and projectiles * Entity class, which is the main class regrouping characters and projectiles
@ -10,7 +13,8 @@ import gameplay.frames.Frame;
public class Entity { public class Entity {
private int posx; private int posx;
private int posy; private int posy;
private Frame currentFrame;
private static nextFrameBuffer frames;
/** /**
* base constructor of the entity class * base constructor of the entity class
@ -18,7 +22,7 @@ public class Entity {
public Entity() { public Entity() {
this.posx = 0; this.posx = 0;
this.posy = 0; this.posy = 0;
this.currentFrame = new Frame(); this.frames = new nextFrameBuffer();
} }
/** /**
@ -30,21 +34,56 @@ public class Entity {
public Entity(int posx, int posy, Frame f) { public Entity(int posx, int posy, Frame f) {
this.posx = posx; this.posx = posx;
this.posy = posy; this.posy = posy;
this.currentFrame = f; this.frames = new nextFrameBuffer();
this.frames.setCurrentFrame(f);
} }
public void setPos(int x, int y) { public void setPos(int x, int y) {
this.posx = x; this.posx = x;
this.posy = y; this.posy = y;
} }
public void setCurrentFrame(Frame f) { public void setCurrentFrame(Frame f) {
this.currentFrame = f; this.frames.setCurrentFrame(f);
} }
public int getPosX() {return this.posx;} public int getPosX() {return this.posx;}
public int getPosY() {return this.posy;} public int getPosY() {return this.posy;}
public Frame getCurrentframe() {return this.currentFrame;} public Frame getCurrentframe() {return this.frames.getCurrentFrame();}
public static nextFrameBuffer Frames() {
return frames;
}
public void setFrames(nextFrameBuffer nextFrames) {
this.frames = nextFrames;
}
public void clearNextFrames() {
this.frames.emptyQueue();
}
/**
* adds frames to the character queue.
* Warning : does not clear the queue first
* @param f
*/
public void addNextFrames(Frame[] f) {
for(int i = 0; i < f.length; i++) {
this.frames.addFrameToQueue(f[i]);
}
}
/**
* adds a list of frames to the character queue.
* Warning : does not clear the queue first
* @param f
*/
public void addNextFramesList(ArrayList<Frame> f) {
for(int i = 0; i < f.size(); i++) {
this.frames.addFrameToQueue(f.get(i));
}
}
} }

View File

@ -1,14 +1,17 @@
package gameplay.input; package gameplay.input;
public enum Button { /**
UP, DOWN, LEFT, RIGHT, A, B, C, D; * In-Game buttons, not to be mixed up with the physical gamepad buttons.
*/
public enum ButtonIG {
UP, DOWN, BACK, FORWARD, A, B, C, D;
public int toInt() { public int toInt() {
switch (this) { switch (this) {
case UP : return 0; case UP : return 0;
case DOWN : return 1; case DOWN : return 1;
case LEFT : return 2; case BACK : return 2;
case RIGHT : return 3; case FORWARD : return 3;
case A : return 4; case A : return 4;
case B : return 5; case B : return 5;
case C : return 6; case C : return 6;
@ -21,8 +24,8 @@ public enum Button {
switch (this) { switch (this) {
case UP : return "UP"; case UP : return "UP";
case DOWN : return "DOWN"; case DOWN : return "DOWN";
case LEFT : return "LEFT"; case BACK : return "LEFT";
case RIGHT : return "RIGHT"; case FORWARD : return "RIGHT";
case A : return "A"; case A : return "A";
case B : return "B"; case B : return "B";
case C : return "C"; case C : return "C";
@ -31,8 +34,8 @@ public enum Button {
} }
} }
public static Button intToButton(int i) { public static ButtonIG intToButton(int i) {
Button[] b = {UP, DOWN, LEFT, RIGHT, A, B, C, D}; ButtonIG[] b = {UP, DOWN, BACK, FORWARD, A, B, C, D};
try { try {
return b[i]; return b[i];
} catch (ArrayIndexOutOfBoundsException e) { } catch (ArrayIndexOutOfBoundsException e) {

View File

@ -82,10 +82,11 @@ public class InputBuffer {
/** /**
* records the new Inputs from a gamepad in the gamepad buffer * records the new Inputs from a gamepad in the gamepad buffer
* @param pad the gamepad to record * @param pad the gamepad to record
* @param facesRight whether the character faces right or not, to know which way is forward
*/ */
public void recordInputsFromGamepad(GamepadInput pad) { public void recordInputsFromGamepad(GamepadInput pad, boolean facesRight) {
Inputs in = new Inputs(); Inputs in = new Inputs();
in.recordFromGamepad(pad); in.recordFromGamepad(pad, facesRight);
this.recordInputs(in); this.recordInputs(in);
} }
} }

View File

@ -23,7 +23,7 @@ public class Inputs {
* record one input * record one input
* @param b the input to be recorded * @param b the input to be recorded
*/ */
public void recordOneInput(Button b) { public void recordOneInput(ButtonIG b) {
int i = b.toInt(); int i = b.toInt();
this.tab[i] = true; this.tab[i] = true;
} }
@ -33,7 +33,7 @@ public class Inputs {
* @param b the button to be checked * @param b the button to be checked
* @return * @return
*/ */
public boolean containsInput(Button b) { public boolean containsInput(ButtonIG b) {
return this.tab[b.toInt()]; return this.tab[b.toInt()];
} }
@ -54,7 +54,7 @@ public class Inputs {
* @param bs a number of inputs. Check if those are contained in this * @param bs a number of inputs. Check if those are contained in this
* @return true if all inputs of in are also in this * @return true if all inputs of in are also in this
*/ */
public boolean containsButtonTab(Button[] bs) { public boolean containsButtonTab(ButtonIG[] bs) {
for(int i = 0; i < bs.length; i++) { for(int i = 0; i < bs.length; i++) {
if(!this.containsInput(bs[i])) { return false;} if(!this.containsInput(bs[i])) { return false;}
} }
@ -65,21 +65,21 @@ public class Inputs {
return this.tab; return this.tab;
} }
public void recordFromGamepad(GamepadInput pad) { public void recordFromGamepad(GamepadInput pad, boolean facesRight) {
this.tab[Button.UP.toInt()] = pad.checkPressed(InputConst.up); this.tab[ButtonIG.UP.toInt()] = pad.checkPressed(InputConst.up);
this.tab[Button.DOWN.toInt()] = pad.checkPressed(InputConst.down); this.tab[ButtonIG.DOWN.toInt()] = pad.checkPressed(InputConst.down);
this.tab[Button.LEFT.toInt()] = pad.checkPressed(InputConst.left); this.tab[ButtonIG.BACK.toInt()] = (pad.checkPressed(InputConst.left) && facesRight) || (pad.checkPressed(InputConst.right)&&!facesRight);
this.tab[Button.RIGHT.toInt()] = pad.checkPressed(InputConst.right); this.tab[ButtonIG.FORWARD.toInt()] = (pad.checkPressed(InputConst.right) && facesRight) || (pad.checkPressed(InputConst.left) && !facesRight);
this.tab[Button.A.toInt()] = pad.checkPressed(InputConst.buttonX); this.tab[ButtonIG.A.toInt()] = pad.checkPressed(InputConst.buttonX);
this.tab[Button.B.toInt()] = pad.checkPressed(InputConst.buttonA); this.tab[ButtonIG.B.toInt()] = pad.checkPressed(InputConst.buttonA);
this.tab[Button.C.toInt()] = pad.checkPressed(InputConst.buttonY); this.tab[ButtonIG.C.toInt()] = pad.checkPressed(InputConst.buttonY);
this.tab[Button.D.toInt()] = pad.checkPressed(InputConst.buttonB); this.tab[ButtonIG.D.toInt()] = pad.checkPressed(InputConst.buttonB);
} }
public String toString() { public String toString() {
String s = ""; String s = "";
for(int i = 0; i < numberOfInputs; i++) { for(int i = 0; i < numberOfInputs; i++) {
if(this.tab[i]) {s = s + Button.intToButton(i).toString() + " "; } if(this.tab[i]) {s = s + ButtonIG.intToButton(i).toString() + " "; }
} }
return s; return s;
} }

View File

@ -1,16 +1,11 @@
package gameplay.match; package gameplay.match;
import engine.Engine;
import engine.input.Button; import engine.input.Button;
import engine.input.GamepadInput; import engine.input.GamepadInput;
import engine.input.InputConst;
import engine.object.ObjectGl;
import gameplay.input.InputBuffer; import gameplay.input.InputBuffer;
import gameplay.entities.*;
import gameplay.entities.Character; import gameplay.entities.Character;
import gameplay.input.Inputs;
import java.util.ArrayList; import gameplay.input.ButtonIG;
import java.util.List;
import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.glfw.GLFW.*;
@ -35,6 +30,8 @@ public class match {
private static long timeStamp1; private static long timeStamp1;
private static long timeStamp2; private static long timeStamp2;
private static int frameCount; private static int frameCount;
private static GamepadInput gamepad1 = null;
private static GamepadInput gamepad2 = null;
/** /**
* base constructor of the match class. * base constructor of the match class.
@ -101,14 +98,14 @@ public class match {
boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1); boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1);
GamepadInput gamepad1 = null; GamepadInput gamepad1 = null;
Button jump = null; ButtonIG jump = null;
if (Joystick1Present) { if (Joystick1Present) {
gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); gamepad1 = new GamepadInput(GLFW_JOYSTICK_1);
gamepad1.inputRefresh(); gamepad1.inputRefresh();
List<Integer> listJump = new ArrayList<>(); List<Integer> listJump = new ArrayList<>();
listJump.add(InputConst.buttonA); listJump.add(InputConst.buttonA);
jump = new Button("jump", listJump, gamepad1); jump = new ButtonIG("jump", listJump, gamepad1);
} }
while (engine.isRunning()) { while (engine.isRunning()) {
@ -148,13 +145,17 @@ public class match {
int frame = 0; int frame = 0;
boolean goToNextFrame = true; boolean goToNextFrame = true;
boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1); boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1);
GamepadInput gamepad1 = null; boolean Joystick2Present = glfwJoystickPresent(GLFW_JOYSTICK_2);
match match = new match(new Character(),new Character()); //TOD0 : Change to not empty chars match match = new match(new Character(),new Character()); //TOD0 : Change to not empty chars
if (Joystick1Present) { if (Joystick1Present) {
gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); gamepad1 = new GamepadInput(GLFW_JOYSTICK_1);
gamepad1.inputRefresh(); gamepad1.inputRefresh();
} }
if(Joystick2Present) {
gamepad2 = new GamepadInput(GLFW_JOYSTICK_2);
gamepad2.inputRefresh();
}
} }
@ -192,9 +193,32 @@ public class match {
//if p2 is at 0 health //if p2 is at 0 health
case 13: case 13:
roundsWonP2++; roundsWonP1++;
ac(11); ac(11);
break; break;
//read both players inputs
case 20:
gamepad1.inputRefresh();
gamepad2.inputRefresh();
inputsP1.recordInputsFromGamepad(gamepad1, p1.getPosX() < p2.getPosX());
inputsP2.recordInputsFromGamepad(gamepad2, p2.getPosX() <= p1.getPosX());
handleInputs(p1, inputsP1);
handleInputs(p2, inputsP2);
}
}
/**
* Will handle the inputs recorder and have the character do a corresponding action if possible
* Order of priority is Throw > Special > Normal > Jump > Dash > Crouch > Move > do nothing
* @param c
* @param input
*/
private static void handleInputs(Character c, InputBuffer input) {
Inputs latestIn = input.getLatestInputs();
if(latestIn.containsButtonTab(c.getNormalthrow().getCommand()[0])) {
c.clearNextFrames();
c.addNextFramesList(c.getNormalthrow().getFrame());
} }
} }
} }