Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration between GameEngine and Rendering #9

Merged
merged 24 commits into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
65d1c00
chore: Add SDPhysicsEngine package
Junyi00 Mar 14, 2024
c87c9e3
Merge branch 'feat/rendering' of github.com:cs3217-2324/group-project…
Junyi00 Mar 14, 2024
72e045c
chore: Transition to use SDGameEngine
Junyi00 Mar 14, 2024
5db77fd
chore: Refactor SDPhysicsEngine
Junyi00 Mar 14, 2024
2c0fb75
chore: Add Scene delegate
Junyi00 Mar 14, 2024
8a0a8c3
fix: Fix syntax issues
Junyi00 Mar 14, 2024
90e2b4d
chore: Remove dependency to SDPhysicsEngine from renderer
Junyi00 Mar 14, 2024
380e0b6
Merge branch 'main' into feat/rendering
Junyi00 Mar 15, 2024
0acbcd2
wip: Synchronise from entities to game scene
Junyi00 Mar 15, 2024
dc06b19
Merge branch 'feat/rendering' of github.com:cs3217-2324/group-project…
Junyi00 Mar 15, 2024
d9a1dac
fix: Fix broken pbxproj file
Junyi00 Mar 15, 2024
213d101
fix: Fix syntax issues
Junyi00 Mar 15, 2024
fbf5c06
fix: Fix syntax issues
Junyi00 Mar 15, 2024
f1ca1a1
wip: setup game entieies
Junyi00 Mar 16, 2024
98971b3
fix: Fix syntax
Junyi00 Mar 16, 2024
3dfba37
refactor: Refactor to create a GameBridge component
Junyi00 Mar 16, 2024
e0fff74
chore: Resolve swiftlint issues
Junyi00 Mar 16, 2024
87d2956
chore: Use an interface between GameBridge and GameEngine
Junyi00 Mar 16, 2024
1047089
fix: Fix syntax issues
Junyi00 Mar 16, 2024
d917d95
chore: Resolve swiftlint issues
Junyi00 Mar 16, 2024
6e5e697
chore: Remove commented code
Junyi00 Mar 16, 2024
e894350
chore: Use CGFloat for PositionComponent's rotation
Junyi00 Mar 16, 2024
f57f202
chore: Make private methods private
Junyi00 Mar 16, 2024
aab969f
fix: Fix syntax issues
Junyi00 Mar 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions SDPhysicsEngine/.gitignore
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
23 changes: 23 additions & 0 deletions SDPhysicsEngine/Package.swift
@@ -0,0 +1,23 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SDPhysicsEngine",
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "SDPhysicsEngine",
targets: ["SDPhysicsEngine"])
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "SDPhysicsEngine"),
.testTarget(
name: "SDPhysicsEngineTests",
dependencies: ["SDPhysicsEngine"])
]
)
26 changes: 26 additions & 0 deletions SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift
@@ -0,0 +1,26 @@
import SpriteKit

public class GameScene: SKScene, SDScene {

public var sceneDelegate: SDSceneDelegate?

private var lastUpdateTime: TimeInterval?

override public func update(_ currentTime: TimeInterval) {
super.update(currentTime)

guard let lastUpdateTime = lastUpdateTime else {
lastUpdateTime = currentTime
return
}

let deltaTime = currentTime - lastUpdateTime
self.lastUpdateTime = currentTime

sceneDelegate?.update(self, deltaTime: deltaTime)
}

public func addObject(_ object: SDObject) {
addChild(object.node)
}
}
37 changes: 37 additions & 0 deletions SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift
@@ -0,0 +1,37 @@
import SpriteKit

public class SDObject {
let node: SKNode

var innerRotation: CGFloat = 0

public init() {
node = SKNode()
}

init(node: SKNode) {
self.node = node
}

public var position: CGPoint {
get { node.position }
set { node.position = newValue }
}

public var zPosition: CGFloat {
get { node.zPosition }
set { node.zPosition = newValue }
}

public var rotation: CGFloat {
get { innerRotation }
set {
innerRotation = newValue
node.run(SKAction.rotate(toAngle: newValue, duration: 1))
}
}

public var physicsBody: SDPhysicsBody? {
willSet { node.physicsBody = newValue?.body }
}
}
39 changes: 39 additions & 0 deletions SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDPhysicsBody.swift
@@ -0,0 +1,39 @@
import SpriteKit

public class SDPhysicsBody {
let body: SKPhysicsBody

public init(rectangleOf size: CGSize) {
body = SKPhysicsBody(rectangleOf: size)
}

public init(circleOf radius: CGFloat) {
body = SKPhysicsBody(circleOfRadius: radius)
}

public var mass: CGFloat {
get { body.mass }
set { body.mass = newValue }
}

public var velocity: CGVector {
get { body.velocity }
set { body.velocity = newValue }
}

public var force: CGVector {
// get { body.force }
// set { body.force = newValue }
CGVector(dx: 0, dy: 0)
}

public var affectedByGravity: Bool {
get { body.affectedByGravity }
set { body.affectedByGravity = newValue }
}

public var isDynamic: Bool {
get { body.isDynamic }
set { body.isDynamic = newValue }
}
}
@@ -0,0 +1,35 @@
import SpriteKit

public class SDSpriteObject: SDObject {
let spriteNode: SKSpriteNode

public var activeTexture: String?

public init(imageNamed: String) {
self.spriteNode = SKSpriteNode(imageNamed: imageNamed)
super.init(node: spriteNode)
}

public var size: CGSize {
get { spriteNode.size }
set { spriteNode.size = newValue }
}

public func runTexture(named: String) {
let texture = loadTexture(named: named)
spriteNode.run(SKAction.repeatForever(
SKAction.animate(with: texture, timePerFrame: TimeInterval(0.1), resize: false, restore: true)
))

activeTexture = named
}

private func loadTexture(named: String) -> [SKTexture] {
let textureAtlas = SKTextureAtlas(named: named)
var frames = [SKTexture]()
for idx in 0..<textureAtlas.textureNames.count {
frames.append(textureAtlas.textureNamed(textureAtlas.textureNames[idx]))
}
return frames
}
}
8 changes: 8 additions & 0 deletions SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift
@@ -0,0 +1,8 @@
import CoreGraphics

public protocol SDScene {

var size: CGSize { get }

func addObject(_ object: SDObject)
}
4 changes: 4 additions & 0 deletions SDPhysicsEngine/Sources/SDPhysicsEngine/SDSceneDelegate.swift
@@ -0,0 +1,4 @@
public protocol SDSceneDelegate: AnyObject {

func update(_ scene: SDScene, deltaTime: Double)
}