Skip to content

Commit

Permalink
Targetable option for tanks
Browse files Browse the repository at this point in the history
  • Loading branch information
aehmttw committed Feb 5, 2025
1 parent 1afc068 commit 6e0072d
Show file tree
Hide file tree
Showing 20 changed files with 300 additions and 66 deletions.
47 changes: 43 additions & 4 deletions src/main/java/tanks/Crusade.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tanks.item.Item;
import tanks.item.ItemBullet;
import tanks.item.ItemMine;
import tanks.network.ServerHandler;
import tanks.network.event.*;
import tanks.tank.Tank;
import tanks.tank.TankAIControlled;
Expand Down Expand Up @@ -271,11 +272,10 @@ public void loadLevel()
{
livesTotal += player.remainingLives;
playersTotal++;

CrusadePlayer cp = crusadePlayers.get(player);
if (cp.currentBuild == null)
cp.currentBuild = this.crusadeShopBuilds.get(0).name;
}

if (player.buildName == null)
player.buildName = this.crusadeShopBuilds.get(0).name;
}

for (Player player : Game.players)
Expand Down Expand Up @@ -319,6 +319,8 @@ public void loadLevel()

l.loadLevel();

ArrayList<TankPlayer.ShopTankBuild> builds = this.getBuildsShop();

for (Player player : Game.players)
{
player.hotbar.coins = crusadePlayers.get(player).coins;
Expand Down Expand Up @@ -356,6 +358,43 @@ else if (item instanceof ItemMine.ItemStackMine)
}
}

if (ScreenPartyHost.isServer)
{
for (ServerHandler h : ScreenPartyHost.server.connections)
{
if (h.player != null)
{
for (String s : h.player.ownedBuilds)
{
h.queueEvent(new EventPurchaseBuild(s));
}

for (int n = 0; n < builds.size(); n++)
{
TankPlayer.ShopTankBuild s = builds.get(n);
if (s.name.equals(h.player.buildName))
h.queueEvent(new EventPlayerSetBuild(n));
}
}
}
}

for (Movable m: Game.movables)
{
if (m instanceof TankPlayerRemote)
((TankPlayerRemote) m).buildName = ((TankPlayerRemote) m).player.buildName;
}

if (Game.playerTank != null)
{
for (int n = 0; n < builds.size(); n++)
{
TankPlayer.ShopTankBuild s = builds.get(n);
if (s.name.equals(Game.player.buildName))
s.clonePropertiesTo(Game.playerTank);
}
}

this.disconnectedPlayers.clear();

String sub = "";
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/tanks/CrusadePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public class CrusadePlayer
public Player player;

public ItemBar itemBar;
public HashSet<String> ownedBuilds = new HashSet<>();
public String currentBuild;

public int coins;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/tanks/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ public static void registerEvents()
NetworkEventMap.register(EventAddShopItem.class);
NetworkEventMap.register(EventSortShopButtons.class);
NetworkEventMap.register(EventPurchaseItem.class);
NetworkEventMap.register(EventPurchaseBuild.class);
NetworkEventMap.register(EventSetItem.class);
NetworkEventMap.register(EventSetItemBarSlot.class);
NetworkEventMap.register(EventLoadItemBarSlot.class);
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/tanks/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,18 @@ else if (j == 2)
else if (!remote)
{
TankPlayer tank = new TankPlayer(x, y, angle);
this.playerBuilds.get(0).clonePropertiesTo(tank);

TankPlayer.ShopTankBuild build = this.playerBuilds.get(0);
if (Crusade.crusadeMode)
{
ArrayList<TankPlayer.ShopTankBuild> builds = Crusade.currentCrusade.getBuildsShop();
for (TankPlayer.ShopTankBuild shopTankBuild : builds)
{
if (shopTankBuild.name.equals(Game.player.buildName))
build = shopTankBuild;
}
}
build.clonePropertiesTo(tank);
Game.playerTank = tank;
Game.player.buildName = tank.buildName;
tank.team = team;
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/tanks/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,17 @@ else if (Game.deterministicMode)
if (ScreenPartyHost.readyPlayers.size() >= ScreenPartyHost.includedPlayers.size() && Game.screen instanceof ScreenGame && ((ScreenGame) Game.screen).cancelCountdown)
{
Game.eventsOut.add(new EventBeginLevelCountdown());
((ScreenGame) Game.screen).cancelCountdown = false;
ScreenGame s = (ScreenGame) Game.screen;
s.cancelCountdown = false;

for (Movable m: Game.movables)
{
if (m instanceof TankPlayable)
{
int build = -1;
for (int i = 0; i < Game.currentLevel.playerBuilds.size(); i++)
for (int i = 0; i < s.builds.size(); i++)
{
TankPlayable t = Game.currentLevel.playerBuilds.get(i);
TankPlayable t = s.builds.get(i);
if (t.name.equals(((TankPlayable) m).buildName))
{
build = i;
Expand Down Expand Up @@ -585,11 +586,7 @@ else if (Game.deterministicMode)
{
for (int j = 0; j < ScreenPartyHost.server.connections.size(); j++)
{
synchronized (ScreenPartyHost.server.connections.get(j).events)
{
ScreenPartyHost.server.connections.get(j).events.addAll(Game.eventsOut);
}

ScreenPartyHost.server.connections.get(j).addEvents(Game.eventsOut);
ScreenPartyHost.server.connections.get(j).reply();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/tanks/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Player
public String username;
public Tank tank;
public String buildName = "player";
public HashSet<String> ownedBuilds = new HashSet<>();

public int colorR = 0;
public int colorG = 150;
Expand Down
58 changes: 51 additions & 7 deletions src/main/java/tanks/gui/screen/ScreenGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,15 @@ public ScreenGame()

this.initBuilds(Game.currentLevel.playerBuilds);

for (TankPlayer.ShopTankBuild b: Game.currentLevel.playerBuilds)
{
if (b.price > 0)
{
shop = true;
break;
}
}

if (!Game.currentLevel.startingItems.isEmpty())
{
startingItems = true;
Expand All @@ -598,6 +607,9 @@ public ScreenGame()
p.hotbar.itemBar.showItems = false;
p.hotbar.enabledCoins = false;

p.ownedBuilds.clear();
p.ownedBuilds.add(Game.currentLevel.playerBuilds.get(0).name);

if (startingItems)
{
for (Item.ItemStack<?> i: Game.currentLevel.startingItems)
Expand Down Expand Up @@ -718,17 +730,32 @@ public void initBuilds(ArrayList<TankPlayer.ShopTankBuild> builds)
this.builds = builds;
for (int i = 0; i < builds.size(); i++)
{
TankPlayer t = builds.get(i);
TankPlayer.ShopTankBuild t = builds.get(i);
TankPlayable display = t.clonePropertiesTo(new TankPlayer().setPlayerColor());
int j = i;
ButtonObject b = new ButtonObject(t, 0, 0, 75, 75, () ->
ButtonObject b = new ButtonObject(display, 0, 0, 75, 75, () ->
{
t.clonePropertiesTo(Game.playerTank);
if (ScreenPartyLobby.isClient)
Game.eventsOut.add(new EventPlayerSetBuild(j));
else
{
boolean success = false;
if (Game.player.ownedBuilds.contains(t.name))
success = true;
else if (Game.player.hotbar.coins >= t.price)
{
Game.player.ownedBuilds.add(t.name);
Game.player.hotbar.coins -= t.price;
success = true;
}

Game.player.buildName = t.name;
if (success)
{
t.clonePropertiesTo(Game.playerTank);
Game.player.buildName = t.name;
}
}
}, t.description);
//b.text = t.name;
this.playerBuildButtons.add(b);
}

Expand Down Expand Up @@ -1308,7 +1335,24 @@ else if (Game.game.window.touchscreen && !shopScreen)
{
for (int i = 0; i < this.builds.size(); i++)
{
this.playerBuildsList.buttons.get(i).enabled = !this.builds.get(i).name.equals(Game.player.buildName);
Button b = this.playerBuildsList.buttons.get(i);
TankPlayer.ShopTankBuild t = this.builds.get(i);

int p = t.price;

((ButtonObject)b).showText = true;

String prefix = p > Game.player.hotbar.coins ? "\u00A7255127127255" : "";
if (Game.player.ownedBuilds.contains(t.name))
b.setText("Owned");
else if (p == 0)
b.setText("Free!");
else if (p == 1)
b.setText("%s1 coin", prefix);
else
b.setText("%s%d coins", prefix, p);

b.enabled = !t.name.equals(Game.player.buildName);
}
}
this.playerBuildsList.update();
Expand Down Expand Up @@ -1501,7 +1545,7 @@ else if (this.shopScreen)
Game.removeMovables.add(m);
Game.movables.add(new MovableNaN(m.lastPosX, m.lastPosY));
}
else if (m instanceof ISolidObject && !(m instanceof Tank && !((Tank) m).targetable))
else if (m instanceof ISolidObject && !(m instanceof Tank && !((Tank) m).currentlyTargetable))
{
Game.horizontalFaces.addAll(Arrays.asList(((ISolidObject) m).getHorizontalFaces()));

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tanks/gui/screen/ScreenTitle.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void update()
throw new RuntimeException("Movable with NaN position: " + m.toString() + " " + m.lastPosX + " " + m.lastPosY);
}

if (m instanceof ISolidObject && !(m instanceof Tank && !(((Tank) m).targetable || ((Tank) m).invulnerabilityTimer > 0)))
if (m instanceof ISolidObject && !(m instanceof Tank && !(((Tank) m).currentlyTargetable || ((Tank) m).invulnerabilityTimer > 0)))
{
Game.horizontalFaces.addAll(Arrays.asList(((ISolidObject) m).getHorizontalFaces()));

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tanks/hotbar/ItemBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public boolean useItem(boolean rightClick)
for (ServerHandler sh : ScreenPartyHost.server.connections)
{
if (sh.player.equals(this.player))
sh.events.add(new EventSetItemBarSlot(-1));
sh.queueEvent(new EventSetItemBarSlot(-1));
}
}
}
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/tanks/network/ServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
import tanks.gui.screen.ScreenPartyHost;
import tanks.network.event.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

public class ServerHandler extends ChannelInboundHandlerAdapter
{
public MessageReader reader = new MessageReader();
public SynchronizedList<INetworkEvent> events = new SynchronizedList<>();

protected ArrayList<Integer> queuedEventIndices = new ArrayList<>();
protected ArrayList<INetworkEvent> queuedEvents = new ArrayList<>();

protected SynchronizedList<INetworkEvent> events = new SynchronizedList<>();
protected HashMap<Integer, IStackableEvent> stackedEvents = new HashMap<>();
protected long lastStackedEventSend = 0;

Expand Down Expand Up @@ -93,6 +98,44 @@ public void channelInactive(ChannelHandlerContext ctx)
// }
}

/**
* Queues an event to be added, to be sent exactly after all the events currently
* in Game.eventsOut
* @param e
*/
public void queueEvent(INetworkEvent e)
{
this.queuedEvents.add(e);
this.queuedEventIndices.add(Game.eventsOut.size());
}

public void addEvents(ArrayList<INetworkEvent> events)
{
synchronized (this.events)
{
int j = 0;
for (int i = 0; i < events.size(); i++)
{
while (j < this.queuedEventIndices.size() && this.queuedEventIndices.get(j) == i)
{
this.events.add(this.queuedEvents.get(j));
j++;
}

this.events.add(events.get(i));
}

while (j < this.queuedEventIndices.size())
{
this.events.add(this.queuedEvents.get(j));
j++;
}

this.queuedEvents.clear();
this.queuedEventIndices.clear();
}
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
{
Expand Down
Loading

0 comments on commit 6e0072d

Please sign in to comment.