Add Redo/Undo system (WIP).

This commit is contained in:
William Burns 2016-02-24 07:58:50 +00:00
parent 790ed4f993
commit 700f5ec44a
7 changed files with 223 additions and 32 deletions

View File

@ -0,0 +1,25 @@
package nautilus.game.arcade.game.games.draw;
import org.bukkit.Material;
public class BlockInfo
{
private Material _type;
private byte _data;
public BlockInfo(Material type, byte data)
{
_type = type;
_data = data;
}
public Material getType()
{
return _type;
}
public byte getData()
{
return _data;
}
}

View File

@ -10,7 +10,6 @@ import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
@ -19,6 +18,7 @@ import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
@ -57,7 +57,6 @@ import nautilus.game.arcade.stats.DrawGuessStatTracker;
import nautilus.game.arcade.stats.KeenEyeStatTracker; import nautilus.game.arcade.stats.KeenEyeStatTracker;
import nautilus.game.arcade.stats.MrSquiggleStatTracker; import nautilus.game.arcade.stats.MrSquiggleStatTracker;
import nautilus.game.arcade.stats.PureLuckStatTracker; import nautilus.game.arcade.stats.PureLuckStatTracker;
import sun.misc.REException;
public class Draw extends SoloGame public class Draw extends SoloGame
{ {
@ -83,6 +82,7 @@ public class Draw extends SoloGame
private ArrayList<Block> _canvas = new ArrayList<Block>(); private ArrayList<Block> _canvas = new ArrayList<Block>();
private Location _drawerLocation = null; private Location _drawerLocation = null;
private Location _textLocation = null; private Location _textLocation = null;
private UndoRedoData _drawerUndoRedo = null;
private Collection<Block> _textBlocks = null; private Collection<Block> _textBlocks = null;
@ -366,6 +366,8 @@ public class Draw extends SoloGame
//Create Round //Create Round
_round = new DrawRound(this, drawer); _round = new DrawRound(this, drawer);
_drawerUndoRedo = new UndoRedoData(this, drawer);
//Prep Drawer //Prep Drawer
drawer.teleport(_drawerLocation); drawer.teleport(_drawerLocation);
@ -383,7 +385,9 @@ public class Draw extends SoloGame
drawer.getInventory().addItem(ItemStackFactory.Instance.CreateStack(Material.IRON_HOE, (byte)0, 1, "Paint Bucket")); drawer.getInventory().addItem(ItemStackFactory.Instance.CreateStack(Material.IRON_HOE, (byte)0, 1, "Paint Bucket"));
drawer.getInventory().addItem(ItemStackFactory.Instance.CreateStack(Material.GOLD_HOE, (byte)0, 1, "Clear Canvas")); drawer.getInventory().addItem(ItemStackFactory.Instance.CreateStack(Material.GOLD_HOE, (byte)0, 1, "Clear Canvas"));
Announce(C.cGold + C.Bold + "Round " + (_roundCount+1) + ": " + C.cYellow + C.Bold + drawer.getName() + " is drawing!"); drawer.getInventory().setItem(10, ItemStackFactory.Instance.CreateStack(Material.ARROW, (byte)0, 1, "Paint"));
Announce(C.cGold + C.Bold + "Round " + (_roundCount + 1) + ": " + C.cYellow + C.Bold + drawer.getName() + " is drawing!");
} }
} }
@ -566,6 +570,86 @@ public class Draw extends SoloGame
tool.update(); tool.update();
} }
@EventHandler
public void sprayCan(UpdateEvent e)
{
if (e.getType() != UpdateType.TICK)
return;
if (!IsLive())
return;
for (Player p : _drawers.GetPlayers(true))
{
if (!UtilGear.isMat(p.getItemInHand(), Material.BOW))
continue;
if (!UtilPlayer.isChargingBow(p))
{
_brushPrevious = null;
continue;
}
Block block = p.getTargetBlock((HashSet<Byte>)null, 200);
if (block == null || !_canvas.contains(block))
continue;
// Spray
block.setType(_brushMaterial);
block.setData(_brushColor);
for (Block surround : UtilBlock.getSurrounding(block, true))
{
if (!_canvas.contains(surround))
continue;
if (Math.random() > 0.1)
{
block.setType(_brushMaterial);
block.setData(_brushColor);
}
}
for (Player other : UtilServer.getPlayers())
other.playSound(other.getLocation(), Sound.FIZZ, 0.2f, 2f);
_lockDrawer = false;
_brushPrevious = block.getLocation().add(0.5, 0.5, 0.5);
}
}
@EventHandler
public void undoRedo(PlayerInteractEvent e)
{
if (!IsLive())
return;
Player p = e.getPlayer();
if (!isDrawer(p))
return;
Block block = p.getTargetBlock((HashSet<Byte>)null, 200);
if (block == null || block.getType() != Material.STAINED_CLAY)
return;
if (block.getData() == 14)
_drawerUndoRedo.undo(); // Undo
else if (block.getData() == 5)
_drawerUndoRedo.redo(); // Redo
}
@EventHandler
public void sprayCanArrowCancel(EntityShootBowEvent e)
{
if (e.getEntity() instanceof Player)
{
e.setCancelled(true);
((Player)e.getEntity()).updateInventory();
}
}
@EventHandler @EventHandler
public void Paint(UpdateEvent event) public void Paint(UpdateEvent event)
{ {
@ -958,10 +1042,8 @@ public class Draw extends SoloGame
_lockDrawer = b; _lockDrawer = b;
} }
//fdmsdmfsdfpmsd public UndoRedoData getDrawerUndoRedo()
/** {
* TODO: return _drawerUndoRedo;
* - Spray Can }
* - Undo/Redo
*/
} }

