public void InitializeEventsOnce() { Events.ServerPlayerHello += e => { new Handshake().VerifyArray(e.handshake); // Map.WriteLine("handshake ok"); // at this point we know it is our server // at the other end // atleast it seems to knows our protocol //Map.WriteLine("Howdy, " + e.name); //// now we know our player id. //this.MapSharedState.RemoteObjects[e.user] = this.MapSharedState.LocalObjects; // how long should we wait for the map? FirstMapLoader.Wait(TimeoutAction.LongOperation); if (e.user_with_map == -1) { Map.WriteLine("got ServerPlayerHello"); FirstMapLoader.Signal( delegate { Map.WriteLine("using generated map"); } ); } // this causes other events to be attached MyIdentity.Value = e; }; MyIdentity.ValueChanged += () => MapInitialized.ContinueWhenDone(CreateLocalCoPlayer); MyIdentity.ValueChanged += () => MapInitialized.ContinueWhenDone(InitializeWalkTo); MyIdentity.ValueChanged += InitializeOtherEvents; }
public void InitializeOtherEvents() { // todo: events need to automatically // queue based on message id and wait timer // currently this is done manually. Events.ServerPlayerJoined += e => { MapInitialized.ContinueWhenDone( delegate { //Map.WriteLine("joined: " + e.name); PlayerAdvertise(); } ); }; Events.UserPlayerAdvertise += e => { MapInitialized.ContinueWhenDone( delegate { PlayerAdvertise(e); } ); }; Events.ServerPlayerLeft += e => { if (UserEnterEndLevelMode_LastSender != null) { if (UserEnterEndLevelMode_LastSender.Identity.user == e.user) { Map.WriteLine("bummer, map owner left..."); // bummer, he was gonna send us a map! // in next 1 to 5 secs we try to use our map 1000.AtDelayDo( delegate { Convert.ToInt32(new Random().NextDouble() * 4000).AtDelayDo( delegate { if (UserEnterEndLevelMode_LastSender == null) { return; } UserEnterEndLevelMode_LastSender = null; Map.WriteLine("sending our map..."); UseOurMapForNextLevel(); } ); } ); } } //Map.WriteLine("left: " + e.name); // self kill the player guard, and remove the coplayer entity this.CoPlayers.Where(k => k.Identity.user == e.user).Where(k => k.Guard != null).ToArray().ForEach(k => k.RemoveFrom(CoPlayers).Guard.TakeDamage(k.Guard.Health, k.WeaponIdentity)); if (this.CoPlayers.Count == 1) { // if ego is left alone reveal exit if (this.Map.HalfOfTheTreasureCollected != null) { this.Map.HalfOfTheTreasureCollected(); } } }; Events.ServerSendMap += e => { // we have been chosen to tell the new guy about current map MapInitializedAndLoaded.ContinueWhenDone( delegate { if (FirstMapLoader.Ready) { WriteSync(); } else { this.Map.WriteLine("we are not ready to send out a map - between levels"); } } ); }; Events.UserSendMap += e => { // got a map, no need to worry about not getting one UserEnterEndLevelMode_LastSender = null; // stop showing score and keep map in sync instead by loading a new map if (this.Map.EnterEndLevelMode_ReadyToContinue != null) { this.Map.EnterEndLevelMode_ReadyToContinue(); this.Map.WriteLine("did we speed up the end game menu?"); } else { this.Map.WriteLine("looks like we cannot speed up the end game menu?"); } var bytestream = e.bytestream; this.Map.WriteLine("got UserSendMap"); FirstMapLoader.Signal( delegate { ReadSync(bytestream); } ); }; // note: you should not listen to non user, non server events Events.UserTakeAmmo += e => { this.Map.AmmoSprites.Where(k => k.ConstructorIndexForSync == e.index).ToArray().ForEach( i => i.RemoveFrom(this.Map.AmmoSprites).RemoveFrom(this.Map.EgoView.Sprites) ); }; Events.UserTakeGold += e => { CoPlayers.Where(k => k.Identity.user == e.user).ForEach(k => k.Score += FlashTreasureHunt.ScoreForGold); this.Map.GoldSprites.Where(k => k.ConstructorIndexForSync == e.index).ToArray().ForEach( i => i.RemoveFrom(this.Map.GoldSprites).RemoveFrom(this.Map.EgoView.Sprites) ); }; Events.UserAddDamageToCoPlayer += e => { this.MapInitializedAndLoaded.ContinueWhenDone( delegate { var DamageOwner = CoPlayers.Single(k => k.Identity.user == e.user).WeaponIdentity; this.DisableAddDamageToCoPlayer = true; this.CoPlayers.Where(k => k.Identity.user == e.target).Where(k => k.Guard != null).ForEach(k => k.Guard.TakeDamage(e.damage, DamageOwner)); this.DisableAddDamageToCoPlayer = false; } ); }; Events.UserFireWeapon += e => { Assets.Default.Sounds.gunshot.play(); this.CoPlayers.Where(k => k.Identity.user == e.user).ForEach(k => k.Guard.PlayShootingAnimation()); }; Events.UserWalkTo += UserWalkTo; Events.UserLookAt += UserLookAt; Events.UserGuardWalkTo += UserGuardWalkTo; Events.UserGuardLookAt += UserGuardLookAt; Events.UserGuardAddDamage += e => { this.MapInitializedAndLoaded.ContinueWhenDone( delegate { this.Map.WriteLine("got UserGuardAddDamage " + e); var DamageOwner = CoPlayers.Single(k => k.Identity.user == e.user).WeaponIdentity; this.DisableGuardAddDamage = true; this.Map.GuardSprites.Where(k => k.ConstructorIndexForSync == e.index).ForEach(k => k.TakeDamage(e.damage, DamageOwner)); this.DisableGuardAddDamage = false; } ); }; Events.UserEnterEndLevelMode += e => { var sender = this.CoPlayers.SingleOrDefault(k => k.Identity.user == e.user); if (sender == null) { Map.WriteLine("must wait until next level is generated"); if (!MapInitializedAndLoaded.Ready) { if (!FirstMapLoader.Ready) { FirstMapLoader.Wait(TimeoutAction.LongOperation); } } return; } sender.Score += FlashTreasureHunt.ScoreForEndLevel; Map.WriteLine("got UserEnterEndLevelMode from " + sender.Identity.name); UserEnterEndLevelMode_LastSender = sender; if (this.AbortGhostMode != null) { this.AbortGhostMode(); } DisableEnterEndLevelMode = true; this.Map.EnterEndLevelMode(); DisableEnterEndLevelMode = false; }; }