-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
engine/src/main/java/org/terasology/logic/behavior/asset/Group.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2019 MovingBlocks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.terasology.logic.behavior.asset; | ||
|
||
import org.terasology.assets.Asset; | ||
import org.terasology.assets.AssetType; | ||
import org.terasology.assets.ResourceUrn; | ||
import org.terasology.assets.module.annotations.RegisterAssetType; | ||
import org.terasology.module.sandbox.API; | ||
|
||
/** | ||
* The main Group asset class. This is the reference type | ||
* used by the asset manager to find all groups loaded | ||
* from .group files. The annotation below indicates that | ||
* all .group files will be under the assets/groups folder. | ||
* A factory class is used to assist the asset creation. | ||
* Using the Group asset is illustrated in the | ||
* WildAnimalsMadness module. | ||
*/ | ||
@RegisterAssetType(folderName = "groups", factoryClass = GroupFactory.class) | ||
public class Group extends Asset<GroupData> { | ||
|
||
private GroupData groupData; | ||
|
||
public Group(ResourceUrn urn, GroupData data, AssetType<?, GroupData> type) { | ||
super(urn, type); | ||
reload(data); | ||
} | ||
|
||
@Override | ||
protected void doReload(GroupData data) { | ||
this.groupData = data; | ||
} | ||
|
||
public GroupData getGroupData() { | ||
return groupData; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
engine/src/main/java/org/terasology/logic/behavior/asset/GroupBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2019 MovingBlocks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.terasology.logic.behavior.asset; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonIOException; | ||
import com.google.gson.JsonSyntaxException; | ||
import org.terasology.module.sandbox.API; | ||
import org.terasology.registry.In; | ||
|
||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
/** | ||
* This class is responsible for implementing the | ||
* serialization functionality required by the Group | ||
* asset class to load group information from .group | ||
* asset files. | ||
* @see Group | ||
*/ | ||
@API | ||
public class GroupBuilder { | ||
|
||
private GroupData groupData; | ||
|
||
@In | ||
private Gson gson; | ||
|
||
public GroupData loadFromJson(InputStream json) { | ||
|
||
GroupData groupFromFile = new GroupData(); | ||
try { | ||
gson = new Gson(); | ||
groupFromFile = gson.fromJson(new InputStreamReader(json), GroupData.class); | ||
} catch (JsonSyntaxException e) { | ||
groupFromFile.setGroupLabel("ERROR - Syntax"); | ||
e.printStackTrace(); | ||
} catch (JsonIOException e) { | ||
groupFromFile.setGroupLabel("ERROR - IO"); | ||
e.printStackTrace(); | ||
} | ||
return groupFromFile; | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
engine/src/main/java/org/terasology/logic/behavior/asset/GroupData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2019 MovingBlocks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.terasology.logic.behavior.asset; | ||
|
||
import org.terasology.assets.AssetData; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Every Group asset is described by a GroupData class. | ||
* @see Group | ||
*/ | ||
public class GroupData implements AssetData { | ||
|
||
/** | ||
* The unique group identifier | ||
*/ | ||
public String groupLabel; | ||
|
||
/** | ||
* Flags the need for a hivemind | ||
* structure (when group members | ||
* need to behave in unison) | ||
*/ | ||
public Boolean needsHive; | ||
|
||
/** | ||
* The name of the behavior tree | ||
* used by the group (trees | ||
* from other modules can be used as | ||
* long as they are listed as | ||
* dependencies) | ||
*/ | ||
public String behavior; | ||
|
||
public GroupData() { | ||
} | ||
|
||
public GroupData(String groupLabel, Boolean needsHive, String behavior) { | ||
this.groupLabel = groupLabel; | ||
this.needsHive = needsHive; | ||
this.behavior = behavior; | ||
} | ||
|
||
public String getGroupLabel() { | ||
return groupLabel; | ||
} | ||
|
||
public void setGroupLabel(String groupLabel) { | ||
this.groupLabel = groupLabel; | ||
} | ||
|
||
public Boolean getNeedsHive() { | ||
return needsHive; | ||
} | ||
|
||
public void setNeedsHive(Boolean needsHive) { | ||
this.needsHive = needsHive; | ||
} | ||
|
||
public String getBehavior() { | ||
return behavior; | ||
} | ||
|
||
public void setBehavior(String behavior) { | ||
this.behavior = behavior; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
engine/src/main/java/org/terasology/logic/behavior/asset/GroupFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2019 MovingBlocks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.terasology.logic.behavior.asset; | ||
|
||
import org.terasology.assets.AssetFactory; | ||
import org.terasology.assets.AssetType; | ||
import org.terasology.assets.ResourceUrn; | ||
|
||
/** | ||
* Factory class used to assist the creation | ||
* of new Group assets. | ||
* @see Group | ||
*/ | ||
public class GroupFactory implements AssetFactory<Group, GroupData> { | ||
|
||
@Override | ||
public Group build(ResourceUrn urn, AssetType<Group, GroupData> type, GroupData data) { | ||
return new Group(urn, data, type); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
engine/src/main/java/org/terasology/logic/behavior/asset/GroupFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2019 MovingBlocks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.terasology.logic.behavior.asset; | ||
|
||
import org.terasology.assets.ResourceUrn; | ||
import org.terasology.assets.format.AbstractAssetFileFormat; | ||
import org.terasology.assets.format.AssetDataFile; | ||
import org.terasology.assets.module.annotations.RegisterAssetFileFormat; | ||
import org.terasology.registry.CoreRegistry; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
/** | ||
* This class is responsible for | ||
* associating the correct file extensions | ||
* (.group) with the Group asset class. | ||
* It is also the actual responsible | ||
* for invoking the serialization method | ||
* from the builder class. | ||
* @see Group | ||
* @see GroupBuilder | ||
*/ | ||
@RegisterAssetFileFormat | ||
public class GroupFormat extends AbstractAssetFileFormat<GroupData> { | ||
|
||
public GroupFormat() { | ||
super("group"); | ||
} | ||
|
||
@Override | ||
public GroupData load(ResourceUrn resourceUrn, List<AssetDataFile> list) throws IOException { | ||
GroupBuilder builder = CoreRegistry.get(GroupBuilder.class); | ||
if (builder == null) { | ||
builder = new GroupBuilder(); | ||
CoreRegistry.put(GroupBuilder.class, builder); | ||
} | ||
|
||
return builder.loadFromJson(list.get(0).openStream()); | ||
} | ||
} |