From 059a3f9534b8f3aeb831642acd397f3fe65c744c Mon Sep 17 00:00:00 2001 From: "J. M. Ellis" Date: Sun, 2 Jun 2024 17:08:27 -0700 Subject: [PATCH] Added platform generator class to platform.cs. --- Content/debugGrid.PNG | Bin 2305 -> 1207 bytes Game1.cs | 89 +++++----------------------------- Platform.cs | 110 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 76 deletions(-) diff --git a/Content/debugGrid.PNG b/Content/debugGrid.PNG index 24583de32acab128663b3d428bd64283bfbeba4d..24ba76c6b9bd9e0f4efa2ece9d0b15419452a0b9 100644 GIT binary patch literal 1207 zcmeAS@N?(olHy`uVBq!ia0y~yU~~Yow{Wll$)``g3IZv{;vjb?hIQv;UIIBR>5jgR z3=A9lx&I`x0{NT;9+AZi419+{nDKc2iWHz4A5Ry@kczmscMh&hag}I$XziB7_ADW( z>EI4?M)9-+wntf;c3f&N3h&;OwVg_-wSKRkSU^#9ZE^>x3WK0Xj%|Nqa& z%l-8|^?&Wv@Bi)Jw{PFSZPyv{mcFP{vW>K z*f1ymllq6w{Ezz&sDJtD|Dt~X-|*k3U)L)h-@p9_|B8C2`2R;T|NpeE`S$(esp8-H z6}7dse{ZI1pMSl(j+tc>kHUmhhDIG`juc4;h0_d7k!%7dj2axKF``RZ>iqq<%^hgi z+kMR|em&0Z-}kR`_TS}y_ul_}>H7TIZJ%Cj|GobGZ(bbQv6=jQ!I&G{-KusxO>m;`R2^|zPabS zdm%W`$861}H2?rJKVR~112(Q22x%)3f@aomEgS4bCG?NR|lDv2|p z(z~4Kps+qxjZiKj$Vg6*3j}w<@cp{X-at(5i-O$w^_Hs&x*v4=0nM6~+s3}zYP3Cf zvbs4Wa?nr+EP1e#RqZP{Q$J0Lb`6Sm4~m~Ld&=SBx&hg7jWi8gI4}DYM%s)J9-rha z1;Rrpm^kHVLUpjBV&i62Suk<}g2esh_GmGYy2|RBF`>2)=)^nuKuDw_hR}8JKC-pC3YZGb8cv@acbkmi99gmj^2W8gsjm2*|-YIa$r=K70))uH@CBtJ@Jt0&2 z$V@{6$-g^jGX&2PVm|uP9mP;bItcYRiBNh>De_kvo&pFi$AJHM&suu$HaeL4>?jGL zl)~`#Z$o`Bv=IaMl5Ut$>6h0~=lw>jYUl~U%j%{=|@mH$Dd_msPj+0Dysv^MVXGH+qKjdn3M_GTNB z)Yi$u$dT|za)n`9mo!l@ILgN+>LR(ygc;K&jt@?2Bo$?y4&INU)r3fi(w{8p+t*St zORs8#5=Lk{sXfSnU`q&MKM7r3kbGAGIdyt~E%}XYGuqM-T4bNI$f_5xFjo{}$dQPx z$tU&k6XlJ~IOr6)1|`mzwsBggRDK!8VnO=qp34;}u5EP_M`@aNZ#4e6juf7^cSiTC zjd2<=X#UtZ3|D!gEWG3S3l548R7h3s3r10r5`OLwG-0d%zsKs`_JyZ+m`(K6r=WwQ z*rmO$<_Y;n)N_X3N03#YQTDj$v`(oo#7LfIaxA(y9{3nTR@@%)89y4lsbIf_=mzbHad3^8*W zax&RLj)+0KQH4U<$$9v?;hHH`T|o8^V%1Se$+D4sw_^W!SE%gA zW!ozS>XhV~V>upZ(9RZbabQv1DvGWXMk`YafjG_U1bI%3StuRnEwGb;$o4! zBfp7lKYE#wcI1RMSG7~17#`DkH5~~sze0+qPmX~=#n6jGD&<09kxcC&+P1*-QKvEv z9uV|o=$Bl&onDM;baet-S4gy&+Q&B*x0HW~@=PG8*4sLPAVKhA#KjQ=_Yef1d{Rh9 z>4d*rxV8yKu2QMk{;DSo3e^`ucsFmK1Hz98%IgxUOkiXkas8D2i&E(wq@wA^Y`f7M zhN_wEgzFfp*lXN8J|+_U0X*g5HoSi*_y9eB@ul+uQ+Y6&yUTEKQu+{hNo9E2=kldR zu@Sq~Rs)n$&kqoaligqQo%tJY?uyy~ diff --git a/Game1.cs b/Game1.cs index 0bfa494..04b25de 100644 --- a/Game1.cs +++ b/Game1.cs @@ -63,16 +63,6 @@ namespace LunaLightXMG random = new Random(42); } - private void OnClientSizeChanged(object sender, EventArgs eventArgs) - { - if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0) - { - resizing = true; - CalculateRenderDestination(); - resizing = false; - } - } - protected override void Initialize() { // Set initial window size @@ -87,71 +77,8 @@ namespace LunaLightXMG // Play music PlayBackgroundMusic(sinkBeats); - // Test initialize platforms -- this should be abstracted to a class - // Also, I'm assuming using a tool like tiled will prevent having to - // manually input platforms like below... - platforms = new Platform[] - { - // platforms - new Platform(new Vector2(48, 128), 16, 16), - new Platform(new Vector2(64, 128), 16, 16), - new Platform(new Vector2(96, 96), 16, 16), - new Platform(new Vector2(112, 96), 16, 16), - new Platform(new Vector2(192, 128), 16, 16), - new Platform(new Vector2(208, 128), 16, 16), - new Platform(new Vector2(240, 96), 16, 16), - new Platform(new Vector2(256, 96), 16, 16), - new Platform(new Vector2(192, 64), 16, 16), - new Platform(new Vector2(208, 64), 16, 16), - new Platform(new Vector2(144, 32), 16, 16), - new Platform(new Vector2(160, 32), 16, 16), - new Platform(new Vector2(288, 128), 16, 16), - new Platform(new Vector2(16, 96), 16, 16), - - // bottom - new Platform(new Vector2(16, 160), 16, 16), - new Platform(new Vector2(32, 160), 16, 16), - new Platform(new Vector2(48, 160), 16, 16), - new Platform(new Vector2(64, 160), 16, 16), - new Platform(new Vector2(80, 160), 16, 16), - new Platform(new Vector2(96, 160), 16, 16), - new Platform(new Vector2(112, 160), 16, 16), - new Platform(new Vector2(128, 160), 16, 16), - new Platform(new Vector2(144, 160), 16, 16), - new Platform(new Vector2(160, 160), 16, 16), - new Platform(new Vector2(176, 160), 16, 16), - new Platform(new Vector2(192, 160), 16, 16), - new Platform(new Vector2(208, 160), 16, 16), - new Platform(new Vector2(224, 160), 16, 16), - new Platform(new Vector2(240, 160), 16, 16), - new Platform(new Vector2(256, 160), 16, 16), - new Platform(new Vector2(272, 160), 16, 16), - new Platform(new Vector2(288, 160), 16, 16), - - // left side - new Platform(new Vector2(0, 0), 16, 16), - new Platform(new Vector2(0, 16), 16, 16), - new Platform(new Vector2(0, 32), 16, 16), - new Platform(new Vector2(0, 48), 16, 16), - new Platform(new Vector2(0, 64), 16, 16), - new Platform(new Vector2(0, 80), 16, 16), - new Platform(new Vector2(0, 96), 16, 16), - new Platform(new Vector2(0, 112), 16, 16), - new Platform(new Vector2(0, 128), 16, 16), - new Platform(new Vector2(0, 144), 16, 16), - - // right side - new Platform(new Vector2(304, 0), 16, 16), - new Platform(new Vector2(304, 16), 16, 16), - new Platform(new Vector2(304, 32), 16, 16), - new Platform(new Vector2(304, 48), 16, 16), - new Platform(new Vector2(304, 64), 16, 16), - new Platform(new Vector2(304, 80), 16, 16), - new Platform(new Vector2(304, 96), 16, 16), - new Platform(new Vector2(304, 112), 16, 16), - new Platform(new Vector2(304, 128), 16, 16), - new Platform(new Vector2(304, 144), 16, 16) - }; + // Generate random platforms - test this idea out... + platforms = PlatformGenerator.CreatePlatforms(14); } @@ -169,7 +96,7 @@ namespace LunaLightXMG // Spawn 10 enemies at the start of the level for (int i = 0; i < 10; i++) { - Vector2 spawnPosition = new Vector2(16 + i*3,16); + Vector2 spawnPosition = new Vector2(16 + i*3,-16); Enemy enemy = new Enemy(spawnPosition, GetRandomValueBetween1And3()); enemy.LoadContent(Content); enemies.Add(enemy); @@ -257,6 +184,16 @@ namespace LunaLightXMG } } + private void OnClientSizeChanged(object sender, EventArgs eventArgs) + { + if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0) + { + resizing = true; + CalculateRenderDestination(); + resizing = false; + } + } + private void CalculateRenderDestination() { Point size = GraphicsDevice.Viewport.Bounds.Size; diff --git a/Platform.cs b/Platform.cs index 9f1b657..b0b611d 100644 --- a/Platform.cs +++ b/Platform.cs @@ -1,5 +1,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using System.Collections.Generic; +using System; namespace LunaLightXMG { @@ -19,5 +21,113 @@ namespace LunaLightXMG spriteBatch.Draw(texture, Position, Color.White); } } + + public class PlatformGenerator + { + private static Random random = new Random(); + + public static Platform[] CreatePlatforms(int numberOfPlatforms) + { + // Bottom wall (static) + List bottomPlatforms = new List + { + new Platform(new Vector2(0, 164), 16, 16), + new Platform(new Vector2(16, 164), 16, 16), + new Platform(new Vector2(32, 164), 16, 16), + new Platform(new Vector2(48, 164), 16, 16), + new Platform(new Vector2(64, 164), 16, 16), + new Platform(new Vector2(80, 164), 16, 16), + new Platform(new Vector2(96, 164), 16, 16), + new Platform(new Vector2(112, 164), 16, 16), + new Platform(new Vector2(128, 164), 16, 16), + new Platform(new Vector2(144, 164), 16, 16), + new Platform(new Vector2(160, 164), 16, 16), + new Platform(new Vector2(176, 164), 16, 16), + new Platform(new Vector2(192, 164), 16, 16), + new Platform(new Vector2(208, 164), 16, 16), + new Platform(new Vector2(224, 164), 16, 16), + new Platform(new Vector2(240, 164), 16, 16), + new Platform(new Vector2(256, 164), 16, 16), + new Platform(new Vector2(272, 164), 16, 16), + new Platform(new Vector2(288, 164), 16, 16), + new Platform(new Vector2(304, 164), 16, 16) + }; + + // Left side wall (static) + List leftSidePlatforms = new List + { + new Platform(new Vector2(0, -12), 16, 16), + new Platform(new Vector2(0, 4), 16, 16), + new Platform(new Vector2(0, 20), 16, 16), + new Platform(new Vector2(0, 36), 16, 16), + new Platform(new Vector2(0, 52), 16, 16), + new Platform(new Vector2(0, 68), 16, 16), + new Platform(new Vector2(0, 84), 16, 16), + new Platform(new Vector2(0, 100), 16, 16), + new Platform(new Vector2(0, 116), 16, 16), + new Platform(new Vector2(0, 132), 16, 16), + new Platform(new Vector2(0, 148), 16, 16) + }; + + // Right side wall (static) + List rightSidePlatforms = new List + { + new Platform(new Vector2(304, -12), 16, 16), + new Platform(new Vector2(304, 4), 16, 16), + new Platform(new Vector2(304, 20), 16, 16), + new Platform(new Vector2(304, 36), 16, 16), + new Platform(new Vector2(304, 52), 16, 16), + new Platform(new Vector2(304, 68), 16, 16), + new Platform(new Vector2(304, 84), 16, 16), + new Platform(new Vector2(304, 100), 16, 16), + new Platform(new Vector2(304, 116), 16, 16), + new Platform(new Vector2(304, 132), 16, 16), + new Platform(new Vector2(304, 148), 16, 16) + }; + // Generate random platforms + List randomizedPlatforms = CreateRandomPlatforms(numberOfPlatforms); + // Combine all platforms + List allPlatforms = new List(); + allPlatforms.AddRange(randomizedPlatforms); + allPlatforms.AddRange(bottomPlatforms); + allPlatforms.AddRange(leftSidePlatforms); + allPlatforms.AddRange(rightSidePlatforms); + + return allPlatforms.ToArray(); + } + + public static List CreateRandomPlatforms(int numberOfPlatforms) + { + List platforms = new List(); + + for (int i = 0; i < numberOfPlatforms; i++) + { + bool validPosition = false; + int x = 0; + int y = 0; + + while (!validPosition) + { + x = random.Next(1, 18) * 16; // Random x multiple of 16 between 16 and 288 + y = random.Next(3, 9) * 16; // Random y multiple of 16 between 48 and 132 + + validPosition = !platforms.Exists(p => p.Position.X == x && p.Position.Y == y); + + if (!validPosition) + { + x += 16; + if (x > 288) + { + x = 16; + y += 16; + } + } + } + platforms.Add(new Platform(new Vector2(x, y + 4), 16, 16)); + } + + return platforms; + } + } }