/// <summary> /// Searchs for the tweets containing a specific search string. /// </summary> /// <param name="tweetText">The search string.</param> public async void SearchTweets(string tweetText) { if (!this.authorizer.IsAuthorized) { await this.authorizer.AuthorizeAsync(); this.context = new TwitterContext(authorizer); } string roamingFolder = ApplicationData.Current.RoamingFolder.Path + "\\"; var searchResult = (from search in context.Search where search.Type == SearchType.Search && search.Query == tweetText select search) .SingleOrDefault(); foreach (var result in searchResult.Statuses) { bool alreadyExists = false; bool fileExists = true; HttpClient http = new HttpClient(); HttpResponseMessage response = await http.GetAsync(result.User.ProfileImageUrl); string userAvatarName = result.User.Identifier.UserID + Path.GetExtension(result.User.ProfileImageUrl); Tweets[userAvatarName] = new TweetInfo("@" + result.User.Identifier.ScreenName, result.Text); try { StorageFile existingFile = await ApplicationData.Current.RoamingFolder.GetFileAsync(userAvatarName); } catch (FileNotFoundException f) { fileExists = false; } if (!fileExists) { using (NativeFileStream file = new NativeFileStream(roamingFolder + userAvatarName, NativeFileMode.Create, NativeFileAccess.Write)) { await response.Content.CopyToAsync(file.AsOutputStream().AsStreamForWrite()); } } for (int i = 0; i < AvatarTextures.Count; i++) { if (AvatarTextures[i].TexturePath == userAvatarName) { alreadyExists = true; break; } } if (!alreadyExists) { AddTextureFromFile(userAvatarName); } } }
/// <summary> /// Renders the scene. /// </summary> /// <param name="render">Render target to draw contents to.</param> public void Render(TargetBase render) { float newTime = (float)clock.ElapsedMilliseconds / 1000.0f; float frameTime = newTime - currentTime; currentTime = newTime; if (!gameStarted) { return; } if (gameStarted && !gamePaused) { accumulator += frameTime; } var d3dContext = render.DeviceManager.ContextDirect3D; float width = (float)render.RenderTargetSize.Width; float height = (float)render.RenderTargetSize.Height; renderer.View = Matrix.LookAtLH(cameraPosition, new Vector3(0, 0, 70), Vector3.UnitY); renderer.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, width / (float)height, 0.1f, 1000.0f); d3dContext.OutputMerger.BlendState = blend; d3dContext.Rasterizer.State = raster; d3dContext.OutputMerger.SetTargets(render.DepthStencilView, render.RenderTargetView); d3dContext.ClearDepthStencilView(render.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); d3dContext.ClearRenderTargetView(render.RenderTargetView, Colors.Black); d3dContext.InputAssembler.InputLayout = layout; d3dContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; d3dContext.VertexShader.SetConstantBuffer(0, renderer.constantBuffer); d3dContext.VertexShader.Set(vertexShader); d3dContext.PixelShader.SetSampler(0, sampler); d3dContext.PixelShader.Set(pixelShader); while (accumulator >= updateFrequency) { drawQueue.Clear(); parts.Sort(new EntityTextureComparer()); for (int i = 0; i < parts.Count; i++) { Entity currentPart = parts[i]; currentPart.Transform = currentPart.Transform * Matrix.Translation(0, 0, -200.0f * updateFrequency); roadDistance = (currentPart.Model.BBox.Maximum.Z - currentPart.Model.BBox.Minimum.Z); // TODO: don't hardcode values if (currentPart.Transform.M43 < -roadDistance) { currentPart.Transform = currentPart.Transform * Matrix.Translation(0, 0, roadDistance * 18); } if (currentPart.Child != null) { tempChilds.Add(currentPart.Child); } if (currentPart.Lightmap != null) { renderer.SetActiveTexture(currentPart.Lightmap, 1); } else { renderer.SetActiveTexture(white, 1); } drawQueue.Add(currentPart); } tempChilds.Sort(new EntityMeshComparer()); for (int i = 0; i < tempChilds.Count; i++) { Entity currentPart = tempChilds[i]; drawQueue.Add(currentPart); } tempChilds.Clear(); if (playerEntity != null) { playerEntity.Update(updateFrequency); drawQueue.Add(playerEntity); drawQueue.Add(playerEntity.Child); cameraPosition.X = playerEntity.Transform.M41; } for (int i = 0; i < collidableObjects.Count; i++) { Entity currentObject = collidableObjects[i]; currentObject.Transform = Matrix.Translation(0, 0, -200.0f * updateFrequency) * currentObject.Transform; // HACK currentObject.RestoreMatrix = true; currentObject.oldMatrix = currentObject.Transform; // Pre-rotation collision if (currentObject.IsCollidable && currentObject.BBox.Intersects(ref playerEntity.BBox)) { if (currentObject.IsPickable) { collidedIcons.Add(currentObject); MainViewModel.Instance.Followers++; if (client.Tweets.ContainsKey(currentObject.Texture.TexturePath)) { TweetInfo info = client.Tweets[currentObject.Texture.TexturePath]; MainViewModel.Instance.ActualTweet = info.TweetText; MainViewModel.Instance.ActualUser = info.Username; } } else { // TODO kill gamePaused = true; if (GameEnded != null) { GameEnded(null, EventArgs.Empty); } } } if (currentObject.Rotates) { currentObject.oldMatrix = Matrix.RotationY(clock.ElapsedMilliseconds * 0.005f + currentObject.EffectDelay) * Matrix.Translation(0, (float)Math.Sin(totalTime / 100.0f + currentObject.EffectDelay) * 5.0f, 0) * currentObject.Transform; } if (client.AvatarTextures.Count > 0 && currentObject.Texture.Equals(white)) { currentObject.Texture = client.AvatarTextures[i % client.AvatarTextures.Count]; } drawQueue.Add(currentObject); } for (int i = 0; i < collidedIcons.Count; i++) { collidableObjects.Remove(collidedIcons[i]); } collidedIcons.Clear(); accumulator -= updateFrequency; nextCarAdd -= updateFrequency; nextIconAdd -= updateFrequency; nextTweetSearch -= updateFrequency; MainViewModel.Instance.Miles += Math.Round(updateFrequency, 2); if (nextTweetSearch <= 0) { client.SearchTweets("#" + MainViewModel.Instance.Hashtag); nextTweetSearch = 30.0f; } if (nextCarAdd <= 0) { int numCars = randomizer.Next(2) + 3; for (int i = 0; i < numCars; i++) { int carPlace = randomizer.Next(0, 10); int carType = randomizer.Next(0, 10); bool isBigCar = carType > 6; Entity car = factory.CreateEntity(isBigCar ? BigCarModelPath : SmallCarModelPath, CarsTexturePath); car.Transform = Matrix.Translation(carPlace > 4 ? -car.Model.Bounds.X / 2 : car.Model.Bounds.X / 2, isBigCar ? 6.0f : 0.0f, i * roadDistance * 4 + roadDistance * 16); car.IsCollidable = true; car.IsPickable = false; collidableObjects.Add(car); } nextCarAdd = (float)randomizer.NextDouble() * 2.0f + 5.0f; } if (nextIconAdd <= 0) { int numIcons = randomizer.Next(2, 5) * 5; int iconPlace = randomizer.Next(0, 3) - 1; for (int i = 0; i < numIcons; i++) { Entity icon = factory.CreateEntity(IconPictureModelPath, WhiteTexturePath); icon.Transform = Matrix.Translation(iconPlace * PathWidth, 0, i * PathWidth * 2 + roadDistance * 16); icon.IsCollidable = true; icon.IsPickable = true; icon.Rotates = true; icon.EffectDelay = i * 0.2f; if (client.AvatarTextures.Count > 0) { icon.Texture = client.AvatarTextures[i % client.AvatarTextures.Count]; } collidableObjects.Add(icon); } nextIconAdd = (float)randomizer.NextDouble() * 2.0f + 3.0f; } } for (int i = 0; i < drawQueue.Count; i++) { if (drawQueue[i].RestoreMatrix) { Matrix tmp = drawQueue[i].Transform; drawQueue[i].Transform = drawQueue[i].oldMatrix; renderer.DrawEntity(drawQueue[i]); drawQueue[i].Transform = tmp; } else { renderer.DrawEntity(drawQueue[i]); } } totalTime = clock.ElapsedMilliseconds; }