Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from cs3217-2324/feat/rendering
Integration between GameEngine and Rendering
- Loading branch information
Showing
23 changed files
with
562 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
37
SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
39
SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDPhysicsBody.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDSpriteObject.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import CoreGraphics | ||
|
||
public protocol SDScene { | ||
|
||
var size: CGSize { get } | ||
|
||
func addObject(_ object: SDObject) | ||
} |
4 changes: 4 additions & 0 deletions
4
SDPhysicsEngine/Sources/SDPhysicsEngine/SDSceneDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public protocol SDSceneDelegate: AnyObject { | ||
|
||
func update(_ scene: SDScene, deltaTime: Double) | ||
} |
Oops, something went wrong.