Skip to content

Commit

Permalink
Session 91: Kill Bill-der
Browse files Browse the repository at this point in the history
  • Loading branch information
dominic committed May 14, 2021
1 parent baf4dc1 commit 8b4e3d4
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 216 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"codemirror": "^5.59.1",
"file-saver": "^2.0.2",
"gl-matrix": "^3.3.0",
"l1-path-finder": "^1.0.0",
"ndarray": "^1.0.19",
"rc-slider": "^9.7.2",
"react": ">= 16.8.0",
"react-dom": ">= 16.8.0",
Expand All @@ -42,6 +44,7 @@
"@types/hapi__hapi": "^20.0.3",
"@types/hapi__inert": "^5.2.2",
"@types/jest": "^25.2.3",
"@types/ndarray": "^1.0.9",
"@types/node-fetch": "^2.5.10",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
Expand Down
1 change: 1 addition & 0 deletions src/components/Bullet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum BulletType {
Standard,
Rocket,
Mortar,
Builder,
}
export const BULLET_TYPE_LENGTH = Object.values(BulletType).length / 2

Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export const MORTAR_GRAVITY = -15
export const MORTAR_MUZZLE_SPEED = 12
export const MORTAR_FIRING_HEIGHT = 0.5

// Builder stuff
export const BUILDER_SPEED = 3

// Camera
export const CAMERA_MIN_Y_OFFSET = 3
export const CAMERA_MAX_Y_OFFSET = 12
Expand Down
5 changes: 2 additions & 3 deletions src/entities/EntityComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Team } from '~/components/team'
import { Transform } from '~/components/Transform'
import { Transform3 } from '~/components/Transform3'
import { Type } from '~/entities/types'
import { BuilderComponent, BuilderCreator } from '~/systems/builder'
import { Builder } from '~/systems/builder'
import { Damager } from '~/systems/damager'
import { EmitterComponent } from '~/systems/emitter'
import { PickupType } from '~/systems/pickups'
Expand Down Expand Up @@ -40,8 +40,7 @@ export interface EntityComponents {
wall?: boolean

// Components that are currently not used by simulation
builder?: BuilderComponent
builderCreator?: BuilderCreator
builder?: Builder
harvestType?: PickupType
inventory?: PickupType[]
pickupType?: PickupType
Expand Down
41 changes: 40 additions & 1 deletion src/entities/EntityManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { vec2 } from 'gl-matrix'
import ndarray from 'ndarray'

import * as bullet from '~/components/Bullet'
import { Bullet } from '~/components/Bullet'
import * as damageable from '~/components/Damageable'
Expand All @@ -15,6 +18,7 @@ import { EntityId } from '~/entities/EntityId'
import { EntitySet } from '~/entities/EntitySet'
import { EntityStateContainer } from '~/entities/EntityStateContainer'
import { Type } from '~/entities/types'
import * as builder from '~/systems/builder'
import * as attack from '~/systems/damager'
import { EmitterComponent, emitterClone } from '~/systems/emitter'
import { PickupType } from '~/systems/pickups'
Expand All @@ -29,7 +33,11 @@ import { Aabb2 } from '~/util/aabb2'
import { Quadtree } from '~/util/quadtree'
import { minBiasAabbOverlap } from '~/util/quadtree/helpers'
import { SortedSet } from '~/util/SortedSet'
import { tileBox } from '~/util/tileMath'
import { tileBox, tileCoords } from '~/util/tileMath'

// l1-path-finder is not typed. Fix this, Jeff and Dom.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createPlanner = require('l1-path-finder')

type QuadtreeEntity = {
id: EntityId
Expand All @@ -45,6 +53,7 @@ export class EntityManager {
private predictedRegistrations: SortedSet<EntityId>

// components
builders: ComponentTable<builder.Builder>
bullets: ComponentTable<Bullet>
damageables: ComponentTable<Damageable>
damagers: ComponentTable<attack.Damager>
Expand Down Expand Up @@ -74,6 +83,9 @@ export class EntityManager {
// indexes
friendlyTeam: SortedSet<EntityId>
private quadtree: Quadtree<EntityId, QuadtreeEntity>
private obstacleGrid: ndarray.NdArray
private obstacleGridDirty: boolean
routePlanner: any

constructor(playfieldAabb: Aabb2) {
this.nextEntityIdUncommitted = 0
Expand All @@ -84,6 +96,7 @@ export class EntityManager {
this.predictedRegistrations = new SortedSet()

// components
this.builders = new ComponentTable(builder.clone)
this.bullets = new ComponentTable(bullet.clone)
this.damageables = new ComponentTable(damageable.clone)
this.damagers = new ComponentTable(attack.clone)
Expand All @@ -109,6 +122,7 @@ export class EntityManager {
this.walls = new EntitySet()

this.allContainers = [
this.builders,
this.bullets,
this.damageables,
this.damagers,
Expand Down Expand Up @@ -143,6 +157,9 @@ export class EntityManager {
return minBiasAabbOverlap(aabb, e.aabb)
},
})
this.obstacleGrid = ndarray(new Float32Array(4096), [64, 64])
this.obstacleGridDirty = false
this.routePlanner = createPlanner(this.obstacleGrid)
}

public undoPrediction(): void {
Expand Down Expand Up @@ -189,6 +206,10 @@ export class EntityManager {
}
}
this.toDelete = new SortedSet()
if (this.obstacleGridDirty) {
this.obstacleGridDirty = false
this.routePlanner = createPlanner(this.obstacleGrid)
}
}

public getPlayerId(playerNumber: number): EntityId | undefined {
Expand All @@ -206,6 +227,10 @@ export class EntityManager {
this.nextEntityIdUncommitted++
this.predictedRegistrations.add(id)

if (e.builder !== undefined) {
this.builders.set(id, e.builder)
}

if (e.bullet !== undefined) {
this.bullets.set(id, e.bullet)
}
Expand Down Expand Up @@ -320,12 +345,26 @@ export class EntityManager {
const entityAabb = tileBox(this.transforms.get(id)!.position)
this.quadtree.insert({ aabb: entityAabb, id: id })
}

if (this.walls.has(id)) {
const wallCoord = vec2.create()
tileCoords(wallCoord, this.transforms.get(id)!.position)
this.obstacleGrid.set(wallCoord[0] + 32, wallCoord[1] + 32, 1)
this.obstacleGridDirty = true
}
}

// FIXME: unindexing only occurs on deletes, not updates.
private unindexEntity(id: EntityId): void {
this.friendlyTeam.delete(id)
this.quadtree.remove(id)

if (this.walls.has(id)) {
const wallCoord = vec2.create()
tileCoords(wallCoord, this.transforms.get(id)!.position)
this.obstacleGrid.set(wallCoord[0] + 32, wallCoord[1] + 32, 0)
this.obstacleGridDirty = true
}
}

public queryByWorldPos(aabb: Aabb2): EntityId[] {
Expand Down
27 changes: 9 additions & 18 deletions src/entities/builder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vec4 } from 'gl-matrix'
import { vec2 } from 'gl-matrix'

import * as damageable from '~/components/Damageable'
Expand All @@ -8,34 +9,24 @@ import {
EntityComponents,
makeDefaultEntity,
} from '~/entities/EntityComponents'
import { EntityId } from '~/entities/EntityId'
import { BuilderComponent, BuilderMode } from '~/systems/builder'
import { PickupType } from '~/systems/pickups'

export const make = (params: {
export const makeBuilder = (params: {
source: vec2
destination: vec2
mode: BuilderMode
host: EntityId
path: vec2[]
}): EntityComponents => {
const e = makeDefaultEntity()
e.transform = transform.make()
e.transform.position = params.source

if (params.mode === BuilderMode.BUILD_TURRET) {
e.dropType = PickupType.Core
} else if (params.mode === BuilderMode.BUILD_WALL) {
e.dropType = PickupType.Wood
e.builder = {
target: params.destination,
}

e.builder = new BuilderComponent({
mode: params.mode,
target: params.destination,
host: params.host,
path: params.path,
})
e.renderable = 'not a real model' // fixme
e.entityModel = {
name: 'core',
color: vec4.fromValues(0, 1, 1, 1),
modifiers: {},
}

e.team = Team.Friendly
e.targetable = true
Expand Down
2 changes: 2 additions & 0 deletions src/entities/bullet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const bulletColor: Record<BulletType, [number, number, number, number]> = {
[BulletType.Standard]: [1, 0, 0, 1],
[BulletType.Rocket]: [1, 1, 0.5, 1],
[BulletType.Mortar]: [0.2, 0.2, 0.5, 1],
[BulletType.Builder]: [0, 0, 0, 0],
}

const bulletModel: Record<BulletType, string> = {
[BulletType.Standard]: 'bullet',
[BulletType.Rocket]: 'bullet',
[BulletType.Mortar]: 'mortar',
[BulletType.Builder]: '',
}

export const makeBullet = ({
Expand Down
2 changes: 0 additions & 2 deletions src/entities/player/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
makeDefaultEntity,
} from '~/entities/EntityComponents'
import { Type } from '~/entities/types'
import { BuilderCreator } from '~/systems/builder'
import * as shooter from '~/systems/shooter'
import * as tankMover from '~/systems/tankMover'

Expand All @@ -21,7 +20,6 @@ export const makePlayer = (): EntityComponents => {
e.targetable = true
e.team = Team.Friendly
e.moveable = true
e.builderCreator = new BuilderCreator()
e.damageable = damageable.make(PLAYER_HEALTH, {
offset: vec2.fromValues(-TILE_SIZE * 0.3, -TILE_SIZE * 0.5),
dimensions: vec2.fromValues(TILE_SIZE * 0.6, TILE_SIZE),
Expand Down
2 changes: 1 addition & 1 deletion src/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const simulate = (
systems.transformInit(simState)

systems.hiding(simState.entityManager)
// systems.builder(this, this.entityManager, dt)
systems.builder(simState, dt)
systems.shooter(simState)
systems.turret(simState, dt)
systems.bullet(simState, dt)
Expand Down
Loading

0 comments on commit 8b4e3d4

Please sign in to comment.