/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // TODO: use this.Content to load your game content here mRenderTarget = new RenderTarget(mGraphics.GraphicsDevice, 1920, 1080, 2048); mMasterRenderer = new MasterRenderer(mGraphics.GraphicsDevice, Content); mTerrain = new Terrain(Content, "heightmap", "shitty-grass", mGraphics.GraphicsDevice); mScene.Add(mTerrain); mTreeModel = Content.Load <Model>("tree"); mTreeMesh = new Mesh(mTreeModel); Random random = new Random(); for (uint i = 0; i < 2000; i++) // number of trees { var randomNumberX = (float)random.NextDouble(); var randomNumberZ = (float)random.NextDouble(); var location = new Vector3(2.0f * randomNumberX - 1.0f, 0.0f, 2.0f * randomNumberZ - 1.0f) * 125.0f; location.Y = mTerrain.GetHeight(location); // so every tree is on right height var actor = new Actor(mTreeMesh); actor.mModelMatrix = Matrix.CreateTranslation(location); mScene.Add(actor); } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } mCameraHandler.Update(gameTime.ElapsedGameTime.Milliseconds); // Does crash in -Z direction (when leaving terrain backwards) mCamera.mLocation.Y = mTerrain.GetHeight(mCamera.mLocation) + 2.0f; // TODO: Add your update logic here mCamera.UpdateView(); mFrustumCulling.CullActorsOutsideFrustum(mScene, mCamera); var nearestActor = RayCasting.CastRayFromMousePosition(mViewport, mCamera, mScene); var mouseState = Mouse.GetState(); if (nearestActor != null && mouseState.RightButton == ButtonState.Pressed) { mScene.Remove(nearestActor); } base.Update(gameTime); }
private static RayIntersection BinarySearch(Ray ray, Terrain terrain, float start, float finish, int count, int maxRecursion) { var half = start + (finish - start) / 2.0f; if (count >= maxRecursion) { var position = ray.Position + ray.Direction * half; position.Y = terrain.GetHeight(position); return(new RayIntersection(terrain.Actor, position, half, true, true)); } if (IntersectionInRange(ray, terrain, start, half)) { return(BinarySearch(ray, terrain, start, half, count + 1, maxRecursion)); } else { return(BinarySearch(ray, terrain, half, finish, count + 1, maxRecursion)); } }
private static bool IsUnderground(Vector3 position, Terrain terrain) { var height = terrain.GetHeight(position); return(height > position.Y); }
/// <summary> /// Load a file, read relevant infos from it and build level /// </summary> public void Load(string filename) { /* File is build up like this line by line(for now) * 1. Light information x,y,z * 1. Heightmap path * 2. Heightmap texture path * 3. M Mesh1 (including relative path to mesh? and other relevant informations) * 4. A Actor1 using Mesh1 is represented by following information: x,z,rotation,(scale?) * 5. A Actor2 using Mesh1 is represented by following information: x,z,rotation,(scale?) * 6. ...... * n. Mesh2 (including relative path to mesh? and other relevant informations) * k. EOF (end of file, stream reader stops reading) */ var currentDirectoryPath = Directory.GetCurrentDirectory(); if (currentDirectoryPath.EndsWith("Content") || currentDirectoryPath.EndsWith("Content/") || currentDirectoryPath.EndsWith("Content\\")) { Directory.SetCurrentDirectory("..\\bin\\Windows\\x86\\Debug\\"); } mLevelFilename = filename; try { using (var sr = new StreamReader(filename)) { var directoryPath = Path.GetDirectoryName(filename); Directory.SetCurrentDirectory(directoryPath); // Load/build terrain mTerrain = new Terrain(mContentManager, mDevice, sr.ReadLine(), sr.ReadLine()); mTerrain.mGrass.mActivated = Options.GraphicsQuality > 0 ? true : false; var mCollisionRectangles = new List <CollisionRectangle>(); string line; Mesh mesh = null; // Load the rest (objects and actors and light) while ((line = sr.ReadLine()) != null) { var lineSub = line.Substring(2); if (line.StartsWith("M ")) { // Read the path to the mesh mesh = mModelLoader.LoadMesh(lineSub); mesh.Path = lineSub; } else if (line.StartsWith("A ")) { var strings = lineSub.Split(' '); var actorLocation = strings[0].Split(','); var actorRotation = strings[1].Split(','); var actorOffset = strings[2]; var x = StringToFloat(actorLocation[0]); var z = StringToFloat(actorLocation[1]); var qX = StringToFloat(actorRotation[0]); var qY = StringToFloat(actorRotation[1]); var qZ = StringToFloat(actorRotation[2]); var qW = StringToFloat(actorRotation[3]); var offset = StringToFloat(actorOffset); var vector = new Vector3(x, 0.0f, z); vector.Y = mTerrain.GetHeight(vector) - offset; var quaternion = new Quaternion(qX, qY, qZ, qW); var matrix = Matrix.CreateFromQuaternion(quaternion) * Matrix.CreateTranslation(vector); var actor = new Actor(mesh) { ModelMatrix = matrix }; // We dont want to allow actors which hover above the ground to be in the collision caluc if (-offset < mesh.mMeshData.mBoundingSphere.Radius && actor.mBoundingRectangle.mCollidable) { mQuadTree.Insert(actor, actor.mBoundingRectangle.GetAxisAlignedRectangle(1)); mCollisionRectangles.Add(actor.mBoundingRectangle); } Add(actor); } else if (line.StartsWith("L ")) { var strings = lineSub.Split(' '); var time = strings[0]; var lightColor = strings[1].Split(','); var lightAmbient = strings[2]; var light = mSky.Light; mSky.mTime = StringToFloat(time); light.mColor.X = StringToFloat(lightColor[0]); light.mColor.Y = StringToFloat(lightColor[1]); light.mColor.Z = StringToFloat(lightColor[2]); light.mAmbient = StringToFloat(lightAmbient); } else if (line.StartsWith("B ")) { var strings = lineSub.Split(' '); var threshold = strings[0]; var power = strings[1]; var intensity = strings[2]; mPostProcessing.mBloom.mThreshold = StringToFloat(threshold); mPostProcessing.mBloom.mPower = StringToFloat(power); mPostProcessing.mBloom.mIntensity = StringToFloat(intensity); } else if (line.StartsWith("F ")) { var strings = lineSub.Split(' '); var lumaThreshold = strings[0]; var lumaThresholdMin = strings[1]; mPostProcessing.mFxaa.mLumaThreshold = StringToFloat(lumaThreshold); mPostProcessing.mFxaa.mLumaThresholdMin = StringToFloat(lumaThresholdMin); } else if (line.StartsWith("T ")) { mLevelTitle = lineSub; } else if (line.StartsWith("S ")) { var story = lineSub.Replace('|', '\n'); mLevelStory = story; } else if (line.StartsWith("H ")) { var strings = lineSub.Split(' '); var hutLocation = strings[0].Split(','); var hutRotation = strings[1].Split(','); var hutHp = strings[2]; var x = StringToFloat(hutLocation[0]); var z = StringToFloat(hutLocation[1]); var qX = StringToFloat(hutRotation[0]); var qY = StringToFloat(hutRotation[1]); var qZ = StringToFloat(hutRotation[2]); var qW = StringToFloat(hutRotation[3]); var hp = Convert.ToInt32(hutHp); var vector = new Vector3(x, 0.0f, z); vector.Y = mTerrain.GetHeight(vector); var quaternion = new Quaternion(qX, qY, qZ, qW); var matrix = Matrix.CreateFromQuaternion(quaternion) * Matrix.CreateTranslation(vector); var hut = new Hut(mHutMesh, mLumberjackMesh, mDoubleAxeKillerMesh, mAxeMesh, mSilverback, this, hp, 15.0f, 30.0f, false); hut.Actor.ModelMatrix = matrix; mHuts.Add(hut); if (!mEditMode) { mQuadTree.Insert(hut.Actor, hut.Actor.mBoundingRectangle.GetAxisAlignedRectangle(1)); Add(hut); } else { hut.Actor.IActor = null; Add(hut.Actor); } mCollisionRectangles.Add(hut.Actor.mBoundingRectangle); } else if (line.StartsWith("W ")) { var strings = lineSub.Split(' '); var fogColor = strings[0].Split(','); var fogDistance = strings[1]; mFog.mColor.X = StringToFloat(fogColor[0]); mFog.mColor.Y = StringToFloat(fogColor[1]); mFog.mColor.Z = StringToFloat(fogColor[2]); mFog.mDistance = StringToFloat(fogDistance); } else if (line.StartsWith("N ")) { mNextLevelFilename = lineSub; } else if (line.StartsWith("P ")) { if (mSpawnablePrimatesCount == -1) { mSpawnablePrimatesCount = Convert.ToInt32(lineSub); } } else if (line.StartsWith("I ")) { var strings = lineSub.Split(','); var x = StringToFloat(strings[0]); var z = StringToFloat(strings[1]); var location = new Vector3(x, 0.0f, z); location.Y = mTerrain.GetHeight(location); mInitialSilverbackLocation = location; mSilverback.Actor.ModelMatrix = Matrix.CreateTranslation(location); } } sr.Close(); Add(mTerrain); mVisibilityGraph = new Pathfinding.VisibilityGraph(mCollisionRectangles, new Rectangle(-128, -128, 256, 256), 1.0f, false); Directory.SetCurrentDirectory(currentDirectoryPath); } } catch (FileNotFoundException e) { Console.WriteLine("This file could not be read:"); Console.WriteLine(e.Message); throw; } }