public void WriteLives(int livesRemaining, DateTime lastEarnedLife) { var entity = new LivesEntity() { LastEarnedLife = lastEarnedLife.ToString(), LivesRemaining = livesRemaining }; var toJson = JsonUtility.ToJson(entity); var result = System.Threading.Tasks.Task.Run(() => FireBaseDatabase.Database.Child(FireBaseSavePaths.PlayerLivesLocation()).SetRawJsonValueAsync(toJson)); if (result.IsCanceled || result.IsFaulted) { Debug.LogWarning(result.Exception); } }
internal void Setup(LivesEntity livesEntity) { if (livesEntity == null) { LastEarnedLife = DateTime.Now; LivesRemaining = 6; } else { LivesRemaining = livesEntity.LivesRemaining; if (DateTime.TryParse(livesEntity.LastEarnedLife, out var time)) { LastEarnedLife = time; WorkOutLivesEarned(); } } }
public async Task ReadLivesAsync() { try { await FireBaseDatabase.Database.Child(FireBaseSavePaths.PlayerLivesLocation()) .GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { } else if (task.IsCompleted) { try { DataSnapshot snapshot = task.Result; string info = snapshot?.GetRawJsonValue()?.ToString(); LivesEntity result = null; if (info != null) { result = JsonUtility.FromJson <LivesEntity>(info); } LivesManager.Instance.Setup(result); } catch (Exception ex) { Debug.LogError(ex); } } }); } catch (Exception ex) { Debug.LogError(ex); } }
void CreateEntities() { EdgeEntity.Create(_engine, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_EDGE), new Vector2(0, Constants.Pong.PLAYFIELD_HEIGHT / 2), new Vector2(0, -1)); // Top edge points down EdgeEntity.Create(_engine, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_EDGE), new Vector2(0, -Constants.Pong.PLAYFIELD_HEIGHT / 2), new Vector2(0, 1)); // Bottom edge points up // Field background FieldBackgroundEntity.Create(_engine, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_FIELD_BACKGROUND)); // Player 1 goal GoalEntity.Create(_engine, _player1, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_GOAL), new Vector2(-Constants.Pong.PLAYFIELD_WIDTH / 2 + Constants.Pong.GOAL_WIDTH / 2, 0)); // Player 2 goal GoalEntity.Create(_engine, _player2, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_GOAL), new Vector2(Constants.Pong.PLAYFIELD_WIDTH / 2 - Constants.Pong.GOAL_WIDTH / 2, 0)); Entity paddle1 = PaddleEntity.Create(_engine, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_PADDLE), new Vector2(-Constants.Pong.PADDLE_STARTING_X, Constants.Pong.PADDLE_STARTING_Y), new Vector2(1, 0)); // Left paddle normal points right Entity paddle2 = PaddleEntity.Create(_engine, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_PADDLE), new Vector2(Constants.Pong.PADDLE_STARTING_X, Constants.Pong.PADDLE_STARTING_Y), new Vector2(-1, 0)); // Right paddle normal points left paddle1.AddComponent(new PlayerComponent(_player1)); paddle2.AddComponent(new PlayerComponent(_player2)); if (Player1 is AIPlayer) { paddle1.AddComponent(new AIComponent(Player1 as AIPlayer)); } if (Player2 is AIPlayer) { paddle2.AddComponent(new AIComponent(Player2 as AIPlayer)); } // Lives LivesEntity.Create(_engine, Content.Load <BitmapFont>(Constants.Resources.FONT_PONG_LIVES), new Vector2(Constants.Pong.LIVES_LEFT_POSITION_X, Constants.Pong.LIVES_POSITION_Y), _player1, Constants.Pong.LIVES_COUNT); BallEntity.CreateWithoutBehavior(_engine, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_BALL), new Vector2(Constants.Pong.LIVES_ICON_LEFT_POSITION_X, Constants.Pong.LIVES_POSITION_Y), 2); LivesEntity.Create(_engine, Content.Load <BitmapFont>(Constants.Resources.FONT_PONG_LIVES), new Vector2(Constants.Pong.LIVES_RIGHT_POSITION_X, Constants.Pong.LIVES_POSITION_Y), _player2, Constants.Pong.LIVES_COUNT); BallEntity.CreateWithoutBehavior(_engine, Content.Load <Texture2D>(Constants.Resources.TEXTURE_PONG_BALL), new Vector2(Constants.Pong.LIVES_ICON_RIGHT_POSITION_X, Constants.Pong.LIVES_POSITION_Y), 2); }