View File

@ -0,0 +1,99 @@
package nautilus.game.arcade.game.games.draw;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.recharge.Recharge;
public class UndoRedoData
{
private Player _player;
private ArrayList<HashMap<Block, BlockInfo>> _steps;
private int _stepIndex;
private Draw _host;
public UndoRedoData(Draw host, Player player)
{
_player = player;
_steps = new ArrayList<>();
_stepIndex = 0;
_host = host;
takeSnapshot(); // Grab current white stage.
}
public void takeSnapshot()
{
if (_stepIndex > 0)
{
// They have drawn on top of an undo.
// Set this to the current stage and
// remove snapshots in front.
for (int i = 0; i < _stepIndex; i++)
_steps.remove(0); // Remove "current" (old) snapshot.
_stepIndex = 0;
}
HashMap<Block, BlockInfo> snapshot = new HashMap<>();
for (Block b : _host.getCanvas())
snapshot.put(b, new BlockInfo(b.getType(), b.getData()));
_steps.add(0, snapshot);
}
public void undo()
{
if (_stepIndex == (_steps.size() - 1))
{
// No snapshots to go back to.
_player.sendMessage(F.main("Game", C.cRed + "Nothing left to undo!"));
return;
}
if (!Recharge.Instance.use(_player, "Undo", 5000, true, false))
return;
_player.sendMessage(F.main("Game", "You used " + C.mSkill + "Undo" + C.mBody + "."));
_stepIndex++; // Move back a snapshot.
resetBlocks();
}
public void redo()
{
if (_stepIndex == 0)
{
// No steps to go forward to.
_player.sendMessage(F.main("Game", C.cRed + "Nothing left to redo!"));
return;
}
if (!Recharge.Instance.use(_player, "Redo", 5000, true, false))
return;
_player.sendMessage(F.main("Recharge", "You used " + C.mSkill + "Redo" + C.mBody + "."));
_stepIndex--; // Move forward a step.
resetBlocks();
}
private void resetBlocks()
{
HashMap<Block, BlockInfo> step = _steps.get(_stepIndex);
for (Block b : step.keySet())
{
// Reset blocks.
b.setType(step.get(b).getType());
b.setData(step.get(b).getData());
//b.getWorld().getBlockAt(b.getLocation()).setType(step.get(b).getType());
//b.getWorld().getBlockAt(b.getLocation()).setData(step.get(b).getData());
}
}
}

