Skip to content

Commit

Permalink
Version 1.1.5, added SnapshotProducer, TimeCompound, AdvancedRectangl…
Browse files Browse the repository at this point in the history
…e and cleaned up some mess in some file classes as well as in CollisionUtils2D
  • Loading branch information
KleeSup committed Apr 16, 2023
1 parent 0a3c29d commit 0da06ef
Show file tree
Hide file tree
Showing 31 changed files with 677 additions and 292 deletions.
Binary file modified .gradle/7.1.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.1.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.1.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.1.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 1 addition & 1 deletion .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
apply plugin: 'maven-publish'

group 'de.kleesup.libraries.gamebase'
version '1.1.4'
version '1.1.5'

sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/de/kleesup/libraries/gamebase/shared/KleeUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.kleesup.libraries.gamebase.shared;

import java.util.Objects;
import java.util.function.Consumer;

/**
* @author KleeSup
* @version 1.1
* @version 1.2
* Class created on 17.10.2022
* <p>
* A simple utility class containing some helpful methods.
Expand Down Expand Up @@ -212,4 +213,28 @@ public static <T> void paramRequireNonNull(T obj, String message){
if(obj == null)throw new IllegalArgumentException(message);
}

/**
* Determines whether a cooldown as worn of or not.
* @param currentTime The current timestamp.
* @param cooldownStartTime The timestamp the cooldown started.
* @param cooldownTime The length of the cooldown.
* @return {@code true} if the cooldown has worn off, {@code false} otherwise.
*/
public static boolean hasCooldownWornOff(long currentTime, long cooldownStartTime, long cooldownTime){
return currentTime - cooldownStartTime >= cooldownTime;
}

/**
* Runs a code part for each element of an array.
* @param array The array to execute for.
* @param consumer The consumer that accepts the elements of the array.
*/
public static <T> void arrayForEach(T[] array, Consumer<T> consumer){
KleeUtil.paramRequireNonNull(array, "Array cannot be null!");
KleeUtil.paramRequireNonNull(consumer, "Consumer cannot be null!");
for(T t : array){
consumer.accept(t);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.kleesup.libraries.gamebase.shared;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
* An interface for classes which require the functionality to create objects from snapshots of the class.
* Snapshots can be useful if you want to have an object which contains only the most necessary information of an object.
* The snapshots can be used in several ways and also to load the full object.
* NOTE: Classes implementing this interface are REQUIRED to have a constructor that accepts the created snapshot type
* or else {@link SnapshotProducer#fromSnapshot(Class, Object)} will fail!
* <br>Created on 24.02.2023</br>
* @author KleeSup
* @version 1.0
* @since 1.1.5
*/
public interface SnapshotProducer<T> {

/**
* Loads a full object from a given snapshot.
* NOTE: All classes that implement {@link SnapshotProducer} need to have a default constructor accepting the snapshot received from {@link SnapshotProducer#toSnapshot()}!
* @param clazz The class of the object to be loaded.
* @param snapshot The snapshot to load the object from.
* @return The loaded object which was created with the given snapshot.
*/
static <S extends SnapshotProducer<?>> S fromSnapshot(Class<S> clazz, Object snapshot){
KleeUtil.paramRequireNonNull(clazz, "Class to construct instance for cannot be null!");
KleeUtil.paramRequireNonNull(snapshot, "Snapshot to build instance from cannot be null!");
try {
Constructor<S> constructor = clazz.getConstructor(snapshot.getClass());
return constructor.newInstance(snapshot);
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Unable to build "+clazz.getName()+
" instance from snapshot, as the class does not have a default constructor accepting the snapshot instance! [new MyClass(myClassSnapshot) not available]", e);
}
}

/**
* @return A created snapshot of this class containing minimal essential information to load this object.
* The snapshot can be used to save this object into files or sent it via packets.
*/
T toSnapshot();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.kleesup.libraries.gamebase.shared;

import java.util.concurrent.TimeUnit;

/**
* A compound of a {@link TimeUnit} and a long-value that refers to the time.
* <br>Created on 22.02.2023</br>
* @author KleeSup
* @version 1.0
* @since 1.1.5
*/
public class TimeCompound {

private final TimeUnit unit;
private final long value;
public TimeCompound(TimeUnit unit, long value){
KleeUtil.paramRequireNonNull(unit, "TimeUnit cannot be null!");
this.unit = unit;
this.value = value;
}

public long convert(TimeUnit otherUnit){
return otherUnit.convert(value, unit);
}

public TimeUnit getUnit() {
return unit;
}

public long getValue() {
return value;
}
}
22 changes: 18 additions & 4 deletions src/main/java/de/kleesup/libraries/gamebase/shared/Updateable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@
import java.util.concurrent.TimeUnit;

/**
* @author KleeSup
* @version 1.1
* Class created on 07.10.2022
*
* Interface for classes that can receive (timed) updates.
* The {@link #update(long, TimeUnit)} method uses a long parameter which determines the time that passed since the last time this
* instance was updated. To determine the unit of the estimated time, a {@link TimeUnit} is required.
* <br>Class created on 07.10.2022</br>
* @since 1.0
* @author KleeSup
* @version 1.2
*/
public interface Updateable {

/**
* Returns the time in any desired TimeUnit between a specific timestamp and now.
* @param lastTimeStamp The timestamp to get the time in between.
* @param inputUnit The unit the timestamp is in.
* @param outputUnit The unit for the output.
* @return The translated time in between of the given timestamp and now.
*/
static long calculateEstimatedTime(long lastTimeStamp, TimeUnit inputUnit, TimeUnit outputUnit){
long timeMillis = System.currentTimeMillis() - inputUnit.toMillis(lastTimeStamp);
return outputUnit.convert(timeMillis, TimeUnit.MILLISECONDS);
}
static long calculateEstimatedTime(long lastTimeStamp, TimeUnit inputUnit){
return calculateEstimatedTime(lastTimeStamp, inputUnit, inputUnit);
}

/**
* Updates the instance.
* @param estimatedTime The estimated time since the last update was called.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.kleesup.libraries.gamebase.shared.entity;

import com.badlogic.gdx.utils.Disposable;
import de.kleesup.libraries.gamebase.shared.Identifiable;
import de.kleesup.libraries.gamebase.shared.math.gdx.AABB2D;
import de.kleesup.libraries.gamebase.shared.world.IDynamicWorldObject2D;
import de.kleesup.libraries.gamebase.shared.world.IGameWorld;
import de.kleesup.libraries.gamebase.shared.world.IWorldHolder;

/**
* A simple base class for entity implementations.
* <br>Created on 20.02.2023</br>
* @author KleeSup
* @version 1.0
* @since 1.1.5
*/
public interface IEntity2D<I, W extends IGameWorld> extends IDynamicWorldObject2D, Identifiable<I>, Disposable, IWorldHolder<W> {

AABB2D getBoundingBox();

default void kill(){
dispose();
}
default void remove(){
dispose();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.kleesup.libraries.gamebase.shared.entity;

import com.badlogic.gdx.utils.Disposable;
import de.kleesup.libraries.gamebase.shared.Identifiable;
import de.kleesup.libraries.gamebase.shared.math.AABB3D;
import de.kleesup.libraries.gamebase.shared.world.IDynamicWorldObject3D;
import de.kleesup.libraries.gamebase.shared.world.IGameWorld;
import de.kleesup.libraries.gamebase.shared.world.IWorldHolder;

/**
* A simple base class for entity implementations.
* <br>Created on 28.02.2023</br>
* @author KleeSup
* @version 1.0
* @since 1.1.5
*/
public interface IEntity3D<I, W extends IGameWorld> extends IDynamicWorldObject3D, Identifiable<I>, Disposable, IWorldHolder<W> {

AABB3D getBoundingBox();

default void kill(){
dispose();
}
default void remove(){
dispose();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.net.URI;

/**
* A basement class for data management with files.
* <br>Class created on 07.10.2022</br>
* @author KleeSup
* @version 1.0
* Class created on 07.10.2022
*
* A basement class for data management with files.
* @since 1.0
*/
public class FileBackend extends AdvancedFile implements Disposable {
public FileBackend(String pathname) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import java.net.URI;

/**
* Basement class for writing single values into file backends.
* <br>Class created on 12.05.2022</br>
* @author KleeSup
* @version 1.0
* Class created on 12.05.2022
*
* Basement class for writing single values into file backends.
* @since 1.0
*/
public abstract class FileBackendWriter<T> extends FileBackend {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import java.util.concurrent.ConcurrentHashMap;

/**
* A file backend that uses a {@link Kryo} instance to serialize the written data (in pair format) into the file.
* <br>Class created on 07.10.2022</br>
* @author KleeSup
* @version 1.1
* Class created on 07.10.2022
*
*
* @since 1.0
*/
public class KryoFileBackend extends PairFileBackend<Object, Object> {
Expand Down
Loading

0 comments on commit 0da06ef

Please sign in to comment.