Example #1
0
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_game);

            firstTime = true;

            // By default, enable auto camera
            autoCamera = true;

            preferences = new AppPreferences(this);

            // Get main view
            mainView            = FindViewById <CoordinatorLayout>(Resource.Id.layout_game);
            mainView.Visibility = ViewStates.Invisible;

            // Get last known location
            locationHandler = new LocationHandler(this);
            userLocation    = await locationHandler.GetLastKnownLocationAsync();

            // Get map and listen when it's ready
            var mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.map);

            mapFragment.GetMapAsync(this);

            var textDebugLocation = FindViewById <TextView>(Resource.Id.text_debug_location);

            locationHandler.OnLocationUpdate += location =>
            {
                // TODO: Check speed etc. here
                // TODO: Show a loading dialog until we get the first position

                Log.Info("POSITION", $"Long={location.Longitude} Lat={location.Latitude} Speed={location.Speed}");

                textDebugLocation.Text = $"Accuracy: {location.Accuracy}%\nSpeed:    {location.Speed} m/s";

                // Update user location
                userLocation = location;

                if (location.Speed > 0 && battleView.Visibility != ViewStates.Visible)
                {
                    AccountManager.CurrentUser.Health += 1;
                }

                // If map hasn't loaded yet, ignore player marker
                if (playerMarker == null)
                {
                    return;
                }

                playerMarker.Position = location.ToLatLng();

                if (autoCamera)
                {
                    googleMap.AnimateCamera(CameraUpdateFactory.NewLatLng(location.ToLatLng()));
                }
            };

            // Show profile when clicking on button
            fabUser = FindViewById <FloatingActionButton>(Resource.Id.fabUser);
            fabMap  = FindViewById <FloatingActionButton>(Resource.Id.fabGame);

            fabUser.Click += (sender, args) => ToggleProfile(true);
            fabMap.Click  += (sender, args) => ToggleProfile(false);

            // Set up profile view
            profileView = FindViewById <ViewStub>(Resource.Id.stub_profile).Inflate();

            // Hide it in code to be able to see it when designing in xml
            profileView.Visibility = ViewStates.Invisible;

            // Set values on profile
            profileView.FindViewById <ImageButton>(Resource.Id.button_settings).Click += (sender, args) =>
                                                                                         StartActivity(new Intent(this, typeof(SettingsActivity)));

            equippedCubes     = new ImageButton[3];
            equippedCubeNames = new TextView[3];

            equippedCubes[0] = profileView.FindViewById <ImageButton>(Resource.Id.inventory_companion_1);
            equippedCubes[1] = profileView.FindViewById <ImageButton>(Resource.Id.inventory_companion_2);
            equippedCubes[2] = profileView.FindViewById <ImageButton>(Resource.Id.inventory_companion_3);

            // Show companion info on click
            for (var i = 0; i < equippedCubes.Length; i++)
            {
                // Local copy of i
                var i2 = i;

                equippedCubes[i].Click += (sender, args) =>
                                          Alert.ShowCompanionInfo(this, AccountManager.CurrentUser.EquippedCompanions[i2]);
            }

            equippedCubeNames[0] = profileView.FindViewById <TextView>(Resource.Id.text_companion_name_1);
            equippedCubeNames[1] = profileView.FindViewById <TextView>(Resource.Id.text_companion_name_2);
            equippedCubeNames[2] = profileView.FindViewById <TextView>(Resource.Id.text_companion_name_3);

            // Avoid clicking through profile view
            profileView.Touch += (sender, args) =>
                                 args.Handled = true;

            // Inflate battle view
            battleView            = FindViewById <ViewStub>(Resource.Id.stub_battle).Inflate();
            battleView.Visibility = ViewStates.Invisible;

            // Avoid clicking through battle view
            battleView.Touch += (sender, args) =>
                                args.Handled = true;

            // Check if using fullscreen HUD
            if (preferences.Fullscreen)
            {
                void HudUpdate()
                {
                    var batteryManager = (BatteryManager)GetSystemService(BatteryService);
                    var hud            = mainView.FindViewById <TextView>(Resource.Id.text_game_hud);

                    hud.SetTextColor(IsMapDay ? Color.Black : Color.White);

                    Task.Run(() =>
                    {
                        while (preferences.Fullscreen)
                        {
                            var time    = DateTime.Now.ToString("HH:mm");
                            var battery = batteryManager.GetIntProperty((int)BatteryProperty.Capacity);
                            hud.Text    = $"{time} | {battery}%";

                            Thread.Sleep(1000);
                        }
                    });
                }

                // Wrap it in a function to avoid an async warning
                HudUpdate();
            }

            // Setup debug mode
            FindViewById <Button>(Resource.Id.button_debug_focus_player).Click += (sender, args) =>
                                                                                  googleMap.AnimateCamera(CameraUpdateFactory.NewLatLng(userLocation.ToLatLng()));

            FindViewById <Button>(Resource.Id.button_debug_respawn).Click += (sender, args) =>
                                                                             AccountManager.CurrentUser.HealthPercentage = 100;

            battleInfoView   = FindViewById <LinearLayout>(Resource.Id.layout_battle_info);
            battleInfo       = BottomSheetBehavior.From(battleInfoView);
            battleInfo.State = BottomSheetBehavior.StateHidden;

            // Avoid clicking through battle info view
            battleInfoView.Touch += (sender, args) =>
                                    args.Handled = true;

            battleInfoView.FindViewById <Button>(Resource.Id.button_battle_info_fight).Click +=
                (sender, args) => StartBattle();

            // Add auto camera toggle
            FindViewById <Switch>(Resource.Id.switch_debug_auto_camera).CheckedChange += (sender, args) =>
                                                                                         autoCamera = args.IsChecked;

            if (!MainActivity.DebugMode)
            {
                FindViewById <LinearLayout>(Resource.Id.layout_debug_tools).Visibility = ViewStates.Gone;
            }

            // So we can manually set a battle range
            battleRange = preferences.DevBattleRange;

            SetUser();
        }