View File

@ -8,6 +8,7 @@ import mineplex.core.common.util.UtilEvent;
import mineplex.core.common.util.UtilGear; import mineplex.core.common.util.UtilGear;
import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilEvent.ActionType; import mineplex.core.common.util.UtilEvent.ActionType;
import nautilus.game.arcade.game.games.draw.BlockInfo;
import nautilus.game.arcade.game.games.draw.Draw; import nautilus.game.arcade.game.games.draw.Draw;
import org.bukkit.Material; import org.bukkit.Material;
@ -18,28 +19,6 @@ import org.bukkit.event.player.PlayerInteractEvent;
public abstract class Tool public abstract class Tool
{ {
public class BlockInfo
{
private Material _type;
private byte _data;
public BlockInfo(Material type, byte data)
{
_type = type;
_data = data;
}
public Material getType()
{
return _type;
}
public byte getData()
{
return _data;
}
}
protected Draw Host; protected Draw Host;
protected Player _drawer; protected Player _drawer;
@ -84,6 +63,8 @@ public abstract class Tool
if (!_drawer.isOnline() || !Host.isDrawer(_drawer) || !_drawer.isBlocking()) if (!_drawer.isOnline() || !Host.isDrawer(_drawer) || !_drawer.isBlocking())
{ {
Host.getDrawerUndoRedo().takeSnapshot(); // Save final shape.
_drawer = null; _drawer = null;
_start = null; _start = null;
_past.clear(); _past.clear();

View File

@ -3,6 +3,7 @@ package nautilus.game.arcade.game.games.draw.tools;
import mineplex.core.common.util.UtilAlg; import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilParticle; import mineplex.core.common.util.UtilParticle;
import mineplex.core.common.util.UtilParticle.ParticleType; import mineplex.core.common.util.UtilParticle.ParticleType;
import nautilus.game.arcade.game.games.draw.BlockInfo;
import nautilus.game.arcade.game.games.draw.Draw; import nautilus.game.arcade.game.games.draw.Draw;
import org.bukkit.Location; import org.bukkit.Location;
@ -56,7 +57,7 @@ public class ToolCircle extends Tool
} }
_new.put(block, new BlockInfo(type, color)); _new.put(block, new BlockInfo(type, color));
block.setData(Host.getColor());
block.setType(Host.getBrushMaterial()); block.setType(Host.getBrushMaterial());
block.setData(Host.getColor());
} }
} }

View File

@ -2,6 +2,7 @@ package nautilus.game.arcade.game.games.draw.tools;
import mineplex.core.common.util.UtilAlg; import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilMath; import mineplex.core.common.util.UtilMath;
import nautilus.game.arcade.game.games.draw.BlockInfo;
import nautilus.game.arcade.game.games.draw.Draw; import nautilus.game.arcade.game.games.draw.Draw;
import org.bukkit.Location; import org.bukkit.Location;

View File

@ -1,5 +1,6 @@
package nautilus.game.arcade.game.games.draw.tools; package nautilus.game.arcade.game.games.draw.tools;
import nautilus.game.arcade.game.games.draw.BlockInfo;
import nautilus.game.arcade.game.games.draw.Draw; import nautilus.game.arcade.game.games.draw.Draw;
import org.bukkit.Location; import org.bukkit.Location;
@ -81,6 +82,7 @@ public class ToolSquare extends Tool
} }
_new.put(block, new BlockInfo(type, color)); _new.put(block, new BlockInfo(type, color));
block.setType(Host.getBrushMaterial());
block.setData(Host.getColor()); block.setData(Host.getColor());
} }
} }