Skip to content

Commit

Permalink
Merge PR #3717 by @casals - new Group asset
Browse files Browse the repository at this point in the history
  • Loading branch information
Cervator committed Aug 10, 2019
2 parents d8d9aa2 + 33455a0 commit b5da560
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 2 deletions.
4 changes: 2 additions & 2 deletions engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ dependencies {
compile 'com.jagrosh:DiscordIPC:0.4'

// Our developed libs
compile group: 'org.terasology', name: 'gestalt-module', version: '5.1.3'
compile group: 'org.terasology', name: 'gestalt-asset-core', version: '5.1.3'
compile group: 'org.terasology', name: 'gestalt-module', version: '5.1.4'
compile group: 'org.terasology', name: 'gestalt-asset-core', version: '5.1.4'
compile group: 'org.terasology', name: 'TeraMath', version: '1.4.0'
compile group: 'org.terasology.bullet', name: 'tera-bullet', version: '1.3.1'
compile group: 'org.terasology', name: 'splash-screen', version: '1.0.2'
Expand Down
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;
}
}
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;
}

}
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;
}

}
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);
}
}
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());
}
}

0 comments on commit b5da560

Please sign in to comment.