public void Update(float timeStep) { if (!IsActive || IsDead) { Velocity = Vector2Int.Zero; return; } m_MoveTimer += timeStep; if (IsOnGround) { m_GravityTimer = 0; Velocity = new Vector2Int(Velocity.X, 0); } else { m_GravityTimer += timeStep; } int speedY = Velocity.Y; if (speedY > c_MaxSpeedY) { speedY = c_MaxSpeedY; } Velocity = new Vector2Int(0, speedY); if (m_MoveTimer > c_MoveDuration) { Velocity += new Vector2Int(Direction == CharacterDirection.Right ? 1 : -1, 0); m_MoveTimer = 0; } if (m_GravityTimer > c_GravityTimePerPixel) { Velocity += new Vector2Int(0, 1); m_GravityTimer = 0; } FrameLogger.Log(string.Format("{0}- HP: [{1}], OnGnd: [{2}]", Id, Health, IsOnGround)); }
public void Update(float timeStep) { m_TowerMoveTimer += timeStep; // move tower, recycle platforms, generate new platforms if (m_TowerMoveTimer > m_TowerMoveDuration) { TowerTopNode.Translate(new DYTA.Math.Vector2Int(0, -1)); m_TowerMoveTimer = 0; // recycle for (int i = 0; i < m_AllPlatforms.Count; i++) { var platform = m_AllPlatforms[i]; if (TowerTopNode.Bounds.Position.Y - 2 + platform.Collider.Position.Y < 0) { platform.IsActive = false; } } m_SpawnPlatformCounter++; if (m_SpawnPlatformCounter >= c_PlatformOffsetY) { m_SpawnPlatformCounter = 0; if (m_PreviousPlatform == null) { m_PreviousPlatform = CreateNormalPlatform( new Vector2Int(m_TowerSize.X / 2 - c_PlatformWidth / 2 - 1, c_PlatformInitialHeight + c_PlatformOffsetY), new Vector2Int(c_PlatformWidth, 1)); } else { var pos = m_PreviousPlatform.Collider.Position; pos.Y += c_PlatformOffsetY; pos.X = m_RandomGenerator.Next(1, m_TowerSize.X - c_PlatformWidth - 1); int rndToken = m_RandomGenerator.Next(0, 10); if (rndToken < m_SpikeSpawnThreshold) { rndToken = m_RandomGenerator.Next(0, 10); m_SpikeSpawnThreshold -= 2; // spawn normal platform if (rndToken < 6) { m_PreviousPlatform = CreateNormalPlatform(pos, new Vector2Int(c_PlatformWidth, 1)); } // spawn sliding platform else { m_PreviousPlatform = CreateSlidingPlatform(pos, new Vector2Int(c_PlatformWidth, 1)); } } // spawn spike platform else { m_PreviousPlatform = CreateSpikePlatform(pos, new Vector2Int(c_PlatformWidth, 1)); m_SpikeSpawnThreshold += 3; if (m_SpikeSpawnThreshold > c_SpikeSpawnInitialThreshold) { m_SpikeSpawnThreshold = c_SpikeSpawnInitialThreshold; } } } m_LevelCounter++; TotalLevelCounter++; if (m_LevelCounter >= 10) { m_LevelCounter = 0; Difficulty++; if (Difficulty < c_MaxDifficulty) { m_TowerMoveDuration -= c_AddUpPer10Level; } } } } FrameLogger.Log("Difficulty - " + Difficulty); FrameLogger.Log("TotalLevelCounter - " + TotalLevelCounter); FrameLogger.Log("SpikeThreshold - " + m_SpikeSpawnThreshold); // update character for (int i = 0; i < Characters.Count; i++) { var character = Characters[i]; character.Update(timeStep); } // update platform for (int i = 0; i < m_AllPlatforms.Count; i++) { var platform = m_AllPlatforms[i]; platform.Update(timeStep); } // update character movement, collision detection for (int i = 0; i < Characters.Count; i++) { var character = Characters[i]; var velocity = character.Velocity; // check velocity collision for (int j = 0; j < m_AllColliders.Count; j++) { var collider = m_AllColliders[j]; if (!collider.IsActive) { continue; } if (collider != character) { // static object if (collider is StaticWall || collider is DeathZone) { var bounds = new RectInt(collider.Collider.Position + new Vector2Int(0, -(TowerTopNode.Bounds.Position.Y - 1)), collider.Collider.Size); var col = ClampVelocity2D(character.Collider, bounds, velocity, out velocity); if (col.IsTrue) { FrameLogger.Log("TYPE " + collider.GetType()); collider.OnCharacterCollision(character); } } // dynamic object else { var col = ClampVelocity2D(character.Collider, collider.Collider, velocity, out velocity); if (col.IsTrue) { FrameLogger.Log("TYPE " + collider.GetType()); collider.OnCharacterCollision(character); } } } } // move var newPos = character.Collider.Position + velocity; character.SetPosition(newPos); // ground checking after movement character.IsOnGround = false; var bottomBox = new RectInt(character.Collider.Min.X, character.Collider.Max.Y + 1, character.Collider.Width, 1); for (int j = 0; j < m_AllColliders.Count; j++) { var collider = m_AllColliders[j]; if (!collider.IsActive) { continue; } // check bottom bool standOnThis = collider.Collider.Overlap(bottomBox); // invoke Enter / Exit event if (standOnThis) { // not on this platform the previous frame if (!collider.IsCharacterOnThisCollider(character)) { collider.OnCharacterStepOn(character); } } else { // on this platform the previous frame if (collider.IsCharacterOnThisCollider(character)) { collider.OnCharacterLiftOff(character); } } character.IsOnGround |= standOnThis; } // falling out of the zone if (character.Collider.Position.Y + TowerTopNode.Bounds.Position.Y + 1 > m_TowerSize.Y) { character.AddHealth(-999); character.IsActive = false; } if (character.Collider.Position.Y + TowerTopNode.Bounds.Position.Y <= 0) { character.IsActive = false; } //FrameLogger.Log(velocity + newPos.ToString()); } }