Beispiel #1
0
        public override void Handle(Event e)
        {
            Station s;

            switch (e.GetEventType())
            {
            case EventType.NameEvent:
                NameEvent ne = (NameEvent)e;
                Game.World.GetPlayer(ne.ID).Name  = ne.Name;
                Game.World.GetPlayer(ne.ID).Color = ne.Color;
                break;

            case EventType.UnitMoveEvent:
                UnitMoveEvent ume = (UnitMoveEvent)e;
                Unit          u   = Game.World.Units[ume.ID];

                u.GetAnimateable().AddAnimation(
                    new PositionAnimation(
                        150,
                        CurveType.EaseOut,
                        u.fPosition,
                        new Vector2(ume.x, ume.y)
                        )
                    );

                u.MoveTo(ume.x, ume.y);

                if (ume.Local)
                {
                    engine.NetClient.Send(
                        new NetMessage3(
                            NM3MessageType.unit_move,
                            u.ID,
                            ume.x, ume.y
                            )
                        );
                }
                break;

            case EventType.BuildStationEvent:
                BuildStationEvent bse = (BuildStationEvent)e;
                engine.NetClient.Send(
                    new NetMessage3(
                        NM3MessageType.station_create,
                        bse.Owner, //isn't this always us?
                                   //oh, right, but since the same message
                                   //is used server -> client,
                                   //it provides owner as well.
                        -1,        //client doesn't give id, handled serverside
                        bse.x,
                        bse.y
                        )
                    );
                break;

            case EventType.BuildUnitEvent:
                BuildUnitEvent bue = (BuildUnitEvent)e;
                engine.NetClient.Send(
                    new NetMessage3(
                        NM3MessageType.unit_build,
                        (int)bue.UnitType,
                        bue.x,
                        bue.y
                        )
                    );
                break;

            case EventType.CreateUnitEvent:
                CreateUnitEvent cue = (CreateUnitEvent)e;
                Game.World.SpawnUnit(
                    new Unit(
                        Game.World,
                        cue.UnitType,
                        cue.Owner,
                        cue.ID,
                        cue.x,
                        cue.y
                        )
                    );
                break;

            case EventType.PlayerDisconnect:
                PlayerDisconnectEvent pde = (PlayerDisconnectEvent)e;
                Game.Log.Add(
                    string.Format(
                        "{0} disconnected.",
                        Game.World.GetPlayer(pde.id).Name
                        )
                    );

                Game.World.RemovePlayer(pde.id);
                break;

            case EventType.SetProjectEvent:
                SetProjectEvent spe = (SetProjectEvent)e;

                s = Game.World.Map.Stations[spe.Station];

                int time;

                switch (spe.ProjectType)
                {
                case ProjectType.UnitProject:
                    UnitType type = UnitType.GetType(
                        (UnitTypes)spe.Project
                        );

                    time = type.ConstructionTime;
                    break;

                case ProjectType.TechProject:
                    Tech tech = Tech.Techs[(TechID)spe.Project];

                    time = tech.ResearchTime;
                    break;

                default:
                    throw new ArgumentException();
                }

                engine.NetClient.Send(
                    new NetMessage3(
                        NM3MessageType.station_set_project,
                        spe.Owner,
                        s.ID,
                        time,
                        (int)spe.ProjectType,
                        spe.Project
                        )
                    );

                break;

            case EventType.ProjectFinishedEvent:
                ProjectFinishedEvent pfe = (ProjectFinishedEvent)e;
                Project p = pfe.Project;
                switch (p.GetProjectType())
                {
                case ProjectType.UnitProject:
                    //this is a bit hacky, but we specifically only
                    //want to send this netmessage from the client
                    //*OWNING* the project, whereas with techprojects
                    //we can handle them just fine on our own side.
                    //this is because we need to keep our unit ids
                    //synchronized.
                    if (p.Station.Tile.Unit == null)
                    {
                        if (
                            Game.LocalPlayer != null &&         //not serverside
                            p.Owner == Game.LocalPlayer.ID      //and our
                            )
                        {
                            Push(
                                new BuildUnitEvent(
                                    (UnitTypes)p.GetProject(),
                                    p.Owner,
                                    p.Station.Position.X,
                                    p.Station.Position.Y
                                    )
                                );
                        }

                        p.SetFinished();
                    }
                    break;

                case ProjectType.TechProject:
                    Game.World.Players[p.Owner].Tech.Add(
                        (TechID)p.GetProject()
                        );

                    p.SetFinished();
                    break;

                default:
                    throw new ArgumentException();
                }
                break;

            default:
                throw new ArgumentException();
            }
        }