SortZ est maintenant VRAIMENT compatibles avec les flottants ajout d'ombre sous les sprites

This commit is contained in:
Antoine 2021-06-16 17:29:40 +02:00
parent 6dbfaa6e14
commit 846716db11
5 changed files with 65 additions and 18 deletions

View File

@ -40,7 +40,7 @@ public class UIElementText extends UIDummy{
public void setBackground(Vector3f color){ public void setBackground(Vector3f color){
this.colorBg = color; this.colorBg = color;
this.background = new ObjectGl(this.zPos, objs.size() * 10f, 10f, this.scalingFactor, null, this.colorBg ); this.background = new ObjectGl(this.zPos, objs.size() * 10f, 10f, this.scalingFactor - 0.1f, null, this.colorBg );
engine.add_objectGl(this.background); engine.add_objectGl(this.background);
} }

View File

@ -30,15 +30,15 @@ public class ObjectGl {
/** /**
* xPos and yPos will stop to be relevant if you use rotate function * xPos and yPos will stop to be relevant if you use rotate function
*/ */
private float xPos; protected float xPos;
private float yPos; protected float yPos;
private float zPos; protected float zPos;
private float xAngle; protected float xAngle;
private float yAngle; protected float yAngle;
private float zAngle; protected float zAngle;
private float width; // To be used in setTextureWrap protected float width; // To be used in setTextureWrap
private float height; protected float height;
private float scalingFactor; protected float scalingFactor;
public boolean useTime; public boolean useTime;

View File

@ -6,6 +6,9 @@ public class SortZ implements Comparator<ObjectGl>
{ {
public int compare(ObjectGl a, ObjectGl b) public int compare(ObjectGl a, ObjectGl b)
{ {
return (int) (a.getZPos() - b.getZPos()); float diff = a.getZPos() - b.getZPos();
if (diff < 0) return -1;
else if (diff > 0) return 1;
else return 0;
} }
} }

View File

@ -1,11 +1,42 @@
package engine.object; package engine.object;
import engine.math.Matrix4f;
import engine.math.Vector3f; import engine.math.Vector3f;
public class Sprite extends ObjectGl { public class Sprite extends ObjectGl {
private ObjectGl shadow;
public Sprite(float z, float size, String tex, Vector3f color) { public Sprite(float z, float size, String tex, Vector3f color) {
super(z, 1f, 1f, size, tex, color); super(z, 1f, 1f, size, tex, color);
this.shadow = null;
}
/**
* Move the object according to vec, direction can change if rotation method have been used
* @param vec Vector3f
*/
public void translate(Vector3f vec){
this.xPos += vec.x;
this.yPos += vec.y;
this.zPos += vec.z;
Vector3f vecTemp = vec.divXYZ(this.scalingFactor);
this.transform = this.transform.multiply(Matrix4f.translate(vecTemp));
if (this.shadow != null){
this.shadow.translate(new Vector3f(vec.x, 0f, 0f));
}
}
/**
* Ajoute une ombre sous le personnage
*/
public void setShadow(){
this.shadow = new ObjectGl(this.zPos, this.width, this.height * 0.3f, this.scalingFactor, "textures/shadow.png", null);
this.shadow.translate(new Vector3f(this.xPos, this.yPos - this.height * 4.3f, -8f)); // 8 ça fait bcp mais y du z-fighting sinon jsp pk
}
public ObjectGl getShadow(){
return this.shadow;
} }
} }

View File

@ -69,7 +69,7 @@ public class match {
private static GamepadInput gamepad1 = null; private static GamepadInput gamepad1 = null;
private static GamepadInput gamepad2 = null; private static GamepadInput gamepad2 = null;
// GUI // GUI / HUD ?
private static UIElementText coordP1; private static UIElementText coordP1;
private static UIElementText coordP2; private static UIElementText coordP2;
private static UIElement healthBarP1; private static UIElement healthBarP1;
@ -78,7 +78,7 @@ public class match {
private static HorizontalProgressBar healthBarP2Obj; private static HorizontalProgressBar healthBarP2Obj;
private static UIElementText timerUI; private static UIElementText timerUI;
private static ObjectGl objP1,objP2; private static Sprite objP1,objP2;
private static Engine engine; private static Engine engine;
private static Frame f; private static Frame f;
private static int acCode = 0; private static int acCode = 0;
@ -95,6 +95,9 @@ public class match {
p2.setPos((int) (750 - objP2.getWidth() * objP2.getScalingFactor()), groundLevel); //TODO : change to better values if needed p2.setPos((int) (750 - objP2.getWidth() * objP2.getScalingFactor()), groundLevel); //TODO : change to better values if needed
objP1.translate(new Vector3f(p1.getPosX(),p1.getPosY(),0)); objP1.translate(new Vector3f(p1.getPosX(),p1.getPosY(),0));
objP2.translate(new Vector3f(p2.getPosX(),p2.getPosY(),0)); objP2.translate(new Vector3f(p2.getPosX(),p2.getPosY(),0));
// TODO meilleur implémentation possible
objP1.getShadow().translate(new Vector3f(0f,p1.getPosY(),0));
objP2.getShadow().translate(new Vector3f(0f,p2.getPosY(),0));
} }
/** /**
@ -148,14 +151,13 @@ public class match {
ObjectGl background = new ObjectGl(0f,1f,1f,2.5f, pathToBG, null); ObjectGl background = new ObjectGl(0f,1f,1f,2.5f, pathToBG, null);
background.setTextureWrap(0, 0, 1914f, 701f); background.setTextureWrap(0, 0, 1914f, 701f);
background.translate(new Vector3f(-1000f, 1000f, 0f)); background.translate(new Vector3f(-1350f, 1000f, 0f));
engine.add_objectGl(background); engine.add_objectGl(background);
p1 = CharacterBlue.generateCharBlue(); p1 = CharacterBlue.generateCharBlue();
p2 = CharacterBlue.generateCharBlue(); p2 = CharacterBlue.generateCharBlue();
objP1 = new Sprite(10f, 5f, path, null); objP1 = new Sprite(14f, 5f, path, null);
objP2 = new Sprite(15f, 5f, path, null); objP2 = new Sprite(15f, 5f, path, new Vector3f(1.0f,0.0f,1.0f));
objP2.setColor(new Vector3f(1.0f,0.0f,1.0f));
engine.add_objectGl(objP1); engine.add_objectGl(objP1);
engine.add_objectGl(objP2); engine.add_objectGl(objP2);
@ -166,6 +168,17 @@ public class match {
objP2.setTextureWrap(f.getSprite()[0], f.getSprite()[1], f.getSprite()[2], f.getSprite()[3]); objP2.setTextureWrap(f.getSprite()[0], f.getSprite()[1], f.getSprite()[2], f.getSprite()[3]);
objP2.flipTextureWrapH(); objP2.flipTextureWrapH();
//Création des ombres
objP1.setShadow();
engine.add_objectGl(objP1.getShadow());
objP2.setShadow();
engine.add_objectGl(objP2.getShadow());
System.out.println(objP2.getZPos());
System.out.println(objP2.getShadow().getZPos());
System.out.println(objP1.getZPos());
System.out.println(objP1.getShadow().getZPos());
if(Joystick1Present) { if(Joystick1Present) {
gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); gamepad1 = new GamepadInput(GLFW_JOYSTICK_1);
gamepad1.inputRefresh(); gamepad1.inputRefresh();
@ -300,7 +313,7 @@ public class match {
f = p2.getCurrentframe(); f = p2.getCurrentframe();
objP2.setTextureWrap(f.getSprite()[0], f.getSprite()[1], f.getSprite()[2], f.getSprite()[3]); objP2.setTextureWrap(f.getSprite()[0], f.getSprite()[1], f.getSprite()[2], f.getSprite()[3]);
objP2.translate(new Vector3f(0-(p2.getPosX()-oldPosXp2),p2.getPosY()-oldPosYp2,0)); objP2.translate(new Vector3f(-(p2.getPosX() - oldPosXp2),p2.getPosY()-oldPosYp2,0));
Frame nf = new Frame(); Frame nf = new Frame();
nf.clone(p2.getCurrentframe()); nf.clone(p2.getCurrentframe());