Skip to content

Commit

Permalink
Merge pull request #9 from cs3217-2324/feat/rendering
Browse files Browse the repository at this point in the history
Integration between GameEngine and Rendering
  • Loading branch information
HoJunHao2000 committed Mar 17, 2024
2 parents 86d9cf6 + aab969f commit 2c83cd6
Show file tree
Hide file tree
Showing 23 changed files with 562 additions and 91 deletions.
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)
}

0 comments on commit 2c83cd6

Please sign in to comment.