Skip to content

Commit

Permalink
Merge pull request #68 from cs3217-2324/refactor-code
Browse files Browse the repository at this point in the history
Remove unused code
  • Loading branch information
JCSnap authored Apr 21, 2024
2 parents 5ff8f77 + c3aca11 commit 04e0404
Show file tree
Hide file tree
Showing 15 changed files with 8 additions and 131 deletions.
4 changes: 0 additions & 4 deletions TheAlienThatEatsTheCarrot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
0A76A4B12BAD54C60067BD6E /* GameRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A76A4B02BAD54C60067BD6E /* GameRenderer.swift */; };
0A76A4BA2BAD61120067BD6E /* CameraComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A76A4B92BAD61120067BD6E /* CameraComponent.swift */; };
0A76A4BE2BAD61F70067BD6E /* GameConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A76A4BD2BAD61F70067BD6E /* GameConstants.swift */; };
0A76A4C42BAE81BD0067BD6E /* CameraSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A76A4C32BAE81BD0067BD6E /* CameraSystem.swift */; };
0A76A4CA2BAEBD2C0067BD6E /* GameStateComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A76A4C92BAEBD2C0067BD6E /* GameStateComponent.swift */; };
0A76A4CE2BAEC4D00067BD6E /* PlayerMovementSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A76A4CD2BAEC4D00067BD6E /* PlayerMovementSystem.swift */; };
0A76A4D02BAF0C410067BD6E /* PlayerRole.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A76A4CF2BAF0C410067BD6E /* PlayerRole.swift */; };
Expand Down Expand Up @@ -237,7 +236,6 @@
0A76A4B02BAD54C60067BD6E /* GameRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameRenderer.swift; sourceTree = "<group>"; };
0A76A4B92BAD61120067BD6E /* CameraComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraComponent.swift; sourceTree = "<group>"; };
0A76A4BD2BAD61F70067BD6E /* GameConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameConstants.swift; sourceTree = "<group>"; };
0A76A4C32BAE81BD0067BD6E /* CameraSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraSystem.swift; sourceTree = "<group>"; };
0A76A4C92BAEBD2C0067BD6E /* GameStateComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameStateComponent.swift; sourceTree = "<group>"; };
0A76A4CD2BAEC4D00067BD6E /* PlayerMovementSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMovementSystem.swift; sourceTree = "<group>"; };
0A76A4CF2BAF0C410067BD6E /* PlayerRole.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerRole.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -935,7 +933,6 @@
0AC860D22BB6A1EE0060EF90 /* Utilities */,
9F8C580D2BAAD71B002E75FB /* MovementSystem.swift */,
0A76A4CD2BAEC4D00067BD6E /* PlayerMovementSystem.swift */,
0A76A4C32BAE81BD0067BD6E /* CameraSystem.swift */,
0AC860D92BB6D7F90060EF90 /* TimerSystem.swift */,
0AC861192BB92D5D0060EF90 /* ScoreSystem.swift */,
0AC861212BBBB9BF0060EF90 /* DamageSystem.swift */,
Expand Down Expand Up @@ -1295,7 +1292,6 @@
0AC861702BC5C09B0060EF90 /* PelletProjectileFactory.swift in Sources */,
0AC8611C2BB933CB0060EF90 /* ScoreComponent.swift in Sources */,
3F96D4E82BCC796D002F4986 /* Character+extensions.swift in Sources */,
0A76A4C42BAE81BD0067BD6E /* CameraSystem.swift in Sources */,
9F8C589E2BAE74F1002E75FB /* TheAlienThatEastsTheCarrot.xcdatamodeld in Sources */,
9F8C58AB2BAEA141002E75FB /* PowerupType.swift in Sources */,
0A76A4A92BAD29310067BD6E /* BlockComponent.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class AchievementManager: ObservableObject, AchievementManagerDelegate {
self.newAchievement = nil
self.storage = UserDefaults.standard
self.achievementGroups = [
KillEnemyAchievementGroup()
KillEnemyAchievementGroup(),
AllTimeAchievementGroup()
]
registerAchievementManagerDelegate()
registerAchievements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class HeadAttackStyle: AttackStyle {

func attack(attacker: Entity, attackee: Entity,
delegate: AttackableDelegate) {
// TODO: Either use renderable component or physics component
guard let attackerPhysicsComponent = delegate.getComponent(of: PhysicsComponent.self, for: attacker),
let attackeePhysicsComponent = delegate.getComponent(of: PhysicsComponent.self, for: attackee) else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation

class ExitBlockFactory: BlockFactory {
override func createComponents() -> [Component] {
// TODO: check if set up properly
let size = CGSize(width: boardObject.width, height: boardObject.height)
let blockComponent = BlockComponent(entity: entity)
let renderableComponent = RenderableComponent(entity: entity,
Expand Down
2 changes: 0 additions & 2 deletions TheAlienThatEatsTheCarrot/GameEngine/GameEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class GameEngine {
createCountdown()

EventManager.shared.postEvent(GameStartEvent())
print("Game started")
}

func pause() {
Expand Down Expand Up @@ -99,7 +98,6 @@ class GameEngine {
PhysicsSystem(nexus: nexus, physicsWorld: physicsWorld),
MovementSystem(nexus: nexus),
TimerSystem(nexus: nexus),
CameraSystem(nexus: nexus),
DamageSystem(nexus: nexus),
FrictionalSystem(nexus: nexus),
CreateNewEntitiesSystem(nexus: nexus),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class PlayerMovementSystem: System {
for player in playerComponents {
updateJumpState(for: player)
applyPhysicsBasedOnControlAction(for: player)
updateCameraBasedOnNewPosition(for: player)
resetPlayerActionIfJump(for: player)
}
}
Expand Down Expand Up @@ -56,8 +55,6 @@ class PlayerMovementSystem: System {
let jumpStateComponent = nexus.getComponent(of: JumpStateComponent.self, for: player.entity) else {
return
}
// TODO: add a system that modifies canJump if the player is standing on an object
// This can either be in the physics system or a separate system
if jumpStateComponent.isGrounded {
jumpStateComponent.remainingJump = jumpStateComponent.maxJump
}
Expand All @@ -82,16 +79,6 @@ class PlayerMovementSystem: System {
}
}

private func updateCameraBasedOnNewPosition(for player: PlayerComponent) {
guard
let camera = nexus.getComponent(of: CameraComponent.self, for: player.entity),
let renderableComponent = nexus.getComponent(of: RenderableComponent.self, for: player.entity)
else {
return
}
camera.updateCameraBoundsFromCenter(center: renderableComponent.position)
}

private func doNothing() {
// Do nothing
}
Expand Down
3 changes: 0 additions & 3 deletions TheAlienThatEatsTheCarrot/LevelDesigner/LevelDesigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ class LevelDesigner {
// MARK: - Delegating to view and model
func add(boardObject: BoardObject, addToView: Bool = true) {
if !level.canAdd(boardObject: boardObject) {
print("cannot add obejct \(boardObject) at \(boardObject.position)")
return
}
level.add(boardObject: boardObject)
// print(">>Level>> object added at \(boardObject.position)")
if addToView {
let id = ObjectIdentifier(boardObject)
view.addImage(id: id, objectType: boardObject.type, center: boardObject.position, width: boardObject.width, height: boardObject.height)
Expand All @@ -132,7 +130,6 @@ class LevelDesigner {

private func loadView(with boardObjects: BoardObjectSet, area: CGRect) {
for boardObject in boardObjects.allObjects {
print("loading object to view \(boardObject.type) \(boardObject.position)")
let id = ObjectIdentifier(boardObject)
view.loadDisplayBounds(area: area)
view.addImage(id: id, objectType: boardObject.type, center: boardObject.position, width: boardObject.width, height: boardObject.height)
Expand Down
2 changes: 0 additions & 2 deletions TheAlienThatEatsTheCarrot/Model/BoardObject/BoardObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

import CoreGraphics

// TODO: merged to GameObject in Component of ECS
protocol BoardObject: AnyObject {
var position: CGPoint { get set }
var width: CGFloat { get set }
var height: CGFloat { get set }
var imageName: String? { get set }
var type: ObjectType { get }

// TODO: remove when ECS is implemented
func move(to newPosition: CGPoint)
func isOverlapping(with boardObject: BoardObject) -> Bool
func contains(point: CGPoint) -> Bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public enum ObjectType: Equatable, Hashable {
case character(CharacterType)
case projectile(ProjectileType)

// TODO: delete
var assetName: String? {
switch self {
case .enemy(let enemyType):
Expand Down
28 changes: 6 additions & 22 deletions TheAlienThatEatsTheCarrot/Persistence/JSONConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ extension Level {
let jsonData = try encoder.encode(jsonLevel)
return String(data: jsonData, encoding: .utf8)
} catch {
print("Error encoding level to JSON: \(error)")
return nil
}
}
Expand All @@ -70,24 +69,24 @@ extension BoardObjectSet {
func toJSONBoardObjectSet() -> JSONBoardObjectSet {
let jsonBlocks = blocks.map { block in
JSONBlock(position: [block.position.x, block.position.y],
blockType: block.blockType.rawValue,
containedPowerupType: block.containedPowerupType?.rawValue)
blockType: block.blockType.rawValue,
containedPowerupType: block.containedPowerupType?.rawValue)
}
let jsonCharacters = characters.map { character in
JSONCharacter(position: [character.position.x, character.position.y],
characterType: character.characterType.rawValue)
characterType: character.characterType.rawValue)
}
let jsonCollectables = collectables.map { collectable in
JSONCollectable(position: [collectable.position.x, collectable.position.y],
collectableType: collectable.collectableType.rawValue)
collectableType: collectable.collectableType.rawValue)
}
let jsonEnemies = enemies.map { enemy in
JSONEnemy(position: [enemy.position.x, enemy.position.y],
enemyType: enemy.enemyType.rawValue)
enemyType: enemy.enemyType.rawValue)
}
let jsonPowerups = powerups.map { powerup in
JSONPowerup(position: [powerup.position.x, powerup.position.y],
powerupType: powerup.powerupType.rawValue)
powerupType: powerup.powerupType.rawValue)
}

return JSONBoardObjectSet(blocks: jsonBlocks,
Expand Down Expand Up @@ -122,7 +121,6 @@ extension Level {
bestTime: jsonLevel.bestTime,
bestCarrot: jsonLevel.bestCarrot)
} catch {
print("Error decoding JSON: \(error)")
return nil
}
}
Expand Down Expand Up @@ -183,18 +181,4 @@ func main() {
bestScore: 0,
bestTime: 0,
bestCarrot: 0)

if let jsonString = level.toJSONString() {
print("JSON String:")
print(jsonString)

if let decodedLevel = Level.fromJSONString(jsonString: jsonString) {
print("Decoded Level:")
print(decodedLevel)
} else {
print("Failed to decode Level from JSON string")
}
} else {
print("Failed to convert Level to JSON string")
}
}
3 changes: 0 additions & 3 deletions TheAlienThatEatsTheCarrot/Persistence/LevelDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ struct LevelDataManager {
let levelNames = levelDatas.compactMap({ $0.name })
return levelNames
} catch {
print("Error fetching level data: \(error)")
return []
}
}
Expand All @@ -112,7 +111,6 @@ struct LevelDataManager {

func fetchEmptyLevel() -> Level? {
guard let fileURL = Bundle.main.url(forResource: "emptyLevel", withExtension: "json") else {
print("Error: emptyLevel.json file not found")
return nil
}
do {
Expand All @@ -123,7 +121,6 @@ struct LevelDataManager {
if let level = jsonString.flatMap({ Level.fromJSONString(jsonString: $0) }) {
return level
} else {
print("Error: Failed to create Level instance from JSON string")
}
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import CoreGraphics

struct PhysicsConstants {
// Physics Constants TODO: adjust based on game design
static let physicsBodyMinimumSize: CGFloat = 0.05
static let restitution: CGFloat = 0.8
static let maxSpeed: CGFloat = 5_000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,6 @@ class GamePlayViewController: UIViewController {
CGPoint(x: position.x * unitSize, y: position.y * unitSize)
}

private func getPositionWithOffsets(for originalPosition: CGPoint) -> CGPoint {
let position = gameEngine.getPlayerPositions().first
let verticalCameraOffset = 1_300.0
var xPosition = originalPosition.x + boardAreaView.frame.width / scale / 2
var yPosition = originalPosition.y + boardAreaView.frame.height / scale / 2 + verticalCameraOffset / scale
if let playerPosition = position {
xPosition -= playerPosition.x
yPosition -= playerPosition.y
}
return toBoardPosition(position: CGPoint(x: xPosition, y: yPosition))
}

// MARK: - game state handling
private func updateCoinCount(_ count: Int) {
coinCountText.text = String(count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class LevelDesignerViewController: UIViewController {
self.unitSize = (frame.maxY - frame.minY) / 50
boardBounds = (min: 0, max: area.size.width * unitSize)
displayBounds = (min: 0, max: frame.maxX - frame.minX)
print("display \(displayBounds) board \(boardBounds)")
}

// MARK: - set up tab bars
Expand Down Expand Up @@ -160,15 +159,13 @@ class LevelDesignerViewController: UIViewController {
/// handle tap action in the board area
@objc func handleBoardTap(_ gesture: UITapGestureRecognizer) {
let tapLocation = relativePixelToAbsoluteUnitPosition(point: gesture.location(in: boardAreaView))
// print("** tap at \(gesture.location(in: boardAreaView)) -> \(tapLocation)")
levelDesigner.handleTap(at: tapLocation, objectType: componentSelected)
}

/// handle long press action in the board area
@objc func handleBoardLongPress(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
let location = relativePixelToAbsoluteUnitPosition(point: gesture.location(in: boardAreaView))
// print("** long press (delete) at \(gesture.location(in: boardAreaView)) -> \(location)")
levelDesigner.handleLongPress(at: location)
}
}
Expand All @@ -178,14 +175,11 @@ class LevelDesignerViewController: UIViewController {
switch gesture.state {
case .began:
let touchPoint = relativePixelToAbsoluteUnitPosition(point: gesture.location(in: boardAreaView))
// print("** long press pan began at \(gesture.location(in: boardAreaView)) -> \(touchPoint)")
levelDesigner.handlePanStart(at: touchPoint)
case .changed:
let touchPoint = relativePixelToAbsoluteUnitPosition(point: gesture.location(in: boardAreaView))
// print("** long press pan began at \(gesture.location(in: boardAreaView)) -> \(touchPoint)")
levelDesigner.handlePanChange(at: touchPoint)
default:
// print("** long press pan end")
levelDesigner.handlePanEnd()
}
}
Expand All @@ -196,7 +190,6 @@ class LevelDesignerViewController: UIViewController {
let translation = gesture.translation(in: boardAreaView)
moveAllImages(scale: translation.x)
gesture.setTranslation(.zero, in: boardAreaView)
// print("** short pan change \(translation.x)")
}
}

Expand All @@ -222,15 +215,12 @@ class LevelDesignerViewController: UIViewController {

let position = absoluteUnitToRelativePixelPosition(point: center)

print(">>view >> image added at \(position) for \(objectType) w \(width * unitSize) h \(height * unitSize)")

let imageView = RectangularImageView(objectType: objectType, center: position, width: width * unitSize, height: height * unitSize)
imageViews[id] = imageView
boardAreaView.addSubview(imageView.imageView)
}

func removeImage(id: ObjectIdentifier) {
// print("image removed for \(id)")
guard let removedImageView = imageViews.removeValue(forKey: id) else {
return
}
Expand All @@ -255,7 +245,6 @@ class LevelDesignerViewController: UIViewController {

func expandLevel(scale: CGFloat) {
boardBounds.max += scale * unitSize
// print("new board bounds \(boardBounds)")
}

// MARK: - other feature buttons
Expand Down

0 comments on commit 04e0404

Please sign in to comment.