diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java b/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java index 16eab6c..2093291 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java @@ -26,12 +26,14 @@ import models.dataFlowModel.DataTransferChannel; import models.dataFlowModel.DataTransferChannel.IResourceStateAccessor; import simulator.Event.IResourceStateValueProvider; +import simulator.interfaces.INativeInitializer; import simulator.interfaces.INativeReceiver; public class Simulator { private DataTransferModel model; private SystemState curState; private List systemReceivers = new ArrayList<>(); + private List systemInitializers = new ArrayList<>(); private Map> nativeReceivers = new HashMap<>(); private Map nativeChannelReceivers = new HashMap<>(); @@ -63,6 +65,9 @@ for (Channel channel: model.getChannels()) { curState.addChannel((DataTransferChannel) channel); } + for (INativeInitializer initializer: systemInitializers) { + initializer.onInitFromModel(curState); + } return curState; } @@ -100,6 +105,10 @@ systemReceivers.add(receiver); } + public void addSystemInitializer(INativeInitializer initializer) { + systemInitializers.add(initializer); + } + public void addNativeReceiver(INativeReceiver receiver, DataTransferChannel channel) { nativeChannelReceivers.put(channel, receiver); } @@ -117,6 +126,10 @@ systemReceivers.remove(receiver); } + public void removeSystemInitializer(INativeInitializer initializer) { + systemInitializers.remove(initializer); + } + public void removeNativeReceiver(DataTransferChannel channel) { nativeChannelReceivers.remove(channel); } diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/INativeInitializer.java b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/INativeInitializer.java new file mode 100644 index 0000000..3117943 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/INativeInitializer.java @@ -0,0 +1,7 @@ +package simulator.interfaces; + +import simulator.SystemState; + +public interface INativeInitializer { + public void onInitFromModel(SystemState initialSystemState); +} diff --git a/GameEngine/src/main/java/gameEngine/GameEngineModelFileGenerator.java b/GameEngine/src/main/java/gameEngine/GameEngineModelFileGenerator.java index 3b4cf32..90cdc60 100644 --- a/GameEngine/src/main/java/gameEngine/GameEngineModelFileGenerator.java +++ b/GameEngine/src/main/java/gameEngine/GameEngineModelFileGenerator.java @@ -98,17 +98,17 @@ TypeInference.infer(model); Simulator simulator = new Simulator(model); - SystemState initialState = simulator.init(); Window window = Window.get(); window.changeScene(1); GameEnginePresenter presenter = new GameEnginePresenter(window.getScene(), simulator); TimerService timerService = new TimerService(simulator); - //simulator.addNativeReceiver(presenter, sceneUpdateChannel); + + SystemState initialState = simulator.init(); - stream.addLine("updateCameraPosition(0.0,0.0,0.0)"); - Expression messageUpdateCPos = parser.parseTerm(stream, model); - Event updateCameraPos = new Event(updateCameraPosChannel, messageUpdateCPos, scene_camera_transform_position, initialState.getResource(ResourceIdentifier.createFrom(scene_camera_transform_position))); - simulator.transition(updateCameraPos); +// stream.addLine("updateCameraPosition(0.0,0.0,0.0)"); +// Expression messageUpdateCPos = parser.parseTerm(stream, model); +// Event updateCameraPos = new Event(updateCameraPosChannel, messageUpdateCPos, scene_camera_transform_position, initialState.getResource(ResourceIdentifier.createFrom(scene_camera_transform_position))); +// simulator.transition(updateCameraPos); } catch (Exception e) { e.printStackTrace(); diff --git a/GameEngine/src/main/java/gameEngine/simulator/interfaces/GameEnginePresenter.java b/GameEngine/src/main/java/gameEngine/simulator/interfaces/GameEnginePresenter.java index 0c41e16..65f74e4 100644 --- a/GameEngine/src/main/java/gameEngine/simulator/interfaces/GameEnginePresenter.java +++ b/GameEngine/src/main/java/gameEngine/simulator/interfaces/GameEnginePresenter.java @@ -12,6 +12,7 @@ import gameEngine.views.Sprite; import models.algebra.Expression; import models.algebra.Term; +import models.algebra.Constant; import models.dataConstraintModel.JsonTerm; import models.dataConstraintModel.MapTerm; import models.dataFlowModel.DataTransferChannel; @@ -19,9 +20,10 @@ import simulator.Resource; import simulator.Simulator; import simulator.SystemState; +import simulator.interfaces.INativeInitializer; import simulator.interfaces.INativeReceiver; -public class GameEnginePresenter implements INativeReceiver { +public class GameEnginePresenter implements INativeReceiver, INativeInitializer { public final String sceneUpdateChannelName = "SceneUpdate"; public final String cameraPositionUpdateChannelName = "CameraPositionUpdate"; public final String cameraRotationUpdateChannelName = "CameraRotationUpdate"; @@ -71,6 +73,44 @@ sceneUpdateEventChannel = (DataTransferChannel) simulator.getModel().getInputChannel(sceneUpdateEventChannelName); keyEventChannel = (DataTransferChannel) simulator.getModel().getInputChannel(keyEventChannelName); simulator.addNativeReceiver(this, sceneUpdateChannel); + simulator.addSystemInitializer(this); + } + + @Override + public void onInitFromModel(SystemState initialSystemState) { + Resource sceneResource = initialSystemState.getResource("scene"); + if (sceneResource != null) { + Resource entitiesResource = sceneResource.getChildrenMap().get("entities"); + if (entitiesResource != null) { + for (Map.Entry entity: entitiesResource.getChildrenMap().entrySet()) { + String eId = entity.getKey(); + Resource entityResource = entity.getValue(); + Resource transformResource = entityResource.getChildrenMap().get("transform"); + Resource meshResource = entityResource.getChildrenMap().get("mesh"); + Resource positionResource = transformResource.getChildrenMap().get("position"); + Resource rotationResource = transformResource.getChildrenMap().get("rotation"); + Resource scaleResource = transformResource.getChildrenMap().get("scale"); + if (meshResource != null && positionResource != null && rotationResource != null && scaleResource != null) { + // Add a game entity to scene. + String px = positionResource.getChildrenMap().get("x").getState().getValue().toString(); + String py = positionResource.getChildrenMap().get("y").getState().getValue().toString(); + String pz = positionResource.getChildrenMap().get("z").getState().getValue().toString(); + String rx = rotationResource.getChildrenMap().get("x").getState().getValue().toString(); + String ry = rotationResource.getChildrenMap().get("y").getState().getValue().toString(); + String rz = rotationResource.getChildrenMap().get("z").getState().getValue().toString(); + String sx = scaleResource.getChildrenMap().get("x").getState().getValue().toString(); + String sy = scaleResource.getChildrenMap().get("y").getState().getValue().toString(); + String sz = scaleResource.getChildrenMap().get("z").getState().getValue().toString(); + String type = (String) ((Constant) meshResource.getChildrenMap().get("type").getState().getValue()).getValue(); + String texturePath = (String) ((Constant) meshResource.getChildrenMap().get("sprite").getState().getValue()).getValue(); + GameObject gameObj = addGameObjectToScene(eId, meshResource, px, py, pz, rx, ry, rz, sx, sy, sz, type, texturePath); + + // Connect game entity and model. + connectGameEntiotyAndModel(eId, gameObj, positionResource, rotationResource, scaleResource); + } + } + } + } } @Override @@ -137,8 +177,8 @@ for (String oldEid: oldEidSet) { Resource resource = widToResource.remove(oldEid); if (resource != null) { - - } simulator.removeNativeReceiver(channel, resource); + simulator.removeNativeReceiver(channel, resource); + } } } @@ -165,63 +205,25 @@ if (positionExp instanceof JsonTerm && rotationExp instanceof JsonTerm && scaleExp instanceof JsonTerm) { + // Add a game entity to scene. JsonTerm position = (JsonTerm) positionExp; JsonTerm rotation = (JsonTerm) rotationExp; JsonTerm scale = (JsonTerm) scaleExp; - GameObject gameObj = new GameObject(newEid);//新しいIDが必要あり - gameObj.transform.setPosition(Float.parseFloat(position.get("x").toString()), - Float.parseFloat(position.get("y").toString()), - Float.parseFloat(position.get("z").toString())); - gameObj.transform.setRotation(Float.parseFloat(rotation.get("x").toString()), - Float.parseFloat(rotation.get("y").toString()), - Float.parseFloat(rotation.get("z").toString())); - gameObj.transform.setScale(Float.parseFloat(scale.get("x").toString()), - Float.parseFloat(scale.get("y").toString()), - Float.parseFloat(scale.get("z").toString())); - Expression type = mesh.get("type"); - if (type.toString().equals("\"sprite\"")) { - String texturePath = mesh.get("sprite").toString().replace("\"", ""); - Mesh meshObj = new Mesh(gameObj, Mesh.MeshType.SPRITE, texturePath); - gameObj.addComponent(meshObj); - SpriteReceiver nativeSpriteReceiver = new SpriteReceiver(meshObj); - simulator.addNativeReceiver(nativeSpriteReceiver, spriteUpdateChannel, meshResource); - - Map resources = channelAndResourcesForEntityReceiving.get(spriteUpdateChannel); - if (resources == null) { - resources = new HashMap<>(); - channelAndResourcesForEntityReceiving.put(spriteUpdateChannel, resources); - } - resources.put(newEid, meshResource); - } - scene.addEntity(newEid, gameObj); + String px = position.get("x").toString(); + String py = position.get("y").toString(); + String pz = position.get("z").toString(); + String rx = rotation.get("x").toString(); + String ry = rotation.get("y").toString(); + String rz = rotation.get("z").toString(); + String sx = scale.get("x").toString(); + String sy = scale.get("y").toString(); + String sz = scale.get("z").toString(); + String type = (String) ((Constant) mesh.get("type")).getValue(); + String texturePath = (String) ((Constant) mesh.get("sprite")).getValue(); + GameObject gameObj = addGameObjectToScene(newEid, meshResource, px, py, pz, rx, ry, rz, sx, sy, sz, type, texturePath); // Connect game entity and model. - EntityPositionReceiver nativePositionReceiver = new EntityPositionReceiver(gameObj); - simulator.addNativeReceiver(nativePositionReceiver, entityPositionUpdateChannel, positionResource); - Map resources = channelAndResourcesForEntityReceiving.get(entityPositionUpdateChannel); - if (resources == null) { - resources = new HashMap<>(); - channelAndResourcesForEntityReceiving.put(entityPositionUpdateChannel, resources); - } - resources.put(newEid, positionResource); - - EntityRotationReceiver nativeRotationReceiver = new EntityRotationReceiver(gameObj); - simulator.addNativeReceiver(nativeRotationReceiver, entityRotationUpdateChannel, rotationResource); - resources = channelAndResourcesForEntityReceiving.get(entityRotationUpdateChannel); - if (resources == null) { - resources = new HashMap<>(); - channelAndResourcesForEntityReceiving.put(entityRotationUpdateChannel, resources); - } - resources.put(newEid, rotationResource); - - EntityScaleReceiver nativeScaleReceiver = new EntityScaleReceiver(gameObj); - simulator.addNativeReceiver(nativeScaleReceiver, entityScaleUpdateChannel, scaleResource); - resources = channelAndResourcesForEntityReceiving.get(entityScaleUpdateChannel); - if (resources == null) { - resources = new HashMap<>(); - channelAndResourcesForEntityReceiving.put(entityScaleUpdateChannel, resources); - } - resources.put(newEid, scaleResource); + connectGameEntiotyAndModel(newEid, gameObj, positionResource, rotationResource, scaleResource); } } } @@ -230,4 +232,56 @@ } } } + + private GameObject addGameObjectToScene(String eId, Resource meshResource, String px, String py, String pz, + String rx, String ry, String rz, String sx, String sy, String sz, String type, String texturePath) { + GameObject gameObj = new GameObject(eId);//新しいIDが必要あり + gameObj.transform.setPosition(Float.parseFloat(px), Float.parseFloat(py), Float.parseFloat(pz)); + gameObj.transform.setRotation(Float.parseFloat(rx), Float.parseFloat(ry), Float.parseFloat(rz)); + gameObj.transform.setScale(Float.parseFloat(sx), Float.parseFloat(sy), Float.parseFloat(sz)); + if (type.equals("sprite")) { + Mesh meshObj = new Mesh(gameObj, Mesh.MeshType.SPRITE, texturePath); + gameObj.addComponent(meshObj); + SpriteReceiver nativeSpriteReceiver = new SpriteReceiver(meshObj); + simulator.addNativeReceiver(nativeSpriteReceiver, spriteUpdateChannel, meshResource); + Map resources = channelAndResourcesForEntityReceiving.get(spriteUpdateChannel); + if (resources == null) { + resources = new HashMap<>(); + channelAndResourcesForEntityReceiving.put(spriteUpdateChannel, resources); + } + resources.put(eId, meshResource); + } + scene.addEntity(eId, gameObj); + return gameObj; + } + + private void connectGameEntiotyAndModel(String eId, GameObject gameObj, Resource positionResource, + Resource rotationResource, Resource scaleResource) { + EntityPositionReceiver nativePositionReceiver = new EntityPositionReceiver(gameObj); + simulator.addNativeReceiver(nativePositionReceiver, entityPositionUpdateChannel, positionResource); + Map resources = channelAndResourcesForEntityReceiving.get(entityPositionUpdateChannel); + if (resources == null) { + resources = new HashMap<>(); + channelAndResourcesForEntityReceiving.put(entityPositionUpdateChannel, resources); + } + resources.put(eId, positionResource); + + EntityRotationReceiver nativeRotationReceiver = new EntityRotationReceiver(gameObj); + simulator.addNativeReceiver(nativeRotationReceiver, entityRotationUpdateChannel, rotationResource); + resources = channelAndResourcesForEntityReceiving.get(entityRotationUpdateChannel); + if (resources == null) { + resources = new HashMap<>(); + channelAndResourcesForEntityReceiving.put(entityRotationUpdateChannel, resources); + } + resources.put(eId, rotationResource); + + EntityScaleReceiver nativeScaleReceiver = new EntityScaleReceiver(gameObj); + simulator.addNativeReceiver(nativeScaleReceiver, entityScaleUpdateChannel, scaleResource); + resources = channelAndResourcesForEntityReceiving.get(entityScaleUpdateChannel); + if (resources == null) { + resources = new HashMap<>(); + channelAndResourcesForEntityReceiving.put(entityScaleUpdateChannel, resources); + } + resources.put(eId, scaleResource); + } }