forked from tModLoader/tModLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTEScoreBoard.cs
208 lines (189 loc) · 7.89 KB
/
TEScoreBoard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics.PackedVector;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.DataStructures;
using Terraria.Enums;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.ObjectData;
namespace ExampleMod.Tiles
{
public class ScoreBoardGlobalNPC : GlobalNPC
{
public override void NPCLoot(NPC npc) {
if (npc.lastInteraction == 255) {
//Main.NewText("Accidental Death, score unchanged");
return;
}
int TEScoreBoardType = ModContent.TileEntityType<TEScoreBoard>();
foreach (TileEntity current in TileEntity.ByID.Values) {
if (current.type == TEScoreBoardType) {
//QuickBox is a neat tool for visualizing things while modding.
//Dust.QuickBox(npc.position, npc.position + new Vector2(npc.width, npc.height), 1, Color.White, null);
var scoreboard = current as TEScoreBoard;
if (scoreboard.GetPlayArea().Intersects(npc.getRect())) {
Player scoringPlayer = Main.player[npc.lastInteraction];
int score = 0;
// Using HalfVector2 and ReinterpretCast.UIntAsFloat is a way to pack a Vector2 into a single float variable.
HalfVector2 halfVector = new HalfVector2((current.Position.X + 1) * 16, (current.Position.Y + 1) * 16);
Projectile.NewProjectile(npc.Center, Vector2.Zero, ModContent.ProjectileType<Projectiles.ScorePoint>(), 0, 0, Main.myPlayer, ReLogic.Utilities.ReinterpretCast.UIntAsFloat(halfVector.PackedValue), npc.lastInteraction);
scoreboard.scores.TryGetValue(scoringPlayer.name, out score);
scoreboard.scores[scoringPlayer.name] = score + 1;
if (Main.dedServ) {
NetworkText text = NetworkText.FromFormattable("{0}: {1}", scoringPlayer.name, scoreboard.scores[scoringPlayer.name]);
NetMessage.BroadcastChatMessage(text, Color.White);
}
else {
Main.NewText(scoringPlayer.name + ": " + scoreboard.scores[scoringPlayer.name]);
}
scoreboard.scoresChanged = true;
}
}
}
}
}
// TODO, reset scores option
public class TEScoreBoard : ModTileEntity
{
// Half the width in Tile Coordinates.
internal const int range = 50;
internal Dictionary<string, int> scores = new Dictionary<string, int>();
internal bool scoresChanged;
internal const int drawBorderWidth = 5;
/// <summary>
/// Returns a rectangle representing the play area in World coordinates.
/// </summary>
public Rectangle GetPlayArea() {
return new Rectangle((Position.X + 1) * 16 - range * 16, (Position.Y + 1) * 16 - range * 16, range * 16 * 2, range * 16 * 2);
}
public override void Update() {
if (scoresChanged) {
// Sending 86 aka, TileEntitySharing, triggers NetSend. Think of it like manually calling sync.
NetMessage.SendData(MessageID.TileEntitySharing, -1, -1, null, ID, Position.X, Position.Y);
scoresChanged = false;
}
}
public override void NetReceive(BinaryReader reader, bool lightReceive) {
scores.Clear();
int count = reader.ReadInt32();
for (int i = 0; i < count; i++) {
string name = reader.ReadString();
int score = reader.ReadInt32();
scores[name] = score;
}
}
public override void NetSend(BinaryWriter writer, bool lightSend) {
writer.Write(scores.Keys.Count);
foreach (var item in scores) {
writer.Write(item.Key);
writer.Write(item.Value);
}
}
public override TagCompound Save() {
return new TagCompound
{
{"scoreNames", scores.Keys.ToList()},
{"scoreValues", scores.Values.ToList()}
};
}
public override void Load(TagCompound tag) {
var names = tag.Get<List<string>>("scoreNames");
var values = tag.Get<List<int>>("scoreValues");
scores = names.Zip(values, (k, v) => new { Key = k, Value = v }).ToDictionary(x => x.Key, x => x.Value);
}
public override bool ValidTile(int i, int j) {
Tile tile = Main.tile[i, j];
return tile.active() && tile.type == ModContent.TileType<ScoreBoard>() && tile.frameX == 0 && tile.frameY == 0;
}
public override int Hook_AfterPlacement(int i, int j, int type, int style, int direction) {
//Main.NewText("i " + i + " j " + j + " t " + type + " s " + style + " d " + direction);
if (Main.netMode == NetmodeID.MultiplayerClient) {
NetMessage.SendTileSquare(Main.myPlayer, i, j, 3);
NetMessage.SendData(MessageID.TileEntityPlacement, -1, -1, null, i, j, Type, 0f, 0, 0, 0);
return -1;
}
return Place(i, j);
}
}
public class ScoreBoard : ModTile
{
// TODO, outline sprites.
public override void SetDefaults() {
Main.tileFrameImportant[Type] = true;
Main.tileLavaDeath[Type] = true;
TileID.Sets.FramesOnKillWall[Type] = true; // Necessary since we have a placement that uses AnchorWall
TileObjectData.newTile.CopyFrom(TileObjectData.Style2x2);
// We set processedCoordinates to true so our Hook_AfterPlacement gets top left coordinates, regardless of Origin.
TileObjectData.newTile.HookPostPlaceMyPlayer = new PlacementHook(ModContent.GetInstance<TEScoreBoard>().Hook_AfterPlacement, -1, 0, true);
TileObjectData.newTile.StyleHorizontal = true;
TileObjectData.newTile.StyleMultiplier = 5;
TileObjectData.newAlternate.CopyFrom(TileObjectData.newTile);
TileObjectData.addAlternate(0);
TileObjectData.newAlternate.CopyFrom(TileObjectData.newTile);
TileObjectData.newAlternate.Origin = Point16.Zero;
TileObjectData.newAlternate.AnchorTop = new AnchorData(AnchorType.SolidTile | AnchorType.SolidSide, 2, 0);
TileObjectData.newAlternate.AnchorBottom = AnchorData.Empty;
TileObjectData.addAlternate(1);
TileObjectData.newAlternate.CopyFrom(TileObjectData.newTile);
TileObjectData.newAlternate.Origin = new Point16(0, 0);
TileObjectData.newAlternate.AnchorLeft = new AnchorData(AnchorType.SolidTile | AnchorType.SolidSide, 2, 0);
TileObjectData.newAlternate.AnchorBottom = AnchorData.Empty;
TileObjectData.addAlternate(2);
TileObjectData.newAlternate.CopyFrom(TileObjectData.newTile);
TileObjectData.newAlternate.Origin = new Point16(1, 0);
TileObjectData.newAlternate.AnchorRight = new AnchorData(AnchorType.SolidTile | AnchorType.SolidSide, 2, 0);
TileObjectData.newAlternate.AnchorBottom = AnchorData.Empty;
TileObjectData.addAlternate(3);
TileObjectData.newTile.Origin = new Point16(0, 1);
TileObjectData.newAlternate.CopyFrom(TileObjectData.newTile);
TileObjectData.newAlternate.Origin = Point16.Zero;
TileObjectData.newAlternate.AnchorWall = true;
TileObjectData.newAlternate.AnchorBottom = AnchorData.Empty;
TileObjectData.addAlternate(4);
TileObjectData.addTile(Type);
ModTranslation name = CreateMapEntryName();
name.SetDefault("ScoreBoard");
AddMapEntry(new Color(26, 127, 206), name);
disableSmartCursor = true; //?
//TODO Main.highlightMaskTexture[Type] = mod.GetTexture("Tiles/ScoreBoard_Outline");
}
public override void KillMultiTile(int i, int j, int frameX, int frameY) {
Item.NewItem(i * 16, j * 16, 32, 48, ModContent.ItemType<Items.Placeable.ScoreBoard>());
ModContent.GetInstance<TEScoreBoard>().Kill(i, j);
}
public override bool NewRightClick(int i, int j) {
Tile tile = Main.tile[i, j];
int left = i - tile.frameX % 36 / 18;
int top = j - tile.frameY / 18;
int index = ModContent.GetInstance<TEScoreBoard>().Find(left, top);
if (index == -1) {
return false;
}
Main.NewText("Scores:");
TEScoreBoard tEScoreBoard = (TEScoreBoard)TileEntity.ByID[index];
foreach (var item in tEScoreBoard.scores) {
Main.NewText(item.Key + ": " + item.Value);
}
return true;
}
public override void MouseOver(int i, int j) {
MouseOverBoth(i, j);
}
public override void MouseOverFar(int i, int j) {
MouseOverBoth(i, j);
}
public void MouseOverBoth(int i, int j) {
Tile tile = Main.tile[i, j];
int left = i - tile.frameX % 36 / 18;
int top = j - tile.frameY % 36 / 18;
Main.signBubble = true;
Main.signX = left * 16 + 16;
Main.signY = top * 16;
}
}
}