Skip to content

Commit

Permalink
Spawn subasteroids
Browse files Browse the repository at this point in the history
  • Loading branch information
hmans committed Sep 26, 2022
1 parent 73d5eb7 commit c0395dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
12 changes: 8 additions & 4 deletions apps/spacerage/src/scenes/gameplay/Asteroids.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "@hmans/physics3d"
import { useGLTF } from "@react-three/drei"
import { between, plusMinus } from "randomish"
import { useLayoutEffect } from "react"
import { useLayoutEffect, useTransition } from "react"
import { Material, Mesh, Quaternion, Vector3 } from "three"
import { Particle, Particles } from "vfx-composer-r3f"
import { ECS, Layers, spawnAsteroid } from "./state"
Expand All @@ -20,16 +20,20 @@ export const Asteroids = () => {

useLayoutEffect(() => {
/* Spawn a bunch of asteroids */
for (let i = 0; i < 100; i++) {
for (let i = 0; i < 1000; i++) {
spawnAsteroid(
new Vector3(plusMinus(100), plusMinus(100), 0),
new Vector3(plusMinus(300), plusMinus(300), 0),
between(0.8, 2)
)
}
}, [])

return (
<Particles geometry={mesh.geometry} material={mesh.material as Material}>
<Particles
capacity={10000}
geometry={mesh.geometry}
material={mesh.material as Material}
>
<ECS.Entities entities={entities}>
{({ asteroid }) => (
<ECS.Component name="rigidBody">
Expand Down
25 changes: 24 additions & 1 deletion apps/spacerage/src/scenes/gameplay/systems/BulletSystem.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useFrame } from "@react-three/fiber"
import { Vector3 } from "three"
import { Stage } from "../../../configuration"
import { ECS, Layers, spawnDebris } from "../state"
import { ECS, Layers, spawnAsteroid, spawnDebris } from "../state"
import * as RAPIER from "@dimforge/rapier3d-compat"
import { interactionGroups, usePhysicsWorld } from "@hmans/physics3d"
import { plusMinus } from "randomish"

const hittableEntities = ECS.world.archetype("health", "rigidBody")

Expand Down Expand Up @@ -62,6 +63,28 @@ export const BulletSystem = () => {
/* TODO: move this into a separate system */
if (otherEntity.health <= 0) {
ECS.world.queue.destroyEntity(otherEntity)

if (otherEntity.asteroid) {
const { scale } = otherEntity.asteroid

if (scale > 0.5) {
const position = otherEntity.rigidBody!.body.translation()

const step = (Math.PI * 2) / 3
for (let i = 0; i < 3; i++) {
const angle = step * i

spawnAsteroid(
new Vector3(
position.x + Math.cos(angle),
position.y + Math.sin(angle),
0
),
scale / 2
)
}
}
}
}
}

Expand Down

0 comments on commit c0395dc

Please sign in to comment.