1+ < html lang ="en ">
2+ < head >
3+ < meta charset ="UTF-8 ">
4+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
5+ < title > Document</ title >
6+ </ head >
7+ < body >
8+ < canvas id ="gameCanvas "> </ canvas >
9+ < script type = "module ">
10+ import * as Forge from '/engine/bundle.js' ;
11+
12+
13+ const { Vector, PolygonCollider, Part, GameObject, Game, Scene, Layer, PhysicsBody, PhysicsEngine, ColorRender, Transform, BoxCollider, Camera} = Forge ;
14+
15+
16+ const game = new Game ( {
17+ name : "my game" ,
18+ canvas : document . getElementById ( 'gameCanvas' ) ,
19+ width : 800 ,
20+ height : 600 ,
21+ showFrameStats : "BASIC"
22+ } ) ;
23+
24+ const scene = new Scene ( {
25+ name : "scene 1"
26+ } ) ;
27+
28+ const pe = new PhysicsEngine ( {
29+ gravity : {
30+ x : 0 ,
31+ y : 1 ,
32+ scale : 0.001
33+ }
34+ } ) ;
35+ const camera = new Camera ( {
36+ name : "cam1"
37+ } ) ;
38+
39+ const l1 = new Layer ( {
40+ name : "layer 1" ,
41+ } ) ;
42+
43+ class PolySpawner extends Part {
44+ constructor ( { name, bottomRange, topRange, polyParent } ) {
45+ super ( { name } ) ;
46+ this . bottomRange = bottomRange ;
47+ this . topRange = topRange ;
48+ this . spawned = false ;
49+ this . polyParent = polyParent ;
50+ }
51+
52+ static singular = true ;
53+ static properties = {
54+ name : { type : "text" , default : "Part" } ,
55+ bottomRange : { type : "number" , default : 0 } ,
56+ topRange : { type : "number" , default : 100 } ,
57+ polyParent : { type : "Part" , default : null , subType : "Layer" } ,
58+ } ;
59+ randomPolygonPoints ( sides , radius ) {
60+ const points = [ ] ;
61+ const angleStep = ( Math . PI * 2 ) / sides ;
62+ for ( let i = 0 ; i < sides ; i ++ ) {
63+ const angle = i * angleStep ;
64+ points . push ( new Vector (
65+ Math . cos ( angle ) * radius ,
66+ Math . sin ( angle ) * radius
67+ ) ) ;
68+ }
69+ return points ;
70+ }
71+ act ( d ) {
72+ super . act ( d ) ;
73+ if ( ! this . spawned ) {
74+ this . spawned = true ;
75+ // Spawn the polygons
76+ const numPolygons = Math . floor ( Math . random ( ) * ( this . topRange - this . bottomRange + 1 ) ) + this . bottomRange ;
77+ for ( let i = 0 ; i < numPolygons ; i ++ ) {
78+ const sides = Math . floor ( Math . random ( ) * 5 ) + 3 ; // 3 to 7 sides
79+ const radius = Math . random ( ) * 40 + 20 ; // 20 to 60 px
80+ const x = Math . random ( ) * ( this . top . width - 100 ) + 50 ;
81+ const y = Math . random ( ) * 200 + 50 ;
82+ const color = `hsl(${ Math . random ( ) * 360 } , 80%, 60%)` ;
83+
84+ const nGon = new GameObject ( {
85+ name : `nGon${ i } `
86+ } ) ;
87+ const points = this . randomPolygonPoints ( sides , radius ) ;
88+ nGon . addChildren (
89+ new Transform ( {
90+ position : new Vector ( x , y ) ,
91+ rotation : Math . random ( ) * Math . PI * 2
92+ } ) ,
93+ new PolygonCollider ( {
94+ vertices : points ,
95+ } ) ,
96+ new ColorRender ( {
97+ vertices : points ,
98+ color : color
99+ } ) ,
100+ new PhysicsBody ( {
101+ isStatic : false ,
102+ restitution : 0.5 , // Bounciness
103+ } )
104+ ) ;
105+ // Add components to the polygon
106+ this . polyParent . addChild ( nGon ) ;
107+ }
108+ }
109+ }
110+ }
111+
112+ const polySpawner = new PolySpawner ( {
113+ name : "polySpawner" ,
114+ bottomRange : 1 ,
115+ topRange : 5 ,
116+ polyParent : l1
117+ } ) ;
118+
119+ const go = new GameObject ( {
120+ name : "block"
121+ } ) ;
122+
123+ const transform = new Transform ( {
124+ position : new Vector ( 0 , 300 ) ,
125+ rotation : 0 ,
126+ scale : Vector . From ( 1 )
127+ } ) ;
128+ const cr = new ColorRender ( {
129+ width : 300 ,
130+ height : 50 ,
131+ color : "red"
132+ } )
133+ const bc = new BoxCollider ( {
134+ width : 300 ,
135+ height : 50
136+ } ) ;
137+ const pb = new PhysicsBody ( {
138+ isStatic : true ,
139+ restitution : 0.8
140+ } ) ;
141+
142+ go . addChildren ( transform , cr , bc , pb ) ;
143+ l1 . addChild ( go ) ;
144+
145+ scene . addChildren ( pe , camera , l1 , polySpawner ) ;
146+
147+ game . addChild ( scene ) ;
148+
149+ game . start ( ) ;
150+ </ script >
151+ </ body >
152+ </ html >
0 commit comments