forked from SteamWar/SteamWar
4c8b55167c
Add DynamicRegionEditor, DynamicRegionVisualizer
115 lines
4.6 KiB
Java
115 lines
4.6 KiB
Java
/*
|
|
* This file is a part of the SteamWar software.
|
|
*
|
|
* Copyright (C) 2026 SteamWar.de-Serverteam
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package de.steamwar.bausystem.region;
|
|
|
|
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
|
|
import de.steamwar.bausystem.region.dynamic.RegionConstructorData;
|
|
import de.steamwar.bausystem.region.dynamic.Tile;
|
|
import de.steamwar.core.SWPlayer;
|
|
import de.steamwar.entity.*;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.World;
|
|
import org.bukkit.entity.Display;
|
|
import org.bukkit.util.Transformation;
|
|
import org.joml.Quaternionf;
|
|
import org.joml.Vector3f;
|
|
|
|
public class DynamicRegionVisualizer implements SWPlayer.Component {
|
|
|
|
public static final DynamicRegionVisualizer INSTANCE = new DynamicRegionVisualizer();
|
|
|
|
private final REntityServer entityServer = new REntityServer();
|
|
|
|
private DynamicRegionVisualizer() {
|
|
RTextDisplay text = new RTextDisplay(entityServer, new Location(null, 0.5, 1.1, 0.5));
|
|
text.setText("Spawn");
|
|
text.setBillboard(Display.Billboard.VERTICAL);
|
|
text.setBackgroundColor(0);
|
|
text.setShadowed(false);
|
|
text.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf().rotationX((float) Math.toRadians(270)), new Vector3f(1, 1, 1), new Quaternionf()));
|
|
}
|
|
|
|
public void addRegion(DynamicRegion region) {
|
|
new CRegion(entityServer, region);
|
|
}
|
|
|
|
public void removeRegion(DynamicRegion region) {
|
|
entityServer.getEntitiesByType(CRegion.class)
|
|
.stream()
|
|
.filter(cRegion -> cRegion.region == region)
|
|
.forEach(CRegion::die);
|
|
}
|
|
|
|
@Override
|
|
public void onMount(SWPlayer player) {
|
|
entityServer.addPlayer(player.getPlayer());
|
|
}
|
|
|
|
@Override
|
|
public void onUnmount(SWPlayer player) {
|
|
entityServer.removePlayer(player.getPlayer());
|
|
}
|
|
|
|
private static final Vector3f VEC_ZERO = new Vector3f(0, 0, 0);
|
|
private static final Quaternionf QUT_ZERO = new Quaternionf(0, 0, 0, 1);
|
|
|
|
public static Point toVisualization(Location worldLocation) {
|
|
Tile tile = Tile.fromLocation(worldLocation).orElseThrow();
|
|
return new Point(tile.getTileX(), 0, tile.getTileZ());
|
|
}
|
|
|
|
private static class CRegion extends CEntity {
|
|
|
|
private final DynamicRegion region;
|
|
|
|
private CRegion(REntityServer server, DynamicRegion region) {
|
|
super(server);
|
|
this.region = region;
|
|
|
|
RegionConstructorData data = DynamicRegionSystem.constructorDataMap.get(region.getClass());
|
|
int widthX = data.widthX();
|
|
int widthZ = data.widthZ();
|
|
|
|
Point point = toVisualization(region.getArea().getMinPoint(false).toLocation((World) null));
|
|
|
|
if (widthX != 1 || widthZ != 1) {
|
|
CArea area = new CArea(server);
|
|
area.setBlock(Material.WHITE_CONCRETE.createBlockData());
|
|
area.setPos1And2(point.toLocation((World) null).add(0, 1 - CArea.DEFAULT_WIDTH, 0), point.toLocation((World) null).add(widthX - 1, 1 - CArea.DEFAULT_WIDTH, widthZ - 1));
|
|
entities.add(area);
|
|
|
|
RTextDisplay text = new RTextDisplay(server, point.toLocation((World) null).add(widthX / 2.0, 1.1, widthZ / 2.0));
|
|
text.setText(data.name());
|
|
text.setBillboard(Display.Billboard.VERTICAL);
|
|
text.setBackgroundColor(0);
|
|
text.setShadowed(false);
|
|
text.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf().rotationX((float) Math.toRadians(270)), new Vector3f(1, 1, 1), new Quaternionf()));
|
|
entities.add(text);
|
|
}
|
|
|
|
RBlockDisplay display = new RBlockDisplay(server, point.toLocation((World) null));
|
|
display.setTransform(new Transformation(VEC_ZERO, QUT_ZERO, new Vector3f(widthX, 1, widthZ), QUT_ZERO));
|
|
display.setBlock(data.material().createBlockData());
|
|
entities.add(display);
|
|
}
|
|
}
|
|
}
|