Skip to content

Commit

Permalink
Merge pull request cs3217-2324#44 from zheng-ze/main
Browse files Browse the repository at this point in the history
Update ContactSystem, HealthSystem and ShootingSystem
  • Loading branch information
Vanessamae23 authored Mar 26, 2024
2 parents 2c3e7e3 + 9bcb8fb commit 06025f1
Show file tree
Hide file tree
Showing 42 changed files with 223 additions and 347 deletions.
4 changes: 4 additions & 0 deletions TowerForge/TowerForge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3C0B608D2BB2B84000FFECB4 /* ContactComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C0B608C2BB2B84000FFECB4 /* ContactComponent.swift */; };
3C769A722BA58DE700F454F9 /* MovementSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C769A712BA58DE700F454F9 /* MovementSystem.swift */; };
3C769A742BA591BD00F454F9 /* SpawnSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C769A732BA591BD00F454F9 /* SpawnSystem.swift */; };
3C9955A12BA47DA500D33FA5 /* BaseTower.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C9955A02BA47DA500D33FA5 /* BaseTower.swift */; };
Expand Down Expand Up @@ -116,6 +117,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3C0B608C2BB2B84000FFECB4 /* ContactComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactComponent.swift; sourceTree = "<group>"; };
3C769A712BA58DE700F454F9 /* MovementSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MovementSystem.swift; sourceTree = "<group>"; };
3C769A732BA591BD00F454F9 /* SpawnSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpawnSystem.swift; sourceTree = "<group>"; };
3C9955A02BA47DA500D33FA5 /* BaseTower.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseTower.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -307,6 +309,7 @@
isa = PBXGroup;
children = (
52578B862BA6209700B4D76C /* DamageComponent.swift */,
3C0B608C2BB2B84000FFECB4 /* ContactComponent.swift */,
52DF5FFE2BA3656500135367 /* ShootingComponent.swift */,
52DF5FFA2BA3601400135367 /* HealthComponent.swift */,
);
Expand Down Expand Up @@ -919,6 +922,7 @@
527E3A242BA613F000FE1628 /* PlayerComponent.swift in Sources */,
3C9955C52BA585DD00D33FA5 /* HealthSystem.swift in Sources */,
520062562BA8E026000DBA30 /* PlayerSpawnable.swift in Sources */,
3C0B608D2BB2B84000FFECB4 /* ContactComponent.swift in Sources */,
3CE951652BAE0A04008B2785 /* HomeSystem.swift in Sources */,
52DF5FFB2BA3601400135367 /* HealthComponent.swift in Sources */,
3C9955BC2BA563A800D33FA5 /* TFEvent.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Vanessa Mae on 23/03/24.
//

import Foundation
import SpriteKit

class LabelComponent: TFComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,22 @@ import CoreGraphics

class MovableComponent: TFComponent {
var velocity: CGVector
var position: CGPoint
var isColliding = false
var shouldMove = true

init(position: CGPoint, velocity: CGVector = .zero) {
init(velocity: CGVector = .zero) {
self.velocity = velocity
self.position = position
super.init()
}

func updatePosition(to position: CGPoint) {
self.position = position
}

func updatePosition(with vector: CGVector) {
self.position.x += vector.dx
self.position.y += vector.dy
}
func updatePosition(with vector: CGVector) {
guard let positionComponent = entity?.component(ofType: PositionComponent.self) else {
return
}
positionComponent.move(by: vector)
}

override func update(deltaTime: TimeInterval) {
guard !isColliding,
let entity = entity,
guard shouldMove, let entity = entity,
let positionComponent = entity.component(ofType: PositionComponent.self),
let playerComponent = entity.component(ofType: PlayerComponent.self) else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import CoreGraphics

class PositionComponent: TFComponent {
var position: CGPoint
private(set) var position: CGPoint
var anchorPoint: CGPoint

init(position: CGPoint, anchorPoint: CGPoint) {
Expand All @@ -25,4 +25,9 @@ class PositionComponent: TFComponent {
func changeTo(to position: CGPoint) {
self.position = position
}

func move(by displacement: CGVector) {
position.x += displacement.dx
position.y += displacement.dy
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class SpriteComponent: TFComponent {
var animatableKey: String
var node: TFAnimatableNode

init(textureNames: [String], height: CGFloat, width: CGFloat, position: CGPoint, animatableKey: String) {
init(textureNames: [String], size: CGSize, animatableKey: String) {
textures = TFTextures(textureNames: textureNames, textureAtlasName: "Sprites")
self.height = height
self.width = width
self.height = size.height
self.width = size.width
self.node = TFAnimatableNode(textures: textures, height: height, width: width, animatableKey: animatableKey)
self.animatableKey = animatableKey
super.init()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ContactComponent.swift
// TowerForge
//
// Created by Zheng Ze on 26/3/24.
//

import Foundation

class ContactComponent: TFComponent {
let hitboxSize: CGSize

init(hitboxSize: CGSize) {
self.hitboxSize = hitboxSize
}

func hitbox(position: CGPoint) -> CGRect {
CGRect(origin: position, size: hitboxSize)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// Created by Vanessa Mae on 17/03/24.
//

import Foundation
import SpriteKit
import QuartzCore

class DamageComponent: TFComponent {
private let attackRate: TimeInterval
Expand All @@ -26,7 +25,7 @@ class DamageComponent: TFComponent {
}

func damage(_ healthComponent: HealthComponent) -> TFEvent? {
guard canDamage, let enemyId = healthComponent.entity?.id, let id = entity?.id else {
guard canDamage, let enemyId = healthComponent.entity?.id, let entityId = entity?.id else {
return nil
}

Expand All @@ -40,7 +39,7 @@ class DamageComponent: TFComponent {
let event = DamageEvent(on: enemyId, at: lastAttackTime, with: attackPower)

if temporary {
return event.concurrentlyWith(RemoveEvent(on: id, at: lastAttackTime))
return event.concurrentlyWith(RemoveEvent(on: entityId, at: lastAttackTime))
}
return event
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Vanessa Mae on 15/03/24.
//

import Foundation
import SpriteKit

class HealthComponent: TFComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
// Created by Vanessa Mae on 15/03/24.
//

import Foundation
import SpriteKit
import QuartzCore

class ShootingComponent: TFComponent {
var fireRate: TimeInterval // Delay between shots
var range: CGFloat
private var lastShotTime = TimeInterval(0)
private(set) var lastShotTime = TimeInterval(0)
let attackPower: CGFloat

init(fireRate: TimeInterval, range: CGFloat, attackPower: CGFloat) {
Expand All @@ -25,30 +24,7 @@ class ShootingComponent: TFComponent {
CACurrentMediaTime() - lastShotTime >= fireRate
}

func shoot(_ healthComponent: HealthComponent) -> SpawnEvent? {
guard canShoot, let entityA = self.entity, let entityB = healthComponent.entity else {
return nil
}

guard let playerA = entityA.component(ofType: PlayerComponent.self)?.player,
let playerB = entityB.component(ofType: PlayerComponent.self)?.player,
playerA != playerB else {
return nil
}

guard let positionA = entityA.component(ofType: PositionComponent.self)?.position,
let positionB = entityB.component(ofType: PositionComponent.self)?.position else {
return nil
}

let direction = playerA == .ownPlayer ? -1.0 : 1.0
let distance = (positionA.x - positionB.x) * direction

guard distance > 0, distance <= range, abs(positionA.y - positionB.y) <= 50 else {
return nil
}

func shoot() {
lastShotTime = CACurrentMediaTime()
return SpawnEvent(ofType: Bullet.self, timestamp: lastShotTime, position: positionA, player: playerA)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// Created by Vanessa Mae on 19/03/24.
//

import Foundation
import SpriteKit
import QuartzCore

class HomeComponent: TFComponent {
var lifeLeft: Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import SpriteKit

class PlayerComponent: TFComponent {
var player: Player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,13 @@ class BaseProjectile: TFEntity {
init(textureNames: [String], size: CGSize, key: String, position: CGPoint,
player: Player, velocity: CGVector = .zero) {
super.init()
// Core Components
self.addComponent(SpriteComponent(textureNames: textureNames, size: size, animatableKey: key))
self.addComponent(PositionComponent(position: position))
self.addComponent(MovableComponent(velocity: velocity))

createSpriteComponent(textureNames: textureNames, size: size, key: key, position: position)
createMovableComponent(position: position, velocity: velocity)
createPositionComponent(position: position)
createPlayerComponent(player: player)
}

private func createSpriteComponent(textureNames: [String], size: CGSize, key: String, position: CGPoint) {
let spriteComponent = SpriteComponent(textureNames: textureNames,
height: size.height,
width: size.width,
position: position,
animatableKey: key)
self.addComponent(spriteComponent)
}

private func createPositionComponent(position: CGPoint) {
let positionComponent = PositionComponent(position: position)
self.addComponent(positionComponent)
}

private func createMovableComponent(position: CGPoint, velocity: CGVector) {
let movableComponent = MovableComponent(position: position, velocity: velocity)
self.addComponent(movableComponent)
}

private func createPlayerComponent(player: Player) {
let playerComponent = PlayerComponent(player: player)
self.addComponent(playerComponent)
// Game Components
self.addComponent(PlayerComponent(player: player))
self.addComponent(ContactComponent(hitboxSize: size))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ class BaseTower: TFEntity {
maxHealth: CGFloat,
player: Player) {
super.init()
// Core Components
self.addComponent(SpriteComponent(textureNames: textureNames, size: size, animatableKey: key))
self.addComponent(PositionComponent(position: position))

createSpriteComponent(textureNames: textureNames, size: size, key: key, position: position)
createHealthComponent(maxHealth: maxHealth)
createPositionComponent(position: position)
createPlayerComponent(player: player)
// Game Components
self.addComponent(HealthComponent(maxHealth: maxHealth))
self.addComponent(PlayerComponent(player: player))
self.addComponent(ContactComponent(hitboxSize: size))
}

override func collide(with other: any Collidable) -> TFEvent? {
Expand Down Expand Up @@ -49,30 +52,7 @@ class BaseTower: TFEntity {
return nil
}

movableComponent.isColliding = true
movableComponent.shouldMove = true
return nil
}

private func createHealthComponent(maxHealth: CGFloat) {
let healthComponent = HealthComponent(maxHealth: maxHealth)
self.addComponent(healthComponent)
}
private func createPositionComponent(position: CGPoint) {
let positionComponent = PositionComponent(position: position)
self.addComponent(positionComponent)
}

private func createSpriteComponent(textureNames: [String], size: CGSize, key: String, position: CGPoint) {
let spriteComponent = SpriteComponent(textureNames: textureNames,
height: size.height,
width: size.width,
position: position,
animatableKey: key)
self.addComponent(spriteComponent)
}

private func createPlayerComponent(player: Player) {
let playerComponent = PlayerComponent(player: player)
self.addComponent(playerComponent)
}
}
Loading

0 comments on commit 06025f1

Please sign in to comment.