123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using CommonAIClient.Client;
- using RoomService.Net.BsServer;
- using RoomService.Net.BsClient;
- using GameEditorPlugin.Win32.Runtime;
- using CommonLang.Protocol;
- using CommonAI.ZoneClient;
- using CommonLang;
- using CommonAI.RTS;
- using CommonLang.Vector;
- using CommonAI.Zone;
- using CommonAI.Zone.ZoneEditor;
- using CommonLang.Log;
- using GameEditorPlugin.Win32.BattleClient;
- using System.IO;
- using CommonLang.IO;
- using CommonLang.ByteOrder;
- using CommonLang.Property;
- using CommonAI.ZoneServer;
- using CommonFroms.Utils;
- using CommonFroms;
- using CommonFroms.G2D;
- using CommonAI.ZoneClient.Agent;
- namespace GameEditorPlugin.Win32.Runtime
- {
- public partial class PanelAbstractClient : UserControl
- {
- // -----------------------------------------------------------------------------------------------------------------
- protected readonly Logger log = LoggerFactory.GetLogger("Client");
- public IAbstractBattleFactory BattleFactory { get; private set; }
- public EditorTemplates DataRoot { get { return BattleFactory.DataRoot; } }
- public AbstractBattle BattleClient { get { return battle; } }
- public DisplayLayerWorld DisplayWorld { get { return display; } }
- public DisplayGameUnit DisplayActor { get { return displayActor; } }
- private AbstractBattle battle;
- private DisplayLayerWorld display;
- private DisplayGameUnit displayActor;
- private long mLastUpdateTime;
- public float TurboX { get; set; }
- public bool IsFreeView { get { return btn_FreeView.Checked || displayActor == null || !displayActor.Data.IsActive; } }
- public bool IsActorGuard
- {
- get
- {
- if (BattleClient.Actor != null)
- {
- return BattleClient.Actor.IsGuard;
- }
- return false;
- }
- set
- {
- btn_Guard.Checked = value;
- if (BattleClient.Actor != null)
- {
- BattleClient.Actor.SendUnitGuard(btn_Guard.Checked);
- }
- }
- }
- public int RenderFPS
- {
- get
- {
- return this.timer1.Interval / 1000;
- }
- set
- {
- this.timer1.Interval = Math.Max(10, 1000 / value);
- }
- }
- public PanelAbstractClient()
- {
- InitializeComponent();
- this.TurboX = 1;
- this.Disposed += PanelAbstractClient_Disposed;
- }
- protected override void DestroyHandle()
- {
- base.DestroyHandle();
- event_OnPanelLoaded = null;
- event_OnPanelClosed = null;
- event_OnTimerBeginUpdate = null;
- event_OnTimerUpdate = null;
- event_OnNetworkViewClicked = null;
- event_DisplayActor_OnRender = null;
- event_DisplayLayer_OnRenderHUD = null;
- event_DisplayLayer_OnRenderLayer = null;
- }
- private void PanelAbstractClient_Disposed(object sender, EventArgs e)
- {
- if (display != null)
- {
- this.display.UnbindMessageFilter();
- if (displayActor != null)
- {
- displayActor.OnRender -= actor_render;
- }
- if (event_OnPanelClosed != null)
- event_OnPanelClosed.Invoke();
- display.Dispose();
- }
- }
- public void Start(IAbstractBattleFactory factory)
- {
- this.BattleFactory = factory;
- if (DataRoot != null)
- {
- this.RenderFPS = (DataRoot.Templates.CFG.SYSTEM_FPS);
- this.battle = BattleFactory.GenBattle();
- this.display = BattleFactory.GenDisplay(pictureBox1);
- this.display.OnRenderHUD += display_OnRenderHUD;
- this.display.OnRenderLayer += display_OnRenderLayer;
- this.display.InitClient(battle);
- this.battle.Layer.LayerInit += Layer_LayerInit;
- this.battle.Layer.ActorAdded += Layer_ActorAdded;
- this.battle.Layer.MessageReceived += Layer_MessageReceived;
- this.timer1.Enabled = true;
- this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
- this.pictureBox1.KeyDown += new KeyEventHandler(pictureBox1_KeyDown);
- this.display.GenShowItems(this.toolStripDropDownButton2);
- foreach (SyncMode mode in Enum.GetValues(typeof(SyncMode)))
- {
- ToolStripButton item = new ToolStripButton(mode.ToString());
- item.Tag = mode;
- item.Click += item_SyncMode_Click;
- item.CheckOnClick = true;
- drop_SyncMode.DropDownItems.Add(item);
- if (battle.Layer.ActorSyncMode == mode)
- {
- item.Checked = true;
- }
- }
- this.display.BindMessageFilter(pictureBox1);
- if (event_OnPanelLoaded != null)
- event_OnPanelLoaded.Invoke();
- }
- }
- //-------------------------------------------------------------------------------------------------------------------
- protected virtual void Layer_LayerInit(ZoneLayer layer)
- {
- float scale = Math.Max(
- ((float)pictureBox1.Width) / battle.Layer.Terrain.TotalWidth,
- ((float)pictureBox1.Height) / battle.Layer.Terrain.TotalHeight);
- this.display.setCameraScale(scale, scale);
- }
- protected virtual void Layer_ActorAdded(ZoneLayer layer, ZoneActor actor)
- {
- actor.SendUnitGuard(btn_Guard.Checked);
- actor.IsSkillAutoFocusTarget = btn_autoFocus.Checked;
- }
- protected virtual void Layer_MessageReceived(ZoneLayer layer, IMessage e)
- {
- if (e is TestMessageBox)
- {
- TestMessageBox over = e as TestMessageBox;
- MessageBox.Show(over.msg, e.GetType().Name);
- }
- else if (e is ServerExceptionB2C)
- {
- ServerExceptionB2C over = e as ServerExceptionB2C;
- MessageBox.Show(over.Message + "\n" + over.StackTrace, e.GetType().Name);
- }
- }
- //-------------------------------------------------------------------------------------------------------------------
- #region Events
- private OnPanelLoadedHandler event_OnPanelLoaded;
- private OnPanelClosedHandler event_OnPanelClosed;
- private OnTimerBeginUpdateHandler event_OnTimerBeginUpdate;
- private OnTimerUpdateHandler event_OnTimerUpdate;
- private OnNetworkViewClickedHandler event_OnNetworkViewClicked;
- private OnExitClickedHandler event_OnExitClicked;
- private DisplayActor_OnRenderHandler event_DisplayActor_OnRender;
- private DisplayLayer_OnRenderHUDHandler event_DisplayLayer_OnRenderHUD;
- private DisplayLayer_OnRenderLayerHandler event_DisplayLayer_OnRenderLayer;
- public event OnPanelLoadedHandler OnPanelLoaded { add { event_OnPanelLoaded += value; } remove { event_OnPanelLoaded -= value; } }
- public event OnPanelClosedHandler OnPanelClosed { add { event_OnPanelClosed += value; } remove { event_OnPanelClosed -= value; } }
- public event OnTimerBeginUpdateHandler OnTimerBeginUpdate { add { event_OnTimerBeginUpdate += value; } remove { event_OnTimerBeginUpdate -= value; } }
- public event OnTimerUpdateHandler OnTimerUpdate { add { event_OnTimerUpdate += value; } remove { event_OnTimerUpdate -= value; } }
- public event OnNetworkViewClickedHandler OnNetworkViewClicked { add { event_OnNetworkViewClicked += value; } remove { event_OnNetworkViewClicked -= value; } }
- public event OnExitClickedHandler OnExitClicked { add { event_OnExitClicked += value; } remove { event_OnExitClicked -= value; } }
- public event DisplayActor_OnRenderHandler DisplayActor_OnRender { add { event_DisplayActor_OnRender += value; } remove { event_DisplayActor_OnRender -= value; } }
- public event DisplayLayer_OnRenderHUDHandler DisplayLayer_OnRenderHUD { add { event_DisplayLayer_OnRenderHUD += value; } remove { event_DisplayLayer_OnRenderHUD -= value; } }
- public event DisplayLayer_OnRenderLayerHandler DisplayLayer_OnRenderLayer { add { event_DisplayLayer_OnRenderLayer += value; } remove { event_DisplayLayer_OnRenderLayer -= value; } }
- public delegate void OnPanelLoadedHandler();
- public delegate void OnPanelClosedHandler();
- public delegate void OnTimerBeginUpdateHandler(int intervalMS);
- public delegate void OnTimerUpdateHandler(int intervalMS);
- public delegate void OnNetworkViewClickedHandler();
- public delegate void DisplayActor_OnRenderHandler(Graphics g, DisplayGameUnit unit);
- public delegate void DisplayLayer_OnRenderHUDHandler(Graphics g);
- public delegate void DisplayLayer_OnRenderLayerHandler(Graphics g);
- public delegate void OnExitClickedHandler();
- #endregion
- //-------------------------------------------------------------------------------------------------------------------
- #region _主更新_
- public virtual void syncUI()
- {
- // sync ui
- foreach (ToolStripButton btn in drop_SyncMode.DropDownItems)
- {
- btn.Checked = ((SyncMode)btn.Tag == battle.Layer.ActorSyncMode);
- }
- if (btn_Guard.Checked != IsActorGuard)
- {
- btn_Guard.Checked = IsActorGuard;
- }
- // chk_ShowTerrain.Checked = this.display.ShowTerrain;
- // chk_ShowGrid.Checked = this.display.ShowGrid;
- // chkShowAttackRangeToolStripMenuItem.Checked = this.display.ShowAttackRange;
- // chkShowDamageRange.Checked = this.display.ShowDamageRange;
- // chkShowGuardRangeToolStripMenuItem.Checked = this.display.ShowGuardRange;
- // chkShowHP.Checked = this.display.ShowHP;
- // chkShowName.Checked = this.display.ShowName;
- // chkShowZ.Checked = this.display.ShowZ;
- // chkShowLog.Checked = this.display.ShowLog;
- }
- public virtual void updateBattle(int intervalMS)
- {
- syncUI();
- if (event_OnTimerBeginUpdate != null)
- event_OnTimerBeginUpdate(intervalMS);
- int totalMS = (int)(intervalMS * TurboX);
- while (totalMS > 0)
- {
- int tick = Math.Min(totalMS, intervalMS);
- this.display.update(tick);
- totalMS -= intervalMS;
- }
- this.display.update_flush();
- if (event_OnTimerUpdate != null)
- {
- event_OnTimerUpdate(intervalMS);
- }
- if (displayActor == null && display.DisplayActor != null)
- {
- displayActor = display.DisplayActor;
- displayActor.OnRender += actor_render;
- }
- this.pictureBox1.Refresh();
- btnTurbo.Text = "加速x" + TurboX;
- }
- protected virtual void timer1_Tick(object sender, EventArgs e)
- {
- try
- {
- long curTime = CommonLang.CUtils.CurrentTimeMS;
- if (mLastUpdateTime == 0)
- {
- mLastUpdateTime = curTime;
- }
- int intervalMS = (int)(curTime - mLastUpdateTime);
- this.mLastUpdateTime = curTime;
- updateBattle(intervalMS);
- }
- catch (Exception err)
- {
- MessageBox.Show(err.Message + "\n" + err.StackTrace);
- }
- }
- private void actor_render(Graphics g, DisplayGameUnit unit)
- {
- if (event_DisplayActor_OnRender != null)
- {
- event_DisplayActor_OnRender.Invoke(g, unit);
- }
- }
- private void display_OnRenderHUD(Graphics g, DisplayLayerWorld world)
- {
- if (event_DisplayLayer_OnRenderHUD != null)
- {
- event_DisplayLayer_OnRenderHUD.Invoke(g);
- }
- }
- private void display_OnRenderLayer(Graphics g, DisplayLayerWorld world)
- {
- if (event_DisplayLayer_OnRenderLayer != null)
- {
- event_DisplayLayer_OnRenderLayer.Invoke(g);
- }
- }
- protected virtual void item_SyncMode_Click(object sender, EventArgs e)
- {
- ToolStripButton item = sender as ToolStripButton;
- if (item != null)
- {
- battle.Layer.ActorSyncMode = (SyncMode)item.Tag;
- }
- }
- #endregion
- //--------------------------------------------------------------------------------------------
- #region _绘制_
- private float mouseX, mouseY;
- private Vector2 pic_lastMouesDown;
- private Vector2 pic_lastCameraPos;
- protected virtual void pictureBox1_KeyDown(object sender, KeyEventArgs e)
- {
- try
- {
- if (e.Shift)
- {
- display.ActorUseItem(e);
- }
- else
- {
- if (display.SelectedObject != null)
- {
- display.ActorLaunchSkill(e, display.SelectedObject.ZoneObjectData);
- }
- else
- {
- display.ActorLaunchSkill(e, mouseX, mouseY);
- }
- }
- }
- catch (Exception err)
- {
- Console.WriteLine(err.Message);
- }
- }
- protected virtual void pictureBox1_MouseDown(object sender, MouseEventArgs e)
- {
- pictureBox1.Focus();
- try
- {
- if (battle != null)
- {
- pic_lastMouesDown = new Vector2(e.Location.X, e.Location.Y);
- pic_lastCameraPos = new Vector2(display.CameraX, display.CameraY);
- float wx = mouseX = display.screenToWorldX(e.X);
- float wy = mouseY = display.screenToWorldY(e.Y);
- if (e.Button == MouseButtons.Left)
- {
- if (Keyboard.IsShiftDown)
- {
- BattleClient.Actor.SendUnitLaunchNormalAttack(wx, wy);
- }
- else
- {
- DisplayLayerObject selected = display.PickObject(wx, wy);
- ZoneItem item = pictureBox1_CheckPickItem();
- if (item != null)
- {
- BattleClient.Actor.SendUnitPickObject(item.ObjectID);
- }
- else if (selected is DisplayGameUnit)
- {
- DisplayGameUnit su = selected as DisplayGameUnit;
- if (su.Data.Force != BattleClient.Actor.Force)
- {
- BattleClient.Actor.SendUnitFocuseTarget(selected.ObjectID);
- }
- else
- {
- BattleClient.Actor.SendUnitPickObject(selected.ObjectID);
- }
- }
- else if (selected is DisplayGameItem)
- {
- BattleClient.Actor.SendUnitPickObject(selected.ObjectID);
- }
- else if (selected is DisplayGameSpell)
- {
- }
- }
- }
- else if (e.Button == MouseButtons.Right)
- {
- if (IsFreeView)
- {
- }
- else
- {
- if (Keyboard.GetKeyState(Keys.A))
- {
- if (BattleClient.Actor.IsGuard)
- {
- BattleClient.Actor.SendUnitAttackMoveTo(wx, wy, true);
- }
- else
- {
- BattleClient.Actor.AddAgent(new ActorMoveAgent(wx, wy, 0.1f));
- }
- }
- else if (BattleClient.Actor.IsGuard)
- {
- BattleClient.Actor.SendUnitAttackMoveTo(wx, wy, false);
- }
- else
- {
- BattleClient.Actor.SendUnitAxis(
- mouseX - BattleClient.Actor.X,
- mouseY - BattleClient.Actor.Y);
- }
- }
- }
- }
- }
- catch (Exception err)
- {
- Console.WriteLine(err.Message);
- }
- }
- protected virtual void pictureBox1_MouseMove(object sender, MouseEventArgs e)
- {
- try
- {
- if (battle != null)
- {
- float wx = mouseX = display.screenToWorldX(e.X);
- float wy = mouseY = display.screenToWorldY(e.Y);
- if (IsFreeView)
- {
- }
- else
- {
- if (Keyboard.IsShiftDown)
- {
- BattleClient.Actor.SendUnitFaceTo(wx, wy);
- }
- }
- if (e.Button == MouseButtons.Left)
- {
- }
- else if (e.Button == MouseButtons.Right)
- {
- if (IsFreeView)
- {
- if (pic_lastMouesDown != null)
- {
- float x = pic_lastCameraPos.X + display.screenToWorldSizeX(pic_lastMouesDown.X - e.X);
- float y = pic_lastCameraPos.Y + display.screenToWorldSizeY(pic_lastMouesDown.Y - e.Y);
- display.setCamera(x, y);
- }
- }
- else
- {
- if (BattleClient.Actor.IsGuard)
- {
- BattleClient.Actor.SendUnitMove(wx, wy);
- }
- else
- {
- BattleClient.Actor.SendUnitAxis((float)Math.Atan2(
- mouseY - BattleClient.Actor.Y,
- mouseX - BattleClient.Actor.X));
- }
- }
- }
- this.pictureBox1_CheckPickItem();
- }
- }
- catch (Exception err)
- {
- Console.WriteLine(err.Message);
- }
- }
- protected virtual void pictureBox1_MouseUp(object sender, MouseEventArgs e)
- {
- pic_lastMouesDown = null;
- pic_lastCameraPos = null;
- if (battle != null)
- {
- float wx = mouseX = display.screenToWorldX(e.X);
- float wy = mouseY = display.screenToWorldY(e.Y);
- if (battle.Actor != null)
- {
- battle.Actor.SendUnitStopMove();
- }
- }
- }
- protected virtual void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
- {
- try
- {
- int d = CMath.getDirect(e.Delta);
- if (d > 0)
- {
- float newD = display.getCameraScale() * 1.1f;
- display.setCameraScale(newD, newD);
- }
- else if (d < 0)
- {
- float newD = display.getCameraScale() / 1.1f;
- display.setCameraScale(newD, newD);
- }
- }
- catch (Exception err)
- {
- Console.WriteLine(err.Message);
- }
- pictureBox1.Refresh();
- }
- protected virtual void pictureBox1_Paint(object sender, PaintEventArgs e)
- {
- if (battle != null && battle.Layer != null)
- {
- try
- {
- this.display.setWindow(new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height));
- if (!IsFreeView && battle.Actor != null)
- {
- this.display.setCamera(battle.Actor.X, battle.Actor.Y);
- }
- this.display.render(e.Graphics);
- this.pictureBox1_CheckPickItem();
- }
- catch (Exception err)
- {
- log.Error(err.Message, err);
- }
- }
- }
- protected virtual ZoneItem pictureBox1_CheckPickItem()
- {
- if (BattleClient.Actor != null)
- {
- pictureBox1.Cursor = Cursors.Default;
- ZoneItem item = BattleClient.Layer.GetNearPickableItem(BattleClient.Actor);
- if (item != null)
- {
- if (CMath.includeRoundPoint(item.X, item.Y, item.Info.BodySize, mouseX, mouseY))
- {
- pictureBox1.Cursor = Cursors.Hand;
- return item;
- }
- }
- }
- return null;
- }
- #endregion
- //--------------------------------------------------------------------------------------------
- #region _控制角色_
- private void btn_Guard_Click(object sender, EventArgs e)
- {
- if (battle.Layer != null)
- {
- if (battle.Actor != null)
- {
- battle.Actor.SendUnitGuard(btn_Guard.Checked);
- }
- }
- }
- private void btn_autoFocus_Click(object sender, EventArgs e)
- {
- if (battle.Layer != null)
- {
- if (battle.Actor != null)
- {
- battle.Actor.IsSkillAutoFocusTarget = btn_autoFocus.Checked;
- }
- }
- }
- private void btn_CleanBuffs_Click(object sender, EventArgs e)
- {
- if (battle.Actor != null)
- {
- foreach (ZoneUnit.BuffState bs in battle.Actor.GetBuffStatus())
- {
- battle.Actor.SendCancelBuff(bs.Data.ID);
- }
- }
- }
- private void pickUnitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (DisplayWorld.SelectedObject != null && DisplayWorld.SelectedObject is DisplayGameUnit)
- {
- DisplayGameUnit su = DisplayWorld.SelectedObject as DisplayGameUnit;
- BattleClient.Actor.SendUnitPickObject(su.ObjectID);
- }
- }
- //--------------------------------------------------------------------------------------------
- private void btn_FreeView_Click(object sender, EventArgs e)
- {
- }
- private void btn_Exit_Click(object sender, EventArgs e)
- {
- if (event_OnExitClicked != null)
- event_OnExitClicked.Invoke();
- }
- private void btn_NewView_Click(object sender, EventArgs e)
- {
- if (event_OnNetworkViewClicked != null)
- event_OnNetworkViewClicked.Invoke();
- }
- #endregion
- //--------------------------------------------------------------------------------------------
- #region _加速_
- private void btn_1X_Click(object sender, EventArgs e)
- {
- TurboX = 1;
- }
- private void btn_2X_Click(object sender, EventArgs e)
- {
- TurboX = 2;
- }
- private void btn_3X_Click(object sender, EventArgs e)
- {
- TurboX = 3;
- }
- private void btn_4X_Click(object sender, EventArgs e)
- {
- TurboX = 4;
- }
- private void btn_5X_Click(object sender, EventArgs e)
- {
- TurboX = 5;
- }
- private void btn_10X_Click(object sender, EventArgs e)
- {
- TurboX = 10;
- }
- private void btn_0_5X_Click(object sender, EventArgs e)
- {
- TurboX = 0.5f;
- }
- private void btn_0_1X_Click(object sender, EventArgs e)
- {
- TurboX = 0.1f;
- }
- private void btn_TurboX_ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string turbo = G2DTextDialog.Show(TurboX.ToString(), "设置加速倍率");
- float turbox = 1;
- if (turbo != null && float.TryParse(turbo, out turbox))
- {
- this.TurboX = turbox;
- }
- }
- #endregion
- //--------------------------------------------------------------------------------------------
- }
- }
|