-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientGame.cs
141 lines (131 loc) · 5.4 KB
/
ClientGame.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
using LegendDialogue;
using LegendItems;
using MongoDB.Bson;
using Packets;
using System;
using System.Collections.Generic;
using System.Text;
namespace LegendSharp
{
class ClientGame : Game
{
ClientHandler handler;
Legend legend;
public ClientGame(ClientHandler handler, String userId, String username, Config config, Legend legend) : base(userId, username, config, legend)
{
this.handler = handler;
this.legend = legend;
handler.SendPacket(new ReadyPacket(1));
handler.SendPacket(new PlayerDataPacket(this.player.sprite, this.player.uuid));
handler.SendPacket(new PlayerPositionPacket(this.player.pos.x, this.player.pos.y));
handler.SendPacket(new InventoryPacket(this.player.inventory));
}
public void HandlePacket(Packet packet)
{
if (packet is RequestWorldPacket)
{
handler.SendPacket(new WorldPacket(legend.world));
}
else if (packet is MoveAndFacePacket)
{
MoveAndFacePacket movePacket = (MoveAndFacePacket)packet;
Position newPos = new Position(movePacket.x, movePacket.y);
player.facing = (FACING) movePacket.facing;
legend.world.MoveEntity(player, newPos, legend.config);
if (player.pos != newPos)
{
handler.SendPacket(new PlayerPositionPacket(player.pos.x, player.pos.y));
}
}
else if (packet is MovePacket)
{
MovePacket movePacket = (MovePacket)packet;
Position newPos = new Position(movePacket.x, movePacket.y);
legend.world.MoveEntity(player, newPos, legend.config);
if (player.pos != newPos)
{
handler.SendPacket(new PlayerPositionPacket(player.pos.x, player.pos.y));
}
}
else if (packet is SendMessagePacket)
{
SendMessagePacket messagePacket = (SendMessagePacket)packet;
var msg = new ChatMessage(this.username, messagePacket.message, this.userId);
legend.world.SendToNearby(msg, player.pos, legend.config);
}
else if (packet is EntityInteractPacket)
{
EntityInteractPacket interactPacket = (EntityInteractPacket)packet;
TryInteract(interactPacket.interactType, interactPacket.guid);
}
else if (packet is GUIOptionPacket)
{
GUIOptionPacket optionPacket = (GUIOptionPacket)packet;
if (dialogueOpen != null && dialogueOpen.HasOption(optionPacket.guid))
{
Option option = dialogueOpen.GetOption(optionPacket.guid);
//TODO: ENFORCE REQUIREMENTS
if (option.IsDisplayed(player.flags))
{
if (option is DialogueOption)
{
DialogueOption dialogueOption = (DialogueOption)option;
OpenDialogue(dialogueOption.dialogueKey);
}
else if (option is EndDialogueOption)
{
handler.SendPacket(new CloseDialoguePacket(Guid.NewGuid()));
}
}
else
{
//SMH Dirty cheater.
handler.SendPacket(new CloseDialoguePacket(Guid.NewGuid()));
}
}
}
}
public override void OpenDialogue(string dialogueKey)
{
//TODO: Actions
if (legend.config.dialogue.ContainsKey(dialogueKey))
{
Dialogue dialogue = legend.config.dialogue[dialogueKey];
foreach (PlayerAction action in dialogue.actions)
{
action.Run(this);
}
DialoguePacket dialoguePacket = new DialoguePacket(dialogue, this);
handler.SendPacket(dialoguePacket);
dialogueOpen = dialogue;
}
}
public override void UpdateEntityPos(Entity entity)
{
handler.SendPacket(new EntityMovePacket(entity.pos.x, entity.pos.y, (int)entity.facing, entity.uuid));
}
public override void AddToCache(Entity entity)
{
cachedEntities.Add(entity);
handler.SendPacket(new EntityPacket(entity));
}
public override void RemoveFromCache(Entity entity)
{
cachedEntities.Remove(entity);
handler.SendPacket(new InvalidateCachePacket(entity.uuid));
}
public override void SendMessage(ChatMessage message, Position pos)
{
handler.SendPacket(new ChatPacket(message.author, message.message, message.authorId, message.uuid, pos.x, pos.y));
}
public override void AddToInventory(Guid guid, Item item, int index)
{
Console.WriteLine("ClientGame called");
handler.SendPacket(new AddItemPacket(guid, item, index));
}
public override void ChangeInInventory(Guid guid, Item item, int index)
{
handler.SendPacket(new ModifyItemPacket(guid, item, index));
}
}
}