Skip to content

Commit

Permalink
Merge pull request #14 from Vanessamae23/main
Browse files Browse the repository at this point in the history
Add AI Component
  • Loading branch information
Vanessamae23 authored Mar 19, 2024
2 parents 41bd7ba + b59903f commit 4bfea71
Show file tree
Hide file tree
Showing 38 changed files with 254 additions and 90 deletions.
38 changes: 35 additions & 3 deletions TowerForge/TowerForge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
3C9955C82BA5865C00D33FA5 /* ConcurrentEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C9955C72BA5865C00D33FA5 /* ConcurrentEvent.swift */; };
3C9955CA2BA5888F00D33FA5 /* SpawnEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C9955C92BA5888F00D33FA5 /* SpawnEvent.swift */; };
3C9955CC2BA5889800D33FA5 /* MoveEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C9955CB2BA5889800D33FA5 /* MoveEvent.swift */; };
5200624E2BA8D597000DBA30 /* AiComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5200624D2BA8D597000DBA30 /* AiComponent.swift */; };
520062522BA8DA09000DBA30 /* UnitGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520062512BA8DA09000DBA30 /* UnitGenerator.swift */; };
520062562BA8E026000DBA30 /* HasCost.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520062552BA8E026000DBA30 /* HasCost.swift */; };
520062582BA8ED73000DBA30 /* HomeComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520062572BA8ED73000DBA30 /* HomeComponent.swift */; };
52578B822BA61AAF00B4D76C /* PositionComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52578B812BA61AAF00B4D76C /* PositionComponent.swift */; };
52578B872BA6209700B4D76C /* DamageComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52578B862BA6209700B4D76C /* DamageComponent.swift */; };
52578B8C2BA627B200B4D76C /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52578B8B2BA627B200B4D76C /* Team.swift */; };
Expand Down Expand Up @@ -91,6 +95,10 @@
3C9955C72BA5865C00D33FA5 /* ConcurrentEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConcurrentEvent.swift; sourceTree = "<group>"; };
3C9955C92BA5888F00D33FA5 /* SpawnEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpawnEvent.swift; sourceTree = "<group>"; };
3C9955CB2BA5889800D33FA5 /* MoveEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveEvent.swift; sourceTree = "<group>"; };
5200624D2BA8D597000DBA30 /* AiComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AiComponent.swift; sourceTree = "<group>"; };
520062512BA8DA09000DBA30 /* UnitGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnitGenerator.swift; sourceTree = "<group>"; };
520062552BA8E026000DBA30 /* HasCost.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HasCost.swift; sourceTree = "<group>"; };
520062572BA8ED73000DBA30 /* HomeComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeComponent.swift; sourceTree = "<group>"; };
52578B812BA61AAF00B4D76C /* PositionComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionComponent.swift; sourceTree = "<group>"; };
52578B862BA6209700B4D76C /* DamageComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamageComponent.swift; sourceTree = "<group>"; };
52578B8B2BA627B200B4D76C /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -197,6 +205,24 @@
path = "Implemented Events";
sourceTree = "<group>";
};
5200624C2BA8D574000DBA30 /* GameComponents */ = {
isa = PBXGroup;
children = (
52578B862BA6209700B4D76C /* DamageComponent.swift */,
52DF5FFE2BA3656500135367 /* ShootingComponent.swift */,
52DF5FFA2BA3601400135367 /* HealthComponent.swift */,
);
path = GameComponents;
sourceTree = "<group>";
};
5200624F2BA8D9E4000DBA30 /* Generators */ = {
isa = PBXGroup;
children = (
520062512BA8DA09000DBA30 /* UnitGenerator.swift */,
);
path = Generators;
sourceTree = "<group>";
};
52578B882BA620A900B4D76C /* BaseComponents */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -266,6 +292,7 @@
52DF5FDB2BA32CEF00135367 /* LevelManager */ = {
isa = PBXGroup;
children = (
5200624F2BA8D9E4000DBA30 /* Generators */,
3C9955C32BA585CD00D33FA5 /* Systems */,
3C9955B82BA5620A00D33FA5 /* Events */,
3C99559F2BA47D3E00D33FA5 /* Entities */,
Expand Down Expand Up @@ -300,18 +327,19 @@
isa = PBXGroup;
children = (
52DF5FE82BA33F9700135367 /* Animatable.swift */,
520062552BA8E026000DBA30 /* HasCost.swift */,
);
path = Protocols;
sourceTree = "<group>";
};
52DF5FF02BA3519D00135367 /* Components */ = {
isa = PBXGroup;
children = (
5200624C2BA8D574000DBA30 /* GameComponents */,
52578B882BA620A900B4D76C /* BaseComponents */,
52DF5FFA2BA3601400135367 /* HealthComponent.swift */,
52DF5FFE2BA3656500135367 /* ShootingComponent.swift */,
527E3A232BA613EF00FE1628 /* PlayerComponent.swift */,
52578B862BA6209700B4D76C /* DamageComponent.swift */,
5200624D2BA8D597000DBA30 /* AiComponent.swift */,
520062572BA8ED73000DBA30 /* HomeComponent.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -475,6 +503,7 @@
52578B8C2BA627B200B4D76C /* Team.swift in Sources */,
52DF5FFF2BA3656500135367 /* ShootingComponent.swift in Sources */,
3C9955CC2BA5889800D33FA5 /* MoveEvent.swift in Sources */,
5200624E2BA8D597000DBA30 /* AiComponent.swift in Sources */,
3C9955C22BA5838900D33FA5 /* EventOutput.swift in Sources */,
52DF5FAE2BA32B2300135367 /* GameScene.swift in Sources */,
52578B822BA61AAF00B4D76C /* PositionComponent.swift in Sources */,
Expand All @@ -485,6 +514,7 @@
52578B872BA6209700B4D76C /* DamageComponent.swift in Sources */,
52DF5FB02BA32B2300135367 /* GameViewController.swift in Sources */,
529F91882BA6D7A7009551D9 /* SoldierUnit.swift in Sources */,
520062522BA8DA09000DBA30 /* UnitGenerator.swift in Sources */,
3C9955C02BA57E5500D33FA5 /* EventTarget.swift in Sources */,
52DF5FF92BA35D2B00135367 /* MovableComponent.swift in Sources */,
52DF5FDE2BA32D7E00135367 /* EntityManager.swift in Sources */,
Expand All @@ -496,6 +526,7 @@
52DF5FE92BA33F9700135367 /* Animatable.swift in Sources */,
3C9955AF2BA48FD200D33FA5 /* MeleeUnit.swift in Sources */,
52DF5FE12BA3349600135367 /* TFTextures.swift in Sources */,
520062582BA8ED73000DBA30 /* HomeComponent.swift in Sources */,
52DF5FA82BA32B2300135367 /* AppDelegate.swift in Sources */,
3C769A742BA591BD00F454F9 /* SpawnSystem.swift in Sources */,
3C9955B12BA4ACA100D33FA5 /* Arrow.swift in Sources */,
Expand All @@ -505,6 +536,7 @@
52DF5FED2BA34D0300135367 /* TFComponent.swift in Sources */,
527E3A242BA613F000FE1628 /* PlayerComponent.swift in Sources */,
3C9955C52BA585DD00D33FA5 /* HealthSystem.swift in Sources */,
520062562BA8E026000DBA30 /* HasCost.swift in Sources */,
52DF5FFB2BA3601400135367 /* HealthComponent.swift in Sources */,
3C9955BC2BA563A800D33FA5 /* TFEvent.swift in Sources */,
);
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion TowerForge/TowerForge/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// AppDelegate.swift
// TowerForge
//
// Created by MacBook Pro on 14/03/24.
// Created by Vanessa Mae on 14/03/24.
//

import UIKit
Expand Down
Binary file modified TowerForge/TowerForge/GameScene.sks
Binary file not shown.
16 changes: 8 additions & 8 deletions TowerForge/TowerForge/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// GameScene.swift
// TowerForge
//
// Created by MacBook Pro on 14/03/24.
// Created by Vanessa Mae on 14/03/24.
//

import SpriteKit
Expand All @@ -14,19 +14,19 @@ class GameScene: SKScene {
private var entityManager: EntityManager?

override func didMove(to view: SKView) {
// let textureNames = ["melee-1", "melee-2"]
// let tfTextures = TFTextures(textureNames: textureNames, textureAtlasName: "Sprites", mainTextureName: "melee-1")
// let animatableNode = TFAnimatableNode(textures: tfTextures, height: 300, width: 300, animatableKey: "melee")
// addChild(animatableNode)
// animatableNode.playAnimation()
entityManager = EntityManager()
guard var entityManager = entityManager else {
return
}
let meleeUnit = MeleeUnit(position: CGPoint(x: 0, y: 100), entityManager: entityManager, attackRate: 1.0, velocity: CGVector(dx: 10.0, dy: 0.0))
let soldierUnit = SoldierUnit(position: CGPoint(x: 0, y: 50), entityManager: entityManager, attackRate: 1.0, velocity: CGVector(dx: 10.0, dy: 0.0))
let meleeUnit = MeleeUnit(position: CGPoint(x: 0, y: 100),
entityManager: entityManager, attackRate: 1.0, velocity: CGVector(dx: 10.0, dy: 0.0), team: Team(player: .ownPlayer))
let soldierUnit = SoldierUnit(position: CGPoint(x: 0, y: 50), entityManager: entityManager,
attackRate: 1.0,
velocity: CGVector(dx: 10.0, dy: 0.0), team: Team(player: .ownPlayer))
let arrowTower = ArrowTower(position: CGPoint(x:0, y: 100), entityManager: entityManager)
entityManager.add(meleeUnit)
entityManager.add(soldierUnit)
entityManager.add(arrowTower)
guard let sprite = meleeUnit.component(ofType: SpriteComponent.self),
let soldierSprite = soldierUnit.component(ofType: SpriteComponent.self),
let arrowTowerSprite = arrowTower.component(ofType: SpriteComponent.self) else {
Expand Down
2 changes: 1 addition & 1 deletion TowerForge/TowerForge/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// GameViewController.swift
// TowerForge
//
// Created by MacBook Pro on 14/03/24.
// Created by Vanessa Mae on 14/03/24.
//

import UIKit
Expand Down
40 changes: 40 additions & 0 deletions TowerForge/TowerForge/LevelManager/Components/AiComponent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// AiComponent.swift
// TowerForge
//
// Created by Vanessa Mae on 19/03/24.
//

import Foundation

enum UnitType {
case melee
case soldier
static let possibleUnits = [melee, soldier]
}

class AiComponent: TFComponent {
private var entityManager: EntityManager
private var chosenUnit: UnitType
init(entityManager: EntityManager) {
self.entityManager = entityManager
self.chosenUnit = UnitType.possibleUnits.randomElement() ?? .melee
super.init()
}

override func update(deltaTime: TimeInterval) {
guard let homeComponent = entity?.component(ofType: HomeComponent.self) else {
return
}
if chosenUnit == .melee && homeComponent.points >= MeleeUnit.cost {
// TODO: Remove hard code of CGPoints
UnitGenerator.spawnMelee(at: CGPoint(x: 0, y: 10), player: .oppositePlayer, entityManager: entityManager)
self.chosenUnit = UnitType.possibleUnits.randomElement() ?? .melee
}
if chosenUnit == .soldier && homeComponent.points >= SoldierUnit.cost {
// TODO: Remove hard code of CGPoints
UnitGenerator.spawnSoldier(at: CGPoint(x: 0, y: 10), player: .oppositePlayer, entityManager: entityManager)
self.chosenUnit = UnitType.possibleUnits.randomElement() ?? .melee
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class MovableComponent: TFComponent {
override func update(deltaTime: TimeInterval) {
guard let entity = entity,
let positionComponent = entity.component(ofType: PositionComponent.self),
let spriteComponent = entity.component(ofType: SpriteComponent.self) else {
let spriteComponent = entity.component(ofType: SpriteComponent.self),
let playerComponent = entity.component(ofType: PlayerComponent.self) else {
return
}

let finalX = positionComponent.position.x + velocity.dx * CGFloat(deltaTime)
let finalY = positionComponent.position.y + velocity.dy * CGFloat(deltaTime)
let directionVelocity = playerComponent.player.getDirectionVelocity()
let finalX = positionComponent.position.x + (velocity.dx * CGFloat(deltaTime) * directionVelocity.dx)
let finalY = positionComponent.position.y + (velocity.dy * CGFloat(deltaTime) * directionVelocity.dy)
positionComponent.changeTo(to: CGPoint(x: finalX, y: finalY))
spriteComponent.node.position = positionComponent.position
print(spriteComponent.node.position)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
// PositionComponent.swift
// TowerForge
//
// Created by MacBook Pro on 17/03/24.
// Created by Vanessa Mae on 17/03/24.
//

import Foundation
import CoreGraphics

class PositionComponent: TFComponent {
var position: CGPoint

init(position: CGPoint) {
self.position = position
super.init()
}

func changeTo(to position: CGPoint) {
guard let entity = entity, let spriteComponent = entity.component(ofType: SpriteComponent.self) else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SpriteComponent.swift
// TowerForge
//
// Created by MacBook Pro on 14/03/24.
// Created by Vanessa Mae on 14/03/24.
//

import Foundation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// CostComponent.swift
// TowerForge
//
// Created by MacBook Pro on 19/03/24.
//

import Foundation

class CostComponent: TFComponent {
public var cost: CGFloat
init(cost: CGFloat) {
self.cost = cost
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// MeleeComponent.swift
// TowerForge
//
// Created by MacBook Pro on 17/03/24.
// Created by Vanessa Mae on 17/03/24.
//

import Foundation
Expand All @@ -15,7 +15,6 @@ class DamageComponent: TFComponent {
private let entityManager: EntityManager
private let temporary: Bool
public let attackPower: CGFloat

init(attackRate: TimeInterval, attackPower: CGFloat, temporary: Bool, entityManager: EntityManager) {
self.attackRate = attackRate
self.attackPower = attackPower
Expand Down Expand Up @@ -46,7 +45,7 @@ class DamageComponent: TFComponent {
let oppositeHealthComponent = entity.component(ofType: HealthComponent.self) else {
return
}

// Check collision with opposite team sprite component
if(oppositeSpriteComponent.node.calculateAccumulatedFrame().intersects(spriteComponent.node.calculateAccumulatedFrame())) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// HealthComponent.swift
// TowerForge
//
// Created by MacBook Pro on 15/03/24.
// Created by Vanessa Mae on 15/03/24.
//

import Foundation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ShootingComponent.swift
// TowerForge
//
// Created by MacBook Pro on 15/03/24.
// Created by Vanessa Mae on 15/03/24.
//

import Foundation
Expand Down Expand Up @@ -61,14 +61,10 @@ class ShootingComponent: TFComponent {
guard let arrowSpriteComponent = arrow.component(ofType: SpriteComponent.self) else {
return
}

// Check if can attack
if(CACurrentMediaTime() - lastShotTime > fireRate) {
if CACurrentMediaTime() - lastShotTime > fireRate {
lastShotTime = CACurrentMediaTime()
}

}

}
} }
}
}
40 changes: 40 additions & 0 deletions TowerForge/TowerForge/LevelManager/Components/HomeComponent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// HomeComponent.swift
// TowerForge
//
// Created by Vanessa Mae on 19/03/24.
//

import Foundation
import SpriteKit

class HomeComponent: TFComponent {
public var lifeLeft: Int
public var points = 0
private var lastPointIncrease = TimeInterval(0)
private var pointInterval: TimeInterval
private var pointsPerInterval: Int = 10
init(initialLifeCount: Int, pointInterval: TimeInterval) {
self.lifeLeft = initialLifeCount
self.pointInterval = pointInterval
super.init()
}
func decreaseLife() -> Int {
self.lifeLeft -= 1
return self.lifeLeft
}
func increaseLife() -> Int {
self.lifeLeft += 1
return self.lifeLeft
}
override func update(deltaTime: TimeInterval) {
super.update(deltaTime: deltaTime)
// Points update
if(CACurrentMediaTime() - lastPointIncrease > pointInterval) {
lastPointIncrease = CACurrentMediaTime()
points += self.pointsPerInterval
}
}
}


Loading

0 comments on commit 4bfea71

Please sign in to comment.