Skip to content

Commit

Permalink
new features
Browse files Browse the repository at this point in the history
  • Loading branch information
NideBruyn committed Jul 26, 2022
1 parent 86396d1 commit fba6dca
Show file tree
Hide file tree
Showing 49 changed files with 186 additions and 14 deletions.
Binary file added OpenAL64.dll
Binary file not shown.
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'com.galago.sprite'
version '1.1'
version '1.3.1'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand All @@ -13,7 +13,7 @@ repositories {
}

project.ext {
jmeVer = '3.3.0-stable'
jmeVer = '3.5.0-stable'
dyn4jVer = '3.3.0'
}

Expand All @@ -23,4 +23,8 @@ dependencies {
implementation "org.jmonkeyengine:jme3-core:$jmeVer"
implementation "org.jmonkeyengine:jme3-desktop:$jmeVer"
implementation "org.dyn4j:dyn4j:$dyn4jVer"

// choose lwjgl 2 or 3
implementation "org.jmonkeyengine:jme3-lwjgl:$jmeVer"
// implementation "org.jmonkeyengine:jme3-lwjgl3:$jmeVer"
}
Binary file added lwjgl64.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/resources/Textures/dirtMid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/resources/Textures/slimeBlock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/main/java/com/galago/sprite/SpriteAnimationControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public void playAnimation(String name, float timePerFrame) {

if (seq != null && !name.equals(currentAnimationName)) {
currentAnimationName = name;
sprite.showIndex(0);
sprite.showIndex(seq[0]);
currentAnimation = seq;
animationFrameTime = timePerFrame;
currentIndex = 0;
currentIndex = seq[0];
elapsedeTime = 0f;
}

Expand Down
128 changes: 128 additions & 0 deletions src/main/java/com/galago/sprite/examples/Example1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.galago.sprite.examples;

import com.galago.sprite.Sprite;
import com.galago.sprite.camera.Camera2DState;
import com.galago.sprite.physics.Dyn4jAppState;
import com.galago.sprite.physics.RigidBodyControl2D;
import com.galago.sprite.physics.characters.PlatformerCharacterControlState;
import com.galago.sprite.physics.shape.BoxCollisionShape;
import com.galago.sprite.physics.shape.CircleCollisionShape;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.Vector2f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.texture.Texture;

/**
* This example will show you how to create simple sprites for the background,
* floor and player.
*
*
* @author NickideBruyn
*/
public class Example1 extends SimpleApplication {

private Geometry player; //The player spatial object
private Geometry background; //The background spatial
private Geometry floor; //The floor

private Dyn4jAppState dyn4jAppState;
private PlatformerCharacterControlState platformerCharacterControlState;
private Camera2DState camera2DState;

/**
* The main method for this java app when we run it.
* @param args
*/
public static void main(String[] args) {
Example1 app = new Example1();
app.start();
}

@Override
public void simpleInitApp() {

dyn4jAppState = new Dyn4jAppState();
stateManager.attach(dyn4jAppState);

//Load the background
Sprite backgroundSprite = new Sprite(20, 10);
background = new Geometry("background", backgroundSprite);
Material material = loadMaterial("Textures/colored_desert.png");
background.setMaterial(material);
backgroundSprite.scaleTextureCoordinates(new Vector2f(2, 1));
rootNode.attachChild(background);
background.move(0, 0, -1);

//Load the floor
Sprite floorSprite = new Sprite(20, 1);
floor = new Geometry("floor", floorSprite);
material = loadMaterial("Textures/dirtMid.png");
floor.setMaterial(material);
floorSprite.scaleTextureCoordinates(new Vector2f(20, 1));
rootNode.attachChild(floor);
floor.move(0, -4f, 0);

//For the floor sprite it will be a static body with 0 mass
RigidBodyControl2D rbcFloor = new RigidBodyControl2D(new BoxCollisionShape(floorSprite.getWidth(), floorSprite.getHeight()), 0);
//Attach the control to the spatial
floor.addControl(rbcFloor);
//Add the rigid body to the physics space
dyn4jAppState.getPhysicsSpace().add(rbcFloor);
//Move the rigid body to the desired position
rbcFloor.move(0, -4f);


//Load the player
Sprite playerSprite = new Sprite(1, 1);
player = new Geometry("player", playerSprite);
material = loadMaterial("Textures/slimeBlock.png");
player.setMaterial(material);
player.setQueueBucket(RenderQueue.Bucket.Transparent);
rootNode.attachChild(player);
player.move(0, 3f, 0);

//The player rigid body will have mass so that it can be affected by gravity
RigidBodyControl2D rbcPlayer = new RigidBodyControl2D(new CircleCollisionShape(0.5f), 1);
//Add the control to the player spatial
player.addControl(rbcPlayer);
//Add it to the physics space
dyn4jAppState.getPhysicsSpace().add(rbcPlayer);
//Now we can move it to a start position
rbcPlayer.move(0, 3);
//Adjust the player's gravity
rbcPlayer.setGravityScale(2);


platformerCharacterControlState = new PlatformerCharacterControlState(dyn4jAppState, player);
stateManager.attach(platformerCharacterControlState);

//Example4-Step1: Create the camera2Dstate and give it a distance and movement interpolation amount
camera2DState = new Camera2DState(player, 6, 0.01f);
//Set camera clipping which will block the camera from moving a certain min and max value on the x and y axis
camera2DState.setCameraClipping(new Vector2f(-2f, 0), new Vector2f(2f, 1));
//You can also set a camera to player offset. Move the camera to stick above the player 2f in the y axis
camera2DState.setTargetOffset(new Vector2f(0, 2));
//Attach the camera2Dstate to the state manager
stateManager.attach(camera2DState);

}

/**
* Local method for reuse of loading a texture as an unshaded material
*
* @param file
* @return
*/
protected Material loadMaterial(String file) {
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture tex = assetManager.loadTexture(new TextureKey(file, false));
tex.setWrap(Texture.WrapMode.Repeat);
mat.setTexture("ColorMap", tex);
mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
return mat;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.galago.sprite.physics.characters;

import com.galago.sprite.Sprite;
import com.galago.sprite.SpriteAnimationControl;
import com.galago.sprite.physics.*;
import com.galago.sprite.physics.shape.CollisionShape;
import com.jme3.app.Application;
Expand Down Expand Up @@ -46,10 +45,16 @@ public class PlatformerCharacterControlState extends BaseAppState implements Act
private boolean onGround;
private int jumpCount = 0;
private float jumpForce = 10;
private PlatformerCharacterListener platformerCharacterListener;

public PlatformerCharacterControlState(Dyn4jAppState dyn4jAppState, Spatial spatial) {
this(dyn4jAppState, spatial, null);
}

public PlatformerCharacterControlState(Dyn4jAppState dyn4jAppState, Spatial spatial, Sprite sprite) {
this.dyn4jAppState = dyn4jAppState;
this.spatial = spatial;
this.sprite = sprite;
}

@Override
Expand Down Expand Up @@ -84,7 +89,9 @@ protected void onEnable() {

}

sprite = (Sprite) ((Geometry) spatial).getMesh();
if (sprite == null) {
sprite = (Sprite) ((Geometry) spatial).getMesh();
}

registerWithInput(inputManager);

Expand Down Expand Up @@ -144,6 +151,28 @@ public void unregisterInput() {

}

public void setPlatformerCharacterListener(PlatformerCharacterListener platformerCharacterListener) {
this.platformerCharacterListener = platformerCharacterListener;
}

protected void fireIdleEvent() {
if (this.platformerCharacterListener != null) {
this.platformerCharacterListener.doCharacterIdleEvent();
}
}

protected void fireWalkEvent(float dir) {
if (this.platformerCharacterListener != null) {
this.platformerCharacterListener.doCharacterWalkEvent(dir);
}
}

protected void fireJumpEvent(int count) {
if (this.platformerCharacterListener != null) {
this.platformerCharacterListener.doCharacterJumpEvent(count);
}
}

@Override
public void onAction(String name, boolean isPressed, float tpf) {

Expand All @@ -152,6 +181,7 @@ public void onAction(String name, boolean isPressed, float tpf) {
if (!jump && (onGround || jumpCount < 2)) {
jump = true;
onGround = false;
fireJumpEvent(jumpCount);
jumpCount++;

}
Expand Down Expand Up @@ -185,7 +215,7 @@ public void onAction(String name, boolean isPressed, float tpf) {
if (!jump && (onGround || jumpCount < 2)) {
jump = true;
onGround = false;

fireJumpEvent(jumpCount);

}

Expand All @@ -210,13 +240,12 @@ public void update(float tpf) {

}

if (spatial.getControl(SpriteAnimationControl.class) != null) {
if (movementDirection.x == 0) {
spatial.getControl(SpriteAnimationControl.class).playAnimation("idle", 0.2f);
} else {
spatial.getControl(SpriteAnimationControl.class).playAnimation("walk", 0.1f);
if (movementDirection.x == 0) {
fireIdleEvent();

} else {
fireWalkEvent(movementDirection.x);

}
}

if (movementDirection.x < 0) {
Expand All @@ -226,7 +255,7 @@ public void update(float tpf) {
sprite.flipCoords(false);

} else {
sprite.flipCoords(false);
// sprite.flipCoords(false);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.galago.sprite.physics.characters;

public interface PlatformerCharacterListener {

public void doCharacterIdleEvent();

public void doCharacterWalkEvent(float direction);

public void doCharacterJumpEvent(int count);

}
Binary file added src/main/resources/Textures/colored_desert.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/Textures/dirtMid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/Textures/slimeBlock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fba6dca

Please sign in to comment.