Skip to content

Commit

Permalink
Merge pull request #67 from cs3217-2324/view-variable-screen-size
Browse files Browse the repository at this point in the history
Default levels
  • Loading branch information
JCSnap authored Apr 21, 2024
2 parents d6d4a24 + 1a8f65c commit 5ff8f77
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 36 deletions.
12 changes: 10 additions & 2 deletions TheAlienThatEatsTheCarrot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@
3FCD005D2BB017F20016389F /* LoadLevelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FCD005C2BB017F20016389F /* LoadLevelViewController.swift */; };
3FCD005F2BB0184D0016389F /* SaveLevelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FCD005E2BB0184D0016389F /* SaveLevelViewController.swift */; };
3FE4570D2BADAB8E00D8D5A9 /* StartScreenViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FE4570C2BADAB8E00D8D5A9 /* StartScreenViewController.swift */; };
3FFD2C8B2BD32B9700D8F4AC /* AchievementBar.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3FFD2C8A2BD32B9700D8F4AC /* AchievementBar.xib */; };
3FFD2C8D2BD33B0B00D8F4AC /* AchievementView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FFD2C8C2BD33B0B00D8F4AC /* AchievementView.swift */; };
9F874E4D2BB3C130002EFA86 /* LevelDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F874E4C2BB3C130002EFA86 /* LevelDataManager.swift */; };
9F8C589E2BAE74F1002E75FB /* TheAlienThatEastsTheCarrot.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 9F8C589C2BAE74F1002E75FB /* TheAlienThatEastsTheCarrot.xcdatamodeld */; };
9F8C58A12BAE78AC002E75FB /* ToDataAble.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F8C58A02BAE78AC002E75FB /* ToDataAble.swift */; };
Expand Down Expand Up @@ -358,6 +360,8 @@
3FCD005C2BB017F20016389F /* LoadLevelViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadLevelViewController.swift; sourceTree = "<group>"; };
3FCD005E2BB0184D0016389F /* SaveLevelViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SaveLevelViewController.swift; sourceTree = "<group>"; };
3FE4570C2BADAB8E00D8D5A9 /* StartScreenViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartScreenViewController.swift; sourceTree = "<group>"; };
3FFD2C8A2BD32B9700D8F4AC /* AchievementBar.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AchievementBar.xib; sourceTree = "<group>"; };
3FFD2C8C2BD33B0B00D8F4AC /* AchievementView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AchievementView.swift; sourceTree = "<group>"; };
9F874E4C2BB3C130002EFA86 /* LevelDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LevelDataManager.swift; sourceTree = "<group>"; };
9F8C57EE2BAAD71B002E75FB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
9F8C57F02BAAD71B002E75FB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -810,6 +814,7 @@
9F8C58372BAAD71B002E75FB /* View */,
9F8C58392BAAD71B002E75FB /* AppDelegate.swift */,
3F68076E2BAE779E00EB7682 /* LevelView.xib */,
3FFD2C8A2BD32B9700D8F4AC /* AchievementBar.xib */,
9F8C589C2BAE74F1002E75FB /* TheAlienThatEastsTheCarrot.xcdatamodeld */,
9F8C583A2BAAD71B002E75FB /* Info.plist */,
9F8C583B2BAAD71B002E75FB /* SceneDelegate.swift */,
Expand Down Expand Up @@ -1027,6 +1032,7 @@
9F8C58372BAAD71B002E75FB /* View */ = {
isa = PBXGroup;
children = (
3FFD2C8C2BD33B0B00D8F4AC /* AchievementView.swift */,
3FAE0EE72BB427D6002521D9 /* ImageViews */,
3FE4570C2BADAB8E00D8D5A9 /* StartScreenViewController.swift */,
3F6874AB2BAFFD37009C526D /* LevelSelect */,
Expand Down Expand Up @@ -1164,6 +1170,7 @@
buildActionMask = 2147483647;
files = (
3F68076F2BAE779E00EB7682 /* LevelView.xib in Resources */,
3FFD2C8B2BD32B9700D8F4AC /* AchievementBar.xib in Resources */,
3F6807692BADAF9D00EB7682 /* Assets.xcassets in Resources */,
3F96D4F02BCD026C002F4986 /* defaultLevels.json in Resources */,
0A76A4A02BAC7E270067BD6E /* Main.storyboard in Resources */,
Expand Down Expand Up @@ -1275,6 +1282,7 @@
0AC861A62BC716C90060EF90 /* AttackIfOutOfBoundsAttackStyle.swift in Sources */,
0AC860D62BB6B0290060EF90 /* TimerComponent.swift in Sources */,
9F8C58B92BAEACB4002E75FB /* PowerupType+extensions.swift in Sources */,
3FFD2C8D2BD33B0B00D8F4AC /* AchievementView.swift in Sources */,
9F9E7B122BC921F300EF22D3 /* CarrotCollectedEvent.swift in Sources */,
9F8C58AD2BAEA2E5002E75FB /* BlockType.swift in Sources */,
0A76A4722BAC78490067BD6E /* Component.swift in Sources */,
Expand Down Expand Up @@ -1558,7 +1566,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = AU29Q33B32;
DEVELOPMENT_TEAM = 47X7TKJ478;
EXCLUDED_ARCHS = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = TheAlienThatEatsTheCarrot/Info.plist;
Expand Down Expand Up @@ -1590,7 +1598,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = AU29Q33B32;
DEVELOPMENT_TEAM = 47X7TKJ478;
EXCLUDED_ARCHS = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = TheAlienThatEatsTheCarrot/Info.plist;
Expand Down
48 changes: 48 additions & 0 deletions TheAlienThatEatsTheCarrot/AchievementBar.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="ipad10_9rounded" orientation="landscape" layout="fullscreen" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="393" height="80"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="projectile-pellet" translatesAutoresizingMaskIntoConstraints="NO" id="wDK-x7-kWo">
<rect key="frame" x="23" y="8" width="64" height="64"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Achievement unlocked: " textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="peA-R2-m5b">
<rect key="frame" x="105" y="15" width="182" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="adgwegawbfdher" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="euj-kC-Iab">
<rect key="frame" x="105" y="44" width="119" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<color key="backgroundColor" systemColor="systemGray4Color"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="-71.440677966101703" y="-331.46341463414632"/>
</view>
</objects>
<resources>
<image name="projectile-pellet" width="50" height="50"/>
<systemColor name="systemGray4Color">
<color red="0.81960784313725488" green="0.81960784313725488" blue="0.83921568627450982" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
class LevelDesigner {
var level: Level
var view: LevelDesignerViewController
var storageManager = LevelDataManager()
var storageManager = LevelDataManager.sharedManager

init(area: CGRect, view: LevelDesignerViewController) {
self.level = Level(area: area)
Expand Down
8 changes: 4 additions & 4 deletions TheAlienThatEatsTheCarrot/LevelView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
<constraint firstAttribute="width" constant="378" id="vd3-vC-29m"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" " textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="x7c-vt-KVC">
<rect key="frame" x="187" y="246" width="4.5" height="20"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="BEST TIME: 2 : 30" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="x7c-vt-KVC">
<rect key="frame" x="123.5" y="246" width="131" height="20"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.25882352939999997" green="0.090196078430000007" blue="0.039215686270000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" " textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Hda-lv-eYg">
<rect key="frame" x="187" y="224" width="4.5" height="20"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" BEST SCORE: 13423" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Hda-lv-eYg">
<rect key="frame" x="111.5" y="224" width="155" height="20"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.25882352941176467" green="0.090196078431372548" blue="0.039215686274509803" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down
75 changes: 70 additions & 5 deletions TheAlienThatEatsTheCarrot/Persistence/LevelDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
import CoreData

struct LevelDataManager {
let context: NSManagedObjectContext

init(mainContext: NSManagedObjectContext = CoreDataManager.sharedManager.context) {
self.context = mainContext
static let sharedManager = LevelDataManager()
let context = CoreDataManager.sharedManager.context
private var preloadedLevels: [Level] = []

private init() {
// deleteAllData() // use when want to clear all data on ipad simulator
fetchPreloadedLevels()
}

/// Fetches the saved `LevelData` matching the provided `levelName`. Throws errors
/// when the level name does not exist or duplicate level names are detected.
/// - Parameters:
/// - levelName: the name of the level to fetch.
/// - Returns : the fetched `LevelData`.
func fetchLevelData(levelName: String) throws -> LevelData {
private func fetchLevelData(levelName: String) throws -> LevelData {
let levelDatas = try fetchAllLevelDataMatching(levelName: levelName)
if levelDatas.isEmpty {
throw TheAlienThatEatsTheCarrotError.invalidPersistenceDataError
Expand Down Expand Up @@ -53,9 +57,10 @@ struct LevelDataManager {
}
_ = level.toData(context: context)
try context.save()
print("saved level \(level.name)")
}

func fetchAllLevelData() throws -> [LevelData] {
private func fetchAllLevelData() throws -> [LevelData] {
let request = LevelData.fetchRequest()
return try context.fetch(request)
}
Expand Down Expand Up @@ -86,6 +91,25 @@ struct LevelDataManager {
}
}

func fetchLevels() -> [Level] {
var levels: [Level] = []
do {
let levelDatas = try fetchAllLevelData()
if levelDatas.isEmpty {
return levels
}
for levelData in levelDatas {
let level = try Level(data: levelData)
levels.append(level)
print("fetched level \(level.name)")
}
} catch {
print("Error fetching level data: \(error)")
return levels
}
return levels
}

func fetchEmptyLevel() -> Level? {
guard let fileURL = Bundle.main.url(forResource: "emptyLevel", withExtension: "json") else {
print("Error: emptyLevel.json file not found")
Expand All @@ -107,4 +131,45 @@ struct LevelDataManager {
}
return nil
}

mutating func fetchPreloadedLevels() {
guard let fileURL = Bundle.main.url(forResource: "defaultLevels", withExtension: "json") else {
print("Error: emptyLevel.json file not found")
return
}
do {
let jsonData = try Data(contentsOf: fileURL)
let jsonArray = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [[String: Any]] ?? []
for jsonDict in jsonArray {
let jsonString = String(data: try JSONSerialization.data(withJSONObject: jsonDict, options: []), encoding: .utf8)
if let level = jsonString.flatMap({ Level.fromJSONString(jsonString: $0) }) {
if preloadedLevels.contains(where: { $0.name == level.name }) {
// "Level with name '\(levelName)' already exists. Skipping preloaded level."
continue
}
preloadedLevels.append(level)
print("fetched preloaded level \(level.name)")
try saveLevelData(level: level, overwrite: true)
} else {
print("Error: Failed to create Level instance from JSON string")
}
}
} catch {
print("Error decoding defaultLevels.json: \(error)")
}
return
}

private mutating func deleteAllData() {
print("deleting all data")
do {
let levelDatas = try fetchAllLevelData()
for levelData in levelDatas {
context.delete(levelData)
}
} catch {
print("Error fetching level data: \(error)")
return
}
}
}
Loading

0 comments on commit 5ff8f77

Please sign in to comment.