1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
const GamePhase = {
PLACEMENT: 'placement',
COMBAT: 'combat'
};
const TowerTypes = {
BASIC: {
name: 'Basic Tower',
cost: 20,
range: 3,
damage: 1,
attackSpeed: 1,
color: '#3498db'
},
SNIPER: {
name: 'Sniper Tower',
cost: 40,
range: 6,
damage: 2,
attackSpeed: 0.5,
color: '#8e44ad'
},
RAPID: {
name: 'Rapid Tower',
cost: 35,
range: 2,
damage: 0.5,
attackSpeed: 2,
color: '#16a085'
},
GOOP: {
name: 'Goop Tower',
cost: 30,
range: 3,
damage: 0,
attackSpeed: 1,
color: '#27ae60',
special: 'slow',
slowAmount: 0.5 // Reduces enemy speed by 50%
},
AOE: {
name: 'AOE Tower',
cost: 45,
range: 2,
damage: 1.5,
attackSpeed: 0.3,
color: '#d35400',
special: 'aoe',
aoeRadius: 1 // Additional tiles affected
}
};
const ParticleTypes = {
DEATH_PARTICLE: {
lifetime: 1000, // milliseconds
speed: 0.1,
colors: ['#e74c3c', '#c0392b', '#d35400', '#e67e22']
},
PROJECTILE: {
lifetime: 300,
speed: 0.3,
color: '#ecf0f1'
},
AOE_EXPLOSION: {
lifetime: 500,
initialRadius: 10,
finalRadius: 60,
color: '#d35400',
ringWidth: 3
}
};
const EnemyTypes = {
BASIC: {
color: '#c0392b',
baseHealth: { min: 2, max: 6 },
speed: { min: 1, max: 1.5 },
damage: 0,
isRanged: false
},
RANGED: {
color: '#2c3e50',
baseHealth: { min: 1, max: 4 },
speed: { min: 0.7, max: 1.2 },
damage: 0.3,
attackRange: 3,
attackSpeed: 1, // attacks per second
isRanged: true
}
};
function createTower(type, position) {
return {
...TowerTypes[type],
type,
position,
lastAttackTime: 0,
currentHealth: 10,
maxHealth: 10
};
}
function createEnemy(startPosition) {
// 20% chance for ranged enemy
const type = Math.random() < 0.2 ? 'RANGED' : 'BASIC';
const enemyType = EnemyTypes[type];
const health = Math.floor(Math.random() *
(enemyType.baseHealth.max - enemyType.baseHealth.min + 1)) +
enemyType.baseHealth.min;
return {
position: { ...startPosition },
currentHealth: health,
maxHealth: health,
speed: enemyType.speed.min + Math.random() * (enemyType.speed.max - enemyType.speed.min),
pathIndex: 0,
type,
lastAttackTime: 0,
damage: enemyType.damage
};
}
function createParticle(type, position, angle) {
return {
position: { ...position },
velocity: {
x: Math.cos(angle) * type.speed,
y: Math.sin(angle) * type.speed
},
color: Array.isArray(type.colors)
? type.colors[Math.floor(Math.random() * type.colors.length)]
: type.color,
createdAt: performance.now(),
lifetime: type.lifetime,
size: 3 + Math.random() * 2
};
}
// Initialize game state at the bottom of the file
const gameState = {
grid: Array(20).fill().map(() => Array(20).fill('empty')),
path: [],
towers: [],
enemies: [],
currency: 100,
phase: 'placement',
isGameOver: false,
particles: [],
projectiles: [],
enemiesDestroyed: 0,
enemiesEscaped: 0,
// Define the function as part of the initial object
awardEnemyDestroyed() {
this.enemiesDestroyed++;
// Random reward between 1 and 3
const reward = Math.floor(Math.random() * 3) + 1;
this.currency += reward;
}
};
|