Skip to content

Commit

Permalink
Placing objects in level with mouse.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinSc committed Sep 9, 2016
1 parent 6c78f8e commit 64ec8ad
Show file tree
Hide file tree
Showing 22 changed files with 308 additions and 112 deletions.
8 changes: 7 additions & 1 deletion core/assets/prefabs/objects/bug.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,11 @@
"FactionMemberComponent": {
"factionName": "bug"
},
"PlatformMovableComponent": {}
"PlatformMovableComponent": {},
"ObjectInEditorComponent": {
"displayName": "Bug",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0",
"placementTranslate": "0.5,0,0.5"
}
}
8 changes: 7 additions & 1 deletion core/assets/prefabs/objects/bugWithGun.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@
"projectileShootFrequency": 3000,
"projectileSpeed": 6
},
"PlatformMovableComponent": {}
"PlatformMovableComponent": {},
"ObjectInEditorComponent": {
"displayName": "Bug with gun",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0",
"placementTranslate": "0.5,0,0.5"
}
}
7 changes: 6 additions & 1 deletion core/assets/prefabs/objects/cycleDoor.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
"interpolationAway": "linear",
"interpolationBack": "bounceOut"
},
"BaseLocationComponent": {}
"BaseLocationComponent": {},
"ObjectInEditorComponent": {
"displayName": "Cycle door",
"renderSize": "0.2,2,1",
"renderTranslate": "0.4,0,0"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/lever.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@
"G3DModelActivateComponent": {
"activateAnimation": "Armature|TurnOff",
"deactivateAnimation": "Armature|TurnOn"
},
"ObjectInEditorComponent": {
"displayName": "Lever",
"renderSize": "0.6,0.6,0.2",
"renderTranslate": "0.2,0,0.8"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/pressurePlate.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"translateY": 1,
"width": 1,
"height": 0.1
},
"ObjectInEditorComponent": {
"displayName": "Pressure plate",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0"
}
}
7 changes: 6 additions & 1 deletion core/assets/prefabs/objects/signalDoor.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@
"moveTime": 1500,
"interpolation": "linear"
},
"BaseLocationComponent": {}
"BaseLocationComponent": {},
"ObjectInEditorComponent": {
"displayName": "Signal door",
"renderSize": "0.2,2,1",
"renderTranslate": "0.4,0,0"
}
}
7 changes: 6 additions & 1 deletion core/assets/prefabs/objects/signalIndicator.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"signalOnGreen": 127,
"signalOnBlue": 0
},
"SignalConsumerComponent": {}
"SignalConsumerComponent": {},
"ObjectInEditorComponent": {
"displayName": "Signal indicator",
"renderSize": "0.2,0.2,0.2",
"renderTranslate": "0.4,0.4,0"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/signalStonePlatform.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
"translateY": 0.5,
"width": 2,
"height": 0.1
},
"ObjectInEditorComponent": {
"displayName": "Signal stone platform",
"renderSize": "2,0.5,1",
"renderTranslate": "0,0,0"
}
}
6 changes: 6 additions & 0 deletions core/assets/prefabs/objects/star.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
"ModelRotateComponent": {},
"ConstantModelRotationComponent": {
"degreesPerSecond": 180
},
"ObjectInEditorComponent": {
"displayName": "Star",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0",
"placementTranslate": "0.5,0,0.5"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/stoneBack.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"MoveActivatorComponent": {
"distanceZ": 1,
"moveTime": 1000
},
"ObjectInEditorComponent": {
"displayName": "Stone to front",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/stoneFront.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"MoveActivatorComponent": {
"distanceZ": -1,
"moveTime": 1000
},
"ObjectInEditorComponent": {
"displayName": "Stone to back",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/stonePlatform.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@
"translateY": 0.5,
"width": 2,
"height": 0.1
},
"ObjectInEditorComponent": {
"displayName": "Stone platform",
"renderSize": "2,0.5,1",
"renderTranslate": "0,0,0"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/tombstone.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
"prefabName": "ghost",
"distanceZ": 0.5,
"distanceX": 0.5
},
"ObjectInEditorComponent": {
"displayName": "Tombstone",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0"
}
}
5 changes: 5 additions & 0 deletions core/assets/prefabs/objects/torch.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
"green": 255,
"blue": 127,
"intensity": 5
},
"ObjectInEditorComponent": {
"displayName": "Torch",
"renderSize": "1,1,1",
"renderTranslate": "0,0,0"
}
}
1 change: 1 addition & 0 deletions core/src/main/java/jgd/platformer/Platformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public void render() {
public void resize(int width, int height) {
menuContext.getSystem(RenderingEngine.class).screenResized(width, height);
gameplayContext.getSystem(RenderingEngine.class).screenResized(width, height);
editorContext.getSystem(RenderingEngine.class).screenResized(width, height);
}

@Override
Expand Down
116 changes: 114 additions & 2 deletions core/src/main/java/jgd/platformer/editor/EditorLogicSystem.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,119 @@
package jgd.platformer.editor;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.gempukku.gaming.rendering.event.RenderEnvironment;
import com.gempukku.secsy.context.annotation.Inject;
import com.gempukku.secsy.context.annotation.RegisterSystem;
import com.gempukku.secsy.context.system.LifeCycleSystem;
import com.gempukku.secsy.entity.EntityRef;
import com.gempukku.secsy.entity.dispatch.ReceiveEvent;
import jgd.platformer.editor.controls.MouseTracked;
import jgd.platformer.editor.controls.PlaceObject;
import jgd.platformer.editor.ui.BlockTypeSelected;
import jgd.platformer.editor.ui.ObjectTypeSelected;
import jgd.platformer.gameplay.level.LevelComponent;
import jgd.platformer.gameplay.logic.spawning.PlatformerEntitySpawner;
import jgd.platformer.gameplay.rendering.platform.RebuildBlockMesh;

import java.util.Collections;
import java.util.Map;

@RegisterSystem(
profiles = {"gameScreen", "editor"}
)
public class EditorLogicSystem {
public class EditorLogicSystem implements LifeCycleSystem {
@Inject
private PlatformerEntitySpawner platformerEntitySpawner;

private Vector3 lastMousePosition = new Vector3();
private float snapSize = 0.25f;

private String lastSelectedBlockType;
private ObjectTypeSelected lastSelectedObjectType;

private Model model;
private ModelInstance modelInstance;

private ModelBatch modelBatch;

@Override
public void initialize() {
float size = 1;

float[] vertices = {
0, 0, 0,
0, 0, size,
0, size, 0,
0, size, size,
size, 0, 0,
size, 0, size,
size, size, 0,
size, size, size};

short[] indices = {
0, 1,
0, 2,
1, 3,
2, 3,

4, 5,
4, 6,
5, 7,
6, 7,

0, 4,
1, 5,
2, 6,
3, 7};

Mesh mesh = new Mesh(true, vertices.length / 3, indices.length, VertexAttribute.Position());
mesh.setVertices(vertices);
mesh.setIndices(indices);

ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
modelBuilder.part("cube", mesh, GL20.GL_LINES, new Material(ColorAttribute.createDiffuse(1, 1, 1, 1)));
model = modelBuilder.end();
modelInstance = new ModelInstance(model);
modelBatch = new ModelBatch();
}

@ReceiveEvent
public void renderCube(RenderEnvironment event, EntityRef entityRef) {
if (lastMousePosition != null) {
if (lastSelectedBlockType != null) {
modelInstance.transform.idt().translate(lastMousePosition);
} else if (lastSelectedObjectType != null) {
modelInstance.transform.idt().translate(lastMousePosition)
.translate(lastSelectedObjectType.getRenderTranslate()).scl(lastSelectedObjectType.getRenderSize());
}

event.getRenderPipeline().getCurrentBuffer().begin();
modelBatch.begin(event.getCamera());
modelBatch.render(modelInstance);
modelBatch.end();
event.getRenderPipeline().getCurrentBuffer().end();
}
}

@ReceiveEvent
public void mouseTracked(MouseTracked mouseTracked, EntityRef entityRef) {
Vector3 position = mouseTracked.getPosition();
boolean snap = mouseTracked.isSnap();
if (snap)
lastMousePosition.set(MathUtils.round(position.x - 0.5f), MathUtils.round(position.y - 0.5f), MathUtils.round(position.z));
lastMousePosition.set(MathUtils.round((position.x - 0.5f) / snapSize) * snapSize,
MathUtils.round((position.y - 0.5f) / snapSize) * snapSize,
MathUtils.round(position.z / snapSize) * snapSize);
else
lastMousePosition.set(position.x - 0.5f, position.y - 0.5f, position.z);
}
Expand All @@ -35,6 +123,15 @@ public void blockTypeSelected(BlockTypeSelected blockTypeSelected, EntityRef ent
lastSelectedBlockType = blockTypeSelected.getPrefabName();
}

@ReceiveEvent
public void objectTypeSelected(ObjectTypeSelected objectTypeSelected, EntityRef entityRef) {
if (objectTypeSelected.getPrefabName() != null) {
lastSelectedObjectType = objectTypeSelected;
} else {
lastSelectedObjectType = null;
}
}

@ReceiveEvent
public void placeObject(PlaceObject placeObject, EntityRef entityRef, LevelComponent levelComponent) {
if (lastSelectedBlockType != null) {
Expand All @@ -47,5 +144,20 @@ public void placeObject(PlaceObject placeObject, EntityRef entityRef, LevelCompo

entityRef.send(new RebuildBlockMesh());
}
if (lastSelectedObjectType != null) {
Map<String, Object> objectCoordinates = levelComponent.getObjectCoordinates();
Vector3 placementTranslate = lastSelectedObjectType.getPlacementTranslate();
float x = lastMousePosition.x + placementTranslate.x;
float y = lastMousePosition.y + placementTranslate.y;
float z = lastMousePosition.z + placementTranslate.z;
String position = x + "," + y + "," + z;
String prefabName = lastSelectedObjectType.getPrefabName();
objectCoordinates.put(position, prefabName);

levelComponent.setObjectCoordinates(objectCoordinates);
entityRef.saveChanges();

platformerEntitySpawner.createEntityAt(x, y, z, prefabName, Collections.emptyMap());
}
}
}
Loading

0 comments on commit 64ec8ad

Please sign in to comment.