Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public long getMousedOverWindow() {
*/
public Control registerControlListener(Control control) {
return switch (control) {
case KeyboardKeyControl kkc -> controlRegistry.register(ClientBase.getInstance().identifierOf("keyboardKey_control_" + kkc.getKey().name()), kkc).getValue();
case GamepadButtonControl gpbc -> controlRegistry.register(ClientBase.getInstance().identifierOf("gamepadButton_control_" + gpbc.getButton().name()), gpbc).getValue();
case MouseButtonControl mbc -> controlRegistry.register(ClientBase.getInstance().identifierOf("mouseButton_control_" + mbc.getButton().name()), mbc).getValue();
case GamepadAxisControl gpac -> controlRegistry.register(ClientBase.getInstance().identifierOf("gamepadAxis_control_" + gpac.getAxis().name()), gpac).getValue();
case MouseMovementControl mmc -> controlRegistry.register(ClientBase.getInstance().identifierOf("mouseMovement_control_" + mmc.getAxis().name()), mmc).getValue();
case MouseScrollControl msc -> controlRegistry.register(ClientBase.getInstance().identifierOf("mouseScroll_control_" + msc.getDirection().name()), msc).getValue();
case KeyboardKeyControl kkc -> controlRegistry.register(ClientBase.getInstance().identifierOf("keyboardKey_control_" + kkc.getKey().name()), kkc).getElement();
case GamepadButtonControl gpbc -> controlRegistry.register(ClientBase.getInstance().identifierOf("gamepadButton_control_" + gpbc.getButton().name()), gpbc).getElement();
case MouseButtonControl mbc -> controlRegistry.register(ClientBase.getInstance().identifierOf("mouseButton_control_" + mbc.getButton().name()), mbc).getElement();
case GamepadAxisControl gpac -> controlRegistry.register(ClientBase.getInstance().identifierOf("gamepadAxis_control_" + gpac.getAxis().name()), gpac).getElement();
case MouseMovementControl mmc -> controlRegistry.register(ClientBase.getInstance().identifierOf("mouseMovement_control_" + mmc.getAxis().name()), mmc).getElement();
case MouseScrollControl msc -> controlRegistry.register(ClientBase.getInstance().identifierOf("mouseScroll_control_" + msc.getDirection().name()), msc).getElement();
};
}

Expand All @@ -141,6 +141,6 @@ public Control registerControlListener(Control control) {
* @return The Controller which was registered
*/
public Controller registerController(Identifier identifier, Controller controller) {
return controllerRegistry.register(identifier, controller).getValue();
return controllerRegistry.register(identifier, controller).getElement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private int getNumIndices() {
}

/**
* @return The opengl id given to this mesh
* @return The opengl identifier given to this mesh
*/
public final int getVaoId() {
return vaoId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private String parseInclusions(String source) {
}

/**
* @return The shader id for this shader once created
* @return The shader identifier for this shader once created
*/
public int create() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void cleanup() {
}

/**
* @return The OpenGL id of this shader program
* @return The OpenGL identifier of this shader program
*/
public int getProgramID() {
return programID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void setManager(Manager manager) {

public <T extends Component> T addComponent(Class<T> componentClass) {
if (containsComponent(componentClass)) {
Log.warn("Tried to add component " + componentClass.getName() + " to entity with id " + getID() + " which already contains it");
Log.warn("Tried to add component " + componentClass.getName() + " to entity with identifier " + getID() + " which already contains it");
}
components.put(componentClass, manager.obtainComponent(componentClass, this));
manager.invalidateQueryCacheForComponents(componentClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.terminalvelocitycabbage.engine.registry;

public interface Identifiable {

public Identifier getIdentifier();

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public String getName() {
return name;
}

public static boolean isValidIdentifierString(String identifier) {
if (!identifier.contains(":")) return false;
return identifier.split(":").length == 2;
}

@Override
public String toString() {
return namespace + ':' + name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.terminalvelocitycabbage.engine.debug.Log;

import java.util.*;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Set;

public class Registry<T> {

Expand All @@ -26,6 +28,25 @@ public Registry() {
this(null);
}

/**
* Registers an item to this registry for retrieval later by its identifier or name
* @param item The item to be registered (if it extends Identifiable)
*/
public RegistryPair<T> register(T item) {
if (item instanceof Identifiable identifiable) {
var identifier = identifiable.getIdentifier();
if (registryContents.containsKey(identifier)) {
Log.warn("Tried to register item of same identifier " + identifier.toString() + " twice, the second addition has been ignored.");
return null;
}
registryContents.put(identifier, item);
return new RegistryPair<>(identifier, item);
} else {
Log.warn("Tried to register item of type " + item.getClass().getName() + " which does not implement Identifiable, the item will not be registered. Try registering with a declared Identifier instead of implicit with this method");
return null;
}
}

/**
* Registers an item to this registry for retrieval later by its identifier or name
* @param identifier The identifier of this registered item
Expand Down Expand Up @@ -64,6 +85,38 @@ public T get(Identifier identifier) {
return registryContents.get(identifier);
}

/**
* @param identifier The key identifier that is requested
* @return a boolean of true if it exists in this registry or false if not
*/
public boolean contains(Identifier identifier) {
return registryContents.containsKey(identifier);
}

/**
* @param namespace the namespace (portion before the : in an identifier) to search this registry for
* @return All identifiers in this registry with that namespace
*/
public Set<Identifier> getIdentifiersWithNamespace(String namespace) {
Set<Identifier> identifiers = new LinkedHashSet<>();
registryContents.keySet().forEach(identifier -> {
if (identifier.getNamespace().equals(namespace)) identifiers.add(identifier);
});
return identifiers;
}

/**
* @param name the name (portion after the : in an identifier) to search this registry for
* @return All identifiers in this registry with that name
*/
public Set<Identifier> getIdentifiersWithName(String name) {
Set<Identifier> identifiers = new LinkedHashSet<>();
registryContents.keySet().forEach(identifier -> {
if (identifier.getName().equals(name)) identifiers.add(identifier);
});
return identifiers;
}

public LinkedHashMap<Identifier, T> getRegistryContents() {
return registryContents;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Identifier getIdentifier() {
/**
* @return The value associated with this registration
*/
public T getValue() {
public T getElement() {
return getValue1();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.terminalvelocitycabbage.engine.scripting.api;

import com.terminalvelocitycabbage.engine.scripting.parser.ExecutionContext;

import java.util.Map;

public final class ActionContext {

private final Map<String, Object> arguments;
private final ExecutionContext executionContext;

public ActionContext(
Map<String, Object> arguments,
ExecutionContext executionContext
) {
this.arguments = arguments;
this.executionContext = executionContext;
}

@SuppressWarnings("unchecked")
public <T> T get(String name) {
return (T) arguments.get(name);
}

public ExecutionContext execution() {
return executionContext;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.terminalvelocitycabbage.engine.scripting.api;

@FunctionalInterface
public interface ActionExecutor {
void execute(ActionContext context);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.terminalvelocitycabbage.engine.scripting.api;

import java.util.function.BiConsumer;
import java.util.function.Function;

public sealed interface PropertyAccess<O, V>
permits PropertyAccess.ReadOnly, PropertyAccess.ReadWrite {

V get(O instance);

final class ReadOnly<O, V> implements PropertyAccess<O, V> {

private final Function<O, V> getter;

public ReadOnly(Function<O, V> getter) {
this.getter = getter;
}

@Override
public V get(O instance) {
return getter.apply(instance);
}
}

final class ReadWrite<O, V> implements PropertyAccess<O, V> {

private final Function<O, V> getter;
private final BiConsumer<O, V> setter;

public ReadWrite(Function<O, V> getter, BiConsumer<O, V> setter) {
this.getter = getter;
this.setter = setter;
}

@Override
public V get(O instance) {
return getter.apply(instance);
}

// setter omitted for now
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.terminalvelocitycabbage.engine.scripting.api;

import com.terminalvelocitycabbage.engine.registry.Identifier;
import com.terminalvelocitycabbage.engine.scripting.api.syntax.SyntaxPattern;

import java.util.List;

public final class ScriptAction {

private final Identifier id;
private final List<SyntaxPattern> patterns;
private final ScriptType returnType;
private final ActionExecutor executor;
private final String documentation;

public ScriptAction(
Identifier id,
List<SyntaxPattern> patterns,
ScriptType returnType,
ActionExecutor executor,
String documentation
) {
this.id = id;
this.patterns = patterns;
this.returnType = returnType;
this.executor = executor;
this.documentation = documentation;
}

public Identifier id() {
return id;
}

public List<SyntaxPattern> patterns() {
return patterns;
}

public ScriptType returnType() {
return returnType;
}

public ActionExecutor executor() {
return executor;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.terminalvelocitycabbage.engine.scripting.api;

import com.terminalvelocitycabbage.engine.registry.Identifiable;
import com.terminalvelocitycabbage.engine.registry.Identifier;

public record ScriptConstant(Identifier identifier, ScriptType type, Object value, String documentation) implements Identifiable {

@Override
public Identifier getIdentifier() {
return identifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.terminalvelocitycabbage.engine.scripting.api;

import com.terminalvelocitycabbage.engine.registry.Identifiable;
import com.terminalvelocitycabbage.engine.registry.Identifier;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;

public record ScriptEvent<E>(
Identifier identifier,
Class<?> eventClass,
Map<String, ScriptEventValue<E, ?>> exposedValues,
String documentation) implements Identifiable {

public static <E> ScriptEventBuilder<E> builder() {
return new ScriptEventBuilder<>();
}

public static final class ScriptEventBuilder<E> {

private Identifier id;
private Class<E> eventClass;
private final Map<String, ScriptEventValue> values = new LinkedHashMap<>();
private String documentation = "";

public ScriptEventBuilder<E> id(Identifier id) {
this.id = id;
return this;
}

public ScriptEventBuilder<E> eventClass(Class<E> eventClass) {
this.eventClass = eventClass;
return this;
}

public <T> ScriptEventBuilder<E> exposedValue(
String name,
ScriptType type,
Function<E, T> extractor
) {
values.put(
name,
new ScriptEventValue(
name,
type,
(Function<Object, Object>) extractor
)
);
return this;
}

public ScriptEventBuilder<E> doc(String documentation) {
this.documentation = documentation;
return this;
}

public ScriptEvent build() {
if (id == null)
throw new IllegalStateException("ScriptEvent identifier is required");
if (eventClass == null)
throw new IllegalStateException("ScriptEvent eventClass is required");

return new ScriptEvent(
id,
eventClass,
Map.copyOf(values),
documentation
);
}
}


@Override
public Identifier getIdentifier() {
return identifier;
}
}
Loading