-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.ts
More file actions
109 lines (95 loc) · 2.7 KB
/
game.ts
File metadata and controls
109 lines (95 loc) · 2.7 KB
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
import { FPS, LOOP } from './constants'
import { Event, OPCODES } from './event'
import { Unit, UnitOptions } from './unit'
import { GetTime } from './utils'
class Handlers {
public static UnitCreate (payload: UnitOptions) {
if (typeof payload !== 'object')
return console.error ('payload is not an object')
if (!Unit.Validate(payload))
return console.error('payload is not valid')
Unit.Create(payload)
}
public static UnitStartAttacking (payload: { unit: number, target: number }) {
if (typeof payload !== 'object')
return console.error ('payload is not an object')
if (Unit.Exists(payload.unit)) {
game.GetUnit(payload.unit).StartAttacking(payload.target)
} else {
console.error(`target ${payload.target} does not exist`)
}
}
public static UnitStopAttacking (payload: number) {
if (typeof payload !== 'number')
return console.error('payload is not a valid GUID')
if (Unit.Exists(payload)) {
game.GetUnit(payload).StopAttacking()
} else {
console.error(`target ${payload} does not exist`)
}
}
public static DebugLog (payload: any) {
console.log(payload)
}
}
export class Game {
private last_update: number = GetTime()
private events: Event[] = []
private units: { [key: string]: Unit } = {}
constructor () {
setInterval(() => this.loop(), LOOP)
}
private loop () {
const time = GetTime()
const delta = time - this.last_update
if (delta >= FPS) {
this.last_update = time
this.update()
}
}
private update () {
for (const event of this.events) {
if (this.last_update < (event.delay || 0))
continue
this.handle(event)
this.events.shift()
}
}
private handle (event: Event) {
const payload = (typeof event.payload === 'function') ? event.payload() : event.payload
switch (event.opcode) {
case OPCODES.UNIT_CREATE:
Handlers.UnitCreate(payload)
break
case OPCODES.UNIT_START_ATTACKING:
Handlers.UnitStartAttacking(payload)
break
case OPCODES.UNIT_STOP_ATTACKING:
Handlers.UnitStopAttacking(payload)
break
case OPCODES.DEBUG_LOG:
Handlers.DebugLog(payload)
break
}
}
public Push (event: Event | Event[]) {
if (Array.isArray(event)) {
for (const e of event)
this.events.push(e)
return
}
this.events.push(event)
}
public AddUnit (unit: Unit) {
this.units[unit.guid] = unit
}
public GetUnit (guid: number) {
if (!this.units[guid])
throw Error('unit does not exist')
return this.units[guid]
}
public DoesUnitExist (guid: number) {
return !!this.units[guid]
}
}
export const game = new Game()