Related
I want to change GUI button text when I click a UI button. onGUI() method which I have written, is below.
This method is used to create new buttons when the button is clicked and to show the Sub-parts of a tree structure.
If I show you the change I want to make in the code, for example, when I click UI Latin Button, currentPart.EnglishTitle changes like currentPart.LatinTitle. I also mentioned this part as a comment in the code
currentPart is a HumanBodyPart object. HumanBodyPart is a class that stores my nodes of my tree structure.
I give you the necessary parts of my code. If there is a missing part in the code that I have given, I can edit the desired parts.
onGUI() method is here...
private void OnGUI()
{
Vector3 scale = new Vector3(Screen.width / nativeSize.x, Screen.height / nativeSize.y, 1.0f);
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, scale);
float spacing = 25;
float x = 7 + spacing;
float y = 63;
HumanBodyPart mainBodyPart = bodyVisualizer.BodyData.Body.SubParts[0];
List<HumanBodyPart> nextPartsToRender = new List<HumanBodyPart>(new HumanBodyPart[] { mainBodyPart });
List<HumanBodyPart> allPartsToRender = new List<HumanBodyPart>(new HumanBodyPart[] { mainBodyPart });
scrollPosition = GUI.BeginScrollView(new Rect(7, y, 264, 485), scrollPosition, new Rect(7, y, 528, scrollPosition_y));
while (nextPartsToRender.Count > 0)
{
HumanBodyPart currentPart = nextPartsToRender[0];
nextPartsToRender.RemoveAt(0);
//The place I want to change above is the place "currentPart.English" in a bottom line
if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), currentPart.EnglishTitle))
{
if (!currentPart.IsClicked)
{
currentPart.IsClicked = true;
HumanBodyVisualizer.ShowMode showModeFullBody = HumanBodyVisualizer.ShowMode.Invisible;
bodyVisualizer.ShowBody(showModeFullBody);
AllSubPartsAndRoot.Insert(AllSubPartsAndRoot.Count, currentPart);
addAllSubPartsOfClickButton(currentPart, AllSubPartsAndRoot, AllSubPartsAndRoot.Count - 1);
HumanBodyVisualizer.ShowMode showModeCurrentPart = HumanBodyVisualizer.ShowMode.LowTransparent;
for (int i = 0; i < AllSubPartsAndRoot.Count; i++)
{
bodyVisualizer.ShowBodyPart(showModeCurrentPart, AllSubPartsAndRoot[i]);
}
}
else
{
currentPart.IsClicked = false;
List<HumanBodyPart> RemoveBodyParts = new List<HumanBodyPart>();
RemoveBodyParts.Insert(0, currentPart);
addAllSubPartsOfClickButton(currentPart, RemoveBodyParts, 1);
for (int i = 0; i < RemoveBodyParts.Count; i++)
{
if (AllSubPartsAndRoot.Contains(RemoveBodyParts[i]))
{
bodyVisualizer.ShowBodyPart(HumanBodyVisualizer.ShowMode.Invisible, RemoveBodyParts[i]);
AllSubPartsAndRoot.Remove(RemoveBodyParts[i]);
}
}
if (AllSubPartsAndRoot.Count == 0)
{
bodyVisualizer.ShowBody(HumanBodyVisualizer.ShowMode.LowTransparent);
}
else
{
for (int ii = 0; ii < AllSubPartsAndRoot.Count; ii++)
{
bodyVisualizer.ShowBodyPart(HumanBodyVisualizer.ShowMode.LowTransparent, AllSubPartsAndRoot[ii]);
}
}
}
}
if (currentPart.SubParts.Count != 0)
{
if (GUI.Button(new Rect(x - spacing + currentPart.DrawDepth * spacing, y, 20, 20), ">"))
{
if (!currentPart.IsExpanded)
{
currentPart.IsExpanded = true;
}
else
currentPart.IsExpanded = false;
}
if (currentPart.IsExpanded)
{
nextPartsToRender.InsertRange(0, currentPart.SubParts);
allPartsToRender.InsertRange(allPartsToRender.Count - 1, currentPart.SubParts);
scrollPosition_y = allPartsToRender.Count * spacing;
}
}
y += spacing;
index++;
}
// End the scroll view that we began above.
GUI.EndScrollView();
}
public Button turkishButton;
public Button englishButton;
public Button latinButton;
The above is UI buttons in the script.
In general you shouldn't use OnGUI but rather the Unity UI system.
You could just have a string with the text to display and do e.g.
private string onGuiButtonLabel;
public Button turkishButton;
public Button englishButton;
public Button latinButton;
private void Awake()
{
turkishButton.onClick.AddListener(()=>{onGuiButtonLabel = "Turkish";})
englishButton.onClick.AddListener(()=>{onGuiButtonLabel = "English";})
latinButton.onClick.AddListener(()=>{onGuiButtonLabel = "Latin";})
}
and then in OnGUI you use
if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), onGuiButtonLabel))
or maybe use a kind of Dictionary with an enum like
public Language
{
English,
Turkish,
Latin
}
private Dictionary<Language, string>() titles;
private Language curentLanguage;
// or wherever you want to initialize it
private void Awake()
{
titles = new Dictionary<Language, string>()
{
{Language.English, currentPart.EnglishTitle}
{Language.Turkish, currentPart.TurkishTitle}
{Language.Latin, currentPart.LatinTitle}
}
turkishButton.onClick.AddListener(()=>{curentLanguage = Language.Turkish;})
englishButton.onClick.AddListener(()=>{curentLanguage = Language.English;})
latinButton.onClick.AddListener(()=>{curentLanguage = Language.Latin;})
}
and then in OnGUI use
if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), titles[currentLanguage]))
The thing that I want to do is writing a method which can call in onGUI() method.
I wrote this method. However when I run the program , method did not show the effect.
private void ShowSubPartsOnClick(float x, float y, float widthLABEL, float heigth, HumanBodyPart bodyPart)
{
x = x + 14;
for(int i = 0; i < bodyPart.SubParts.Count; i++)
{
y = y + 14;
GUI.Label(new Rect(x+14,y,widthLABEL,heigth), bodyPart.SubParts[i].EnglishTitle);
if(GUI.Button(new Rect(x, y, 14, heigth),"+"))
{
ShowSubPartsOnClick(x, y, widthLABEL, heigth, bodyPart.SubParts[i]);
}
}
}
}
private void OnGUI()
{
GUI.Label(new Rect(text.transform.position.x+14, text.transform.position.y, text.rectTransform.sizeDelta.x, 14),bodyVisualizer.BodyData.Body.SubParts[0].EnglishTitle);
if(GUI.Button(new Rect(text.transform.position.x, text.transform.position.y, 14, 14), "+"))
{
ShowSubPartsOnClick(text.transform.position.x, text.transform.position.y, text.rectTransform.sizeDelta.x, 14, bodyVisualizer.BodyData.Body.SubParts[0]);
}
}
How can I fix this or what is the problem?
The challenge here is functions like GUI.Label and GUI.Button must be invoked directly from OnGUI to work:
From Unity Forums: "The only place you can draw/create GUI elements is by triggering them from inside an OnGUI function."
Given the recommendation there, one solution is to run an iterative depth first search via a while loop. See attached example.
That being said, I'd highly recommend using Unity Canvas instead of OnGUI. It's much more powerful and its programmatic logic is not constrained to a single function.
OnGUI Snippet:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HumanBodyPart
{
public string EnglishTitle;
public List<HumanBodyPart> SubParts;
public bool IsExpanded;
public int DrawDepth;
public HumanBodyPart(string title, HumanBodyPart[] subParts)
{
this.EnglishTitle = title;
this.SubParts = new List<HumanBodyPart>();
this.SubParts.AddRange(subParts);
this.IsExpanded = false;
this.DrawDepth = 0;
}
}
public class Script : MonoBehaviour
{
[SerializeField]
Text text;
HumanBodyPart mainBodyPart;
private void Start()
{
HumanBodyPart subSubSubBodyPart = new HumanBodyPart("SubSubSubBodyPart", new HumanBodyPart[] { });
HumanBodyPart subSubBodyPart1 = new HumanBodyPart("SubSubBodyPart1", new HumanBodyPart[] { subSubSubBodyPart });
HumanBodyPart subSubBodyPart2 = new HumanBodyPart("SubSubBodyPart2", new HumanBodyPart[] { });
HumanBodyPart subBodyPart = new HumanBodyPart("SubBodyPart", new HumanBodyPart[] { subSubBodyPart1, subSubBodyPart2});
mainBodyPart = new HumanBodyPart("BodyPart", new HumanBodyPart[] { subBodyPart });
UpdateDrawDepths(mainBodyPart);
}
private void UpdateDrawDepths(HumanBodyPart currentBodyPart, int currentDrawDepth=0)
{
currentBodyPart.DrawDepth = currentDrawDepth;
foreach (HumanBodyPart bodyPart in currentBodyPart.SubParts)
{
UpdateDrawDepths(bodyPart, currentDrawDepth + 1);
}
}
private void OnGUI()
{
float spacing = 30;
float x = text.transform.position.x + spacing;
float y = text.transform.position.y;
int drawDepth = 0;
List<HumanBodyPart> nextPartsToRender = new List<HumanBodyPart>(new HumanBodyPart[] { mainBodyPart });
while (nextPartsToRender.Count > 0)
{
HumanBodyPart currentPart = nextPartsToRender[0];
GUI.Label(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), currentPart.EnglishTitle);
nextPartsToRender.RemoveAt(0);
if (GUI.Button(new Rect(x - spacing + currentPart.DrawDepth * spacing, y, 20, 20), "+"))
{
currentPart.IsExpanded = true;
}
if (currentPart.IsExpanded)
{
nextPartsToRender.InsertRange(0, currentPart.SubParts);
}
y += spacing;
}
}
}
Error saying 'MPPlayer' could not be found, however I think it's referenced.
private void Menu_Lobby()
{
ScrollLobby = GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth("200"));
foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList)
{
GUILayout.Box (pl.PlayerName);
}
GUILayout.EndScrollView();
}
Full page code:
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour
{
public string CurrentMenu;
public string MatchName = "";
public string MatchPassword = "";
public int MatchMaxPlayers = 20;
private Vector2 ScrollLobby = Vector2.zero;
void Start()
{
CurrentMenu = "Main";
MatchName = "My Server " + Random.Range(0 , 100);
}
void OnGUI()
{
if (CurrentMenu == "Main")
Menu_Main();
if (CurrentMenu == "Lobby")
Menu_Lobby();
if (CurrentMenu == "Host")
Menu_HostGame();
}
public void NavigateTo(string nextmenu)
{
CurrentMenu = nextmenu;
}
private void Menu_Main()
{
if (GUI.Button(new Rect(10,10,200,50), "Create Game"))
{
NavigateTo ("Host");
}
GUI.Label(new Rect(220, 10, 130, 30), "Player Name");
MultiplayerManager.instance.PlayerName = GUI.TextField( new Rect(350, 10, 150, 30), MultiplayerManager.instance.PlayerName);
if (GUI.Button (new Rect(510,10,100,30), "Save"))
{
PlayerPrefs.SetString("PlayerName", MultiplayerManager.instance.PlayerName);
}
}
private void Menu_HostGame()
{
// Buttons Host Game
if (GUI.Button(new Rect(10,10,200,50), "Back"))
{
NavigateTo("Main");
}
if (GUI.Button(new Rect(10,60,200,50), "Start Server"))
{
MultiplayerManager.instance.StartServer(MatchName, MatchPassword, MatchMaxPlayers);
}
GUI.Label(new Rect(220, 10, 130, 30), "Match Name");
MatchName = GUI.TextField( new Rect(400, 10, 200, 30), MatchName);
GUI.Label(new Rect(220, 50, 130, 30), "Match Password");
MatchPassword = GUI.PasswordField( new Rect(400, 50, 200, 30), MatchPassword, '*');
GUI.Label(new Rect(220, 90, 130, 30), "Match Max Players");
GUI.Label(new Rect(400, 90, 200, 30), MatchMaxPlayers.ToString());
MatchMaxPlayers = Mathf.Clamp (MatchMaxPlayers, 6, 20); // Allows min of 6 players and a max of 20 players.
if (GUI.Button (new Rect(425,90,25,30), "+"))
MatchMaxPlayers += 2; // Adds 2 players to the number
if (GUI.Button (new Rect(450,90,25,30), "-"))
MatchMaxPlayers -= 2; // Takes 2 players from the number
}
private void Menu_Lobby()
{
ScrollLobby = GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth("200"));
foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList)
{
GUILayout.Box (pl.PlayerName);
}
GUILayout.EndScrollView();
}
void OnServerInitialized()
{
NavigateTo("Lobby");
}
void OnConnectedToServer()
{
NavigateTo("Lobby");
}
}
maybe this would help
http://unity3d.com/learn/tutorials/modules
especially this one
http://unity3d.com/learn/tutorials/modules/beginner/scripting/variable-scope-and-access-modifiers
Change foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList) to foreach (var pl in MultiplayerManager.instance.PlayerList) and mouseover in VS the var part to find out what's the actual type name.
Am I the only one experiencing this? I have nothing but a little, tiny, game, but it's close to unplayable now because of FPS drops.
Here's my code, if anyone is wondering:
Game1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
namespace Innovationally
{
enum GameState
{
TITLESCREEN,
HELPSCREEN,
PLAYING,
WON,
LOST
}
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameState gameState = GameState.PLAYING;
//PLAYER STATS
public static Player player;
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;
float playerMoveSpeed;
//COLLISION STATS
Rectangle kollision;
int bHit;
//LEVEL STATS
int level_number = 0;
int loadlevel = 0;
Texture2D hud, level0, level1, level2, level3, level4, level5;
Vector2 levelPos;
//ROOM STATS
List<int> tile_life = new List<int>();
Texture2D tile_gfx, stairsUp, stairsDown;
List<Vector2> tile_position = new List<Vector2>();
List<int> tile_type = new List<int>();
List<int> tile_elev = new List<int>();
int antlabb = 0;
int antvapen = 0;
int antpolis = 0;
int antwavers = 0;
int researchSpan;
//MISC
SpriteFont font;
Loot loot;
//GAMEPLAY STATS
TimeSpan timeElapsed;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 800;
graphics.PreferredBackBufferWidth = 900;
}
protected override void Initialize()
{
player = new Player();
playerMoveSpeed = 4.0f;
levelPos.X = 0;
levelPos.Y = 0;
loot = new Loot();
researchSpan = 120;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Vector2 playerPosition = new Vector2(430, 450);
//LEVEL STATS
LaddaLevel(level_number);
level0 = Content.Load<Texture2D>("level0");
level1 = Content.Load<Texture2D>("level1");
level2 = Content.Load<Texture2D>("level2");
level3 = Content.Load<Texture2D>("level3");
level4 = Content.Load<Texture2D>("level4");
level5 = Content.Load<Texture2D>("level5");
hud = Content.Load<Texture2D>("hud");
//ROOM STATS
tile_gfx = Content.Load<Texture2D>("tile");
stairsUp = Content.Load<Texture2D>("stairsUp");
stairsDown = Content.Load<Texture2D>("stairsDown");
font = Content.Load<SpriteFont>("SpriteFont1");
player.Initialize(Content.Load<Texture2D>("Leftplayer"), playerPosition);
//SOMEONE STOP THE MUSIC, MUSIC.
MediaPlayer.Volume = 0.5f;
MediaPlayer.IsRepeating = true;
MediaPlayer.Play(Content.Load<Song>("barn-beat"));
}
public void LaddaLevel(int nummer)
{
StreamReader SR = new StreamReader(nummer.ToString());
string bana = SR.ReadToEnd();
SR.Close();
int temp_positionY = 0;
int temp_positionX = 0;
tile_position.Clear();
tile_type.Clear();
tile_life.Clear();
for (int i = 0; i < bana.Length; i++)
{
switch (bana[i])
{
case ' ':
temp_positionX++;
break;
case '0':
tile_life.Add(loot.myRnd.Next(8));
tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
temp_positionX++;
tile_type.Add(int.Parse(bana[i].ToString()));
break;
case '8':
tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
temp_positionX++;
tile_type.Add(int.Parse(bana[i].ToString()));
tile_life.Add(8);
break;
case '9':
tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
temp_positionX++;
tile_type.Add(int.Parse(bana[i].ToString()));
tile_life.Add(9);
break;
case '\n':
temp_positionY++;
temp_positionX = 0;
break;
}
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
this.Exit();
switch (gameState)
{
case GameState.TITLESCREEN:
if (currentKeyboardState.IsKeyDown(Keys.S) && previousKeyboardState.IsKeyUp(Keys.S))
gameState = GameState.PLAYING;
if (currentKeyboardState.IsKeyDown(Keys.H) && previousKeyboardState.IsKeyUp(Keys.H))
gameState = GameState.HELPSCREEN;
break;
case GameState.HELPSCREEN:
if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
gameState = GameState.TITLESCREEN;
break;
case GameState.PLAYING:
timeElapsed += gameTime.ElapsedGameTime;
UpdatePlayer(gameTime);
UpdateResearchCenters(gameTime);
UpdateCollisions(gameTime);
break;
case GameState.LOST:
if (currentKeyboardState.IsKeyDown(Keys.S) && previousKeyboardState.IsKeyUp(Keys.S))
{
loadlevel = 0;
gameState = GameState.PLAYING;
}
break;
}
base.Update(gameTime);
}
private void UpdatePlayer(GameTime gameTime)
{
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
player.angle = (float)Math.PI * 1.5f;
player.Position.X -= playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
player.angle = (float)Math.PI / 2;
player.Position.X += playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
player.angle = (float)Math.PI * 2;
player.Position.Y -= playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
player.angle = (float)Math.PI;
player.Position.Y += playerMoveSpeed;
}
if (player.health <= 0)
gameState = GameState.LOST;
}
public void UpdateResearchCenters(GameTime gameTime)
{
researchSpan -= gameTime.ElapsedGameTime.Seconds;
if (researchSpan <= 0)
{
loot.RandomResearch();
researchSpan = 120;
}
}
public void UpdateCollisions(GameTime gameTime)
{
Rectangle playerBox = new Rectangle((int)player.Position.X - 20, (int)player.Position.Y - 20, 40, 37);
Rectangle levelBox = new Rectangle(0, 0, 900, 800);
for (int i = 0; i < tile_position.Count; i++)
{
Rectangle tileBox = new Rectangle((int)tile_position[i].X, (int)tile_position[i].Y, 100, 100);
if (playerBox.Intersects(tileBox))
{
if (tile_life[i] <= 9)
{
if (tile_life[i] == 9 && currentKeyboardState.IsKeyDown(Keys.Space))
{
loadlevel += 1;
LaddaLevel(loadlevel);
}
else if (tile_life[i] == 8 && currentKeyboardState.IsKeyDown(Keys.Space))
{
loadlevel -= 1;
LaddaLevel(loadlevel);
}
else if (tile_life[i] == 7 && currentKeyboardState.IsKeyDown(Keys.Space))
{
loot.RandomLoot();
tile_life[i] = 70;
}
else if ((tile_life[i] == 6 || tile_life[i] == 60) && currentKeyboardState.IsKeyDown(Keys.Space) && (player.mvgelever >= 1 || player.problemelever >= 1 || player.normalaelever >= 1))
{
if (player.mvgelever >= 1)
{
player.mvgelever -= 1;
tile_elev.Add(1);
}
else if (player.normalaelever >= 1)
{
player.normalaelever -= 1;
tile_elev.Add(2);
}
else if (player.problemelever >= 1)
{
player.problemelever -= 1;
tile_elev.Add(3);
}
tile_life[i] = 60;
antlabb += 1;
}
if (tile_life[i] == 60 && currentKeyboardState.IsKeyDown(Keys.Space))
{
antlabb -= 1;
if (tile_elev.Contains(1))
player.mvgelever += 1;
if (tile_elev.Contains(2))
player.normalaelever += 1;
if (tile_elev.Contains(3))
player.problemelever += 1;
tile_life[i] = 6;
}
}
}
}
//Överlappar vi?
kollision = Intersection(playerBox, levelBox);
if (kollision.Width > 0 && kollision.Height > 0)
{
Rectangle r1 = Normalize(playerBox, kollision);
Rectangle r2 = Normalize(levelBox, kollision);
if (loadlevel == 0)
bHit = TestCollision(player.PlayerTexture, r1, level0, r2);
if (loadlevel == 1)
bHit = TestCollision(player.PlayerTexture, r1, level1, r2);
if (loadlevel == 2)
bHit = TestCollision(player.PlayerTexture, r1, level2, r2);
if (loadlevel == 3)
bHit = TestCollision(player.PlayerTexture, r1, level3, r2);
if (loadlevel == 4)
bHit = TestCollision(player.PlayerTexture, r1, level4, r2);
if (loadlevel == 5)
bHit = TestCollision(player.PlayerTexture, r1, level5, r2);
}
else
{
bHit = 0;
}
if (bHit == 1 || bHit == 2)
{
if (player.angle == (float)Math.PI)
player.Position.Y -= playerMoveSpeed;
if (player.angle == (float)Math.PI * 2)
player.Position.Y += playerMoveSpeed;
if (player.angle == (float)Math.PI / 2)
player.Position.X -= playerMoveSpeed;
if (player.angle == (float)Math.PI * 1.5f)
player.Position.X += playerMoveSpeed;
}
}
public static Rectangle Intersection(Rectangle r1, Rectangle r2)
{
int x1 = Math.Max(r1.Left, r2.Left);
int y1 = Math.Max(r1.Top, r2.Top);
int x2 = Math.Min(r1.Right, r2.Right);
int y2 = Math.Min(r1.Bottom, r2.Bottom);
if ((x2 >= x1) && (y2 >= y1))
{
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
return Rectangle.Empty;
}
public static Rectangle Normalize(Rectangle reference, Rectangle overlap)
{
//Räkna ut en rektangel som kan användas relativt till referensrektangeln
return new Rectangle(
overlap.X - reference.X,
overlap.Y - reference.Y,
overlap.Width,
overlap.Height);
}
public static int TestCollision(Texture2D t1, Rectangle r1, Texture2D t2, Rectangle r2)
{
//Beräkna hur många pixlar som finns i området som ska undersökas
int pixelCount = r1.Width * r1.Height;
uint[] texture1Pixels = new uint[pixelCount];
uint[] texture2Pixels = new uint[pixelCount];
//Kopiera ut pixlarna från båda områdena
t1.GetData(0, r1, texture1Pixels, 0, pixelCount);
t2.GetData(0, r2, texture2Pixels, 0, pixelCount);
//Jämför om vi har några pixlar som överlappar varandra i områdena
for (int i = 0; i < pixelCount; ++i)
{
if (((texture1Pixels[i] & 0xff000000) > 0) && (texture2Pixels[i] == 0xffC3C3C3))
{
return 1;
}
if (((texture1Pixels[i] & 0xff000000) > 0) && (texture2Pixels[i] == 0xff000000))
{
return 2;
}
if (((texture1Pixels[i] & 0xff000000) > 0) && (texture2Pixels[i] == 0xff000000))
{
return 1;
}
}
return 0;
}
private void DrawHud()
{
string timeString = "TIME: " + timeElapsed.Minutes.ToString("00") + ":" + timeElapsed.Seconds.ToString("00");
spriteBatch.Draw(hud, new Vector2(0, 0), Color.White);
spriteBatch.DrawString(font, timeString, new Vector2(15, 35), Color.White);
spriteBatch.DrawString(font, "Level " + (loadlevel + 1), new Vector2(15, 10), Color.White);
spriteBatch.DrawString(font, "" + player.mvgelever, new Vector2(739, 55), Color.White);
spriteBatch.DrawString(font, "" + player.problemelever, new Vector2(799, 55), Color.White);
spriteBatch.DrawString(font, "" + player.normalaelever, new Vector2(859, 55), Color.White);
spriteBatch.DrawString(font, "" + antwavers, new Vector2(454, 55), Color.White);
spriteBatch.DrawString(font, "" + antpolis, new Vector2(514, 55), Color.White);
spriteBatch.DrawString(font, "" + antvapen, new Vector2(574, 55), Color.White);
spriteBatch.DrawString(font, "" + antlabb, new Vector2(633, 55), Color.White);
spriteBatch.DrawString(font, "" + player.coins, new Vector2(359, 55), Color.White);
spriteBatch.DrawString(font, "" + player.nyckel, new Vector2(328, 55), Color.White);
spriteBatch.DrawString(font, "" + player.bombs, new Vector2(296, 55), Color.White);
spriteBatch.DrawString(font, "RESEARCH IN: " + researchSpan, new Vector2(15, 55), Color.White);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (gameState)
{
case GameState.TITLESCREEN:
break;
case GameState.PLAYING:
if (loadlevel == 0)
spriteBatch.Draw(level0, new Vector2(0, 0), Color.White);
if (loadlevel == 1)
spriteBatch.Draw(level1, new Vector2(0, 0), Color.White);
if (loadlevel == 2)
spriteBatch.Draw(level2, new Vector2(0, 0), Color.White);
if (loadlevel == 3)
spriteBatch.Draw(level3, new Vector2(0, 0), Color.White);
if (loadlevel == 4)
spriteBatch.Draw(level4, new Vector2(0, 0), Color.White);
if (loadlevel == 5)
spriteBatch.Draw(level5, new Vector2(0, 0), Color.White);
for (int i = 0; i < tile_position.Count; i++)
{
switch (tile_life[i])
{
case 0: spriteBatch.Draw(tile_gfx, tile_position[i], Color.White);
break;
case 1: spriteBatch.Draw(tile_gfx, tile_position[i], Color.HotPink);
break;
case 2: spriteBatch.Draw(tile_gfx, tile_position[i], Color.YellowGreen);
break;
case 3: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Purple);
break;
case 4: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Yellow);
break;
case 5: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Silver);
break;
case 6: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Gold);
break;
case 60: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Gold);
spriteBatch.Draw(player.PlayerTexture, new Vector2(tile_position[i].X + 40, tile_position[i].Y + 40), Color.White);
break;
case 7: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Orange);
break;
case 70: spriteBatch.Draw(tile_gfx, tile_position[i], Color.DarkOrange);
break;
case 8: spriteBatch.Draw(stairsDown, tile_position[i], Color.White);
break;
case 9: spriteBatch.Draw(stairsUp, tile_position[i], Color.White);
break;
}
}
DrawHud();
player.Draw(spriteBatch);
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Player class (Player.cs):
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Innovationally
{
public class Player
{
public Texture2D PlayerTexture;
public Vector2 Position;
public bool Active;
public float angle;
public int coins { get; set; }
public int bombs { get; set; }
public int normalaelever { get; set; }
public int health { get; set; }
public int damage { get; set; }
public int maximumhealth { get; set; }
public int problemelever { get; set; }
public int mvgelever { get; set; }
public int elever { get; set; }
public int nyckel { get; set; }
public int Width
{
get { return PlayerTexture.Width; }
}
public int Height
{
get { return PlayerTexture.Height; }
}
public void Initialize(Texture2D texture, Vector2 position)
{
PlayerTexture = texture;
Position = position;
Active = true;
elever = 0;
mvgelever = 0;
normalaelever = 0;
coins = 0;
bombs = 0;
health = 100;
maximumhealth = 100;
damage = 20;
nyckel = 1;
angle = (float)Math.PI * 2;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(PlayerTexture, Position, null, Color.White, angle, new Vector2(PlayerTexture.Width / 2, PlayerTexture.Height / 2), 1f, SpriteEffects.None, 0f);
}
}
}
And finally, Loot.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Innovationally
{
class Loot
{
Player player = Game1.player;
public Random myRnd = new Random();
public int primarylootnumber;
public int secondarylootnumber;
public int tertiarylootnumber;
public void RandomLoot()
{
primarylootnumber = myRnd.Next(1, 11);
switch (primarylootnumber)
{
case 1:player.coins++;
break;
case 2:player.coins += 3;
break;
case 3:player.coins += 5;
break;
case 4:player.normalaelever += 1;
break;
case 5:player.normalaelever += 2;
break;
case 6:player.health += 10;
break;
case 7:player.health += 30;
break;
case 8:player.health += 50;
break;
case 9:player.damage += 5;
break;
case 10:player.damage += 10;
break;
}
secondarylootnumber = myRnd.Next(1, 11);
switch (primarylootnumber)
{
case 1:player.maximumhealth += 10;
break;
case 2:player.coins += 10;
break;
case 3:player.damage += 15;
break;
case 4:player.mvgelever += 1;
break;
case 5:player.problemelever += 1;
break;
}
tertiarylootnumber = myRnd.Next(1, 31);
switch (primarylootnumber)
{
case 10:
player.mvgelever += 1;
player.problemelever += 1;
break;
case 20:player.coins += 50;
break;
case 30:player.maximumhealth += 30;
break;
}
}
public void RandomResearch()
{
primarylootnumber = myRnd.Next(1, 6);
switch (primarylootnumber)
{
case 1:player.maximumhealth += 30;
break;
case 2: player.damage += 20;
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
}
}
The only red flag I can see at the moment is:
t1.GetData(0, r1, texture1Pixels, 0, pixelCount);
t2.GetData(0, r2, texture2Pixels, 0, pixelCount);
These calls have a high overhead. I would recommend, if you are doing per-pixel collision detection, caching or pre-computing the pixel data rather than retrieving the data from the texture every frame.
EDIT:
here is a toy implementation (NOT TESTED!) of how you may want to retrieve the pixel data. Hopefully this will be enough to get you started.
Here are two mini functions for getting the texture data. When initializing your game, use 'PixelMaps' passing in all of your textures (e.g in an array). You will then have a dictionary you can use to perform lookups of the pixel data. You will want to use the information gleamed from the Texture and the rectangle you are testing to pick the right pixels.
public static uint[] Pixels(Texture2D texture)
{
uint[] data = new uint[texture.Width * texture.Height];
texture.GetData(data);
return data;
}
public static Dictionary<Texture2D, uint[]> PixelMaps(IEnumerable<Texture2D> textures)
{
return textures.ToDictionary(t => t, Pixels);
}
The TestCollision function may now look something like this (I have tried to keep the code as close as possible to the original).
public static int TestCollision(uint[] t1,
int t1Width,
Rectangle r,
uint[] t2)
{
for(var x = r.X; x < r.X + r.Width; x++)
for (var y = r.Y; y < r.Y + r.Height; y++)
{
var i = x + (y * t1Width);
if (((t1[i] & 0xff000000) > 0) && (t2[i] == 0xffC3C3C3))
{
return 1;
}
if (((t1[i] & 0xff000000) > 0) && (t2[i] == 0xff000000))
{
return 2;
}
if (((t1[i] & 0xff000000) > 0) && (t2[i] == 0xff000000))
{
return 1;
}
}
return 0;
}
Hopefully that is enough to get you started! Have fun :)
EDIT2:
Ok more information, again, trying to not change your code too much here we go:
introduce a new private member for the pixel maps. e.g:
Dictionary<Texture2D, uint[]> pixelMaps;
In your init function try this (btw I would also put the levels in a dictionary using an enum as a key, look it up. You should never have fields/properties like 0,1,2,3 that just screams I need some kind of list/array/assoc data structure.):
//LEVEL STATS
level0 = Content.Load<Texture2D>("level0");
level1 = Content.Load<Texture2D>("level1");
level2 = Content.Load<Texture2D>("level2");
level3 = Content.Load<Texture2D>("level3");
level4 = Content.Load<Texture2D>("level4");
level5 = Content.Load<Texture2D>("level5");
//As you can see I'm using the new PixelMaps function here...
pixelMaps = PixelMaps(new[] { level0, level1, level2, level3, level4, level5 });
When you come to test collisions you can do this:
var playerMap = pixelMaps[player.PlayerTexture];
var level0Map = pixelMaps[level0];
if (loadlevel == 0)
bHit = TestCollision(playerMap, r1, level0Map);
The best and quickest way to solve this problem (and many others while your at it) is to use a profiler. It will be able to show you what methods (and possibly lines, ANTS supports this) are taking the longest to complete. Once you narrow it down you can figure out how to fix these individual problems.
As for the code you posted, I'd have to agree with thedajaw that the collision code has alot of room for optimization. Getting the texture data everyframe is never a good idea, and should be done once at the begining of the game and stored in a variable for later use.
Overall I think the rest is fine, but see what the profiler shows up. Some minor structure issues (Could use some else ifs instead of ifs, things could be handled better,etc)
I'd recommend to read this question I asked a while back on a simlar subject.
The original code is:
partial class Game1 : Microsoft.Xna.Framework.Game
{
public enum GameState
{
StateMainMenu,
StatePlaying,
StateGameOver,
StateHelp,
}
GameState currentState = GameState.StateMainMenu;
KeyboardState kbState;
Keys[] pressedKeys;
Queue<FallingCharacter> Letters = new Queue<FallingCharacter>();
float nextLetterTime = 0.6f;
int NextSpeedup = 20; // in 20 points lets speed it up
int iSpeed = 2;
int Score = 0;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont spriteFont;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ApplyChanges();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteFont = Content.Load<SpriteFont>("GameFont");
base.LoadContent();
}
public void OnKeyPressed(Keys k)
{
if ((currentState == GameState.StateMainMenu ||
currentState == GameState.StateGameOver)
&& k == Keys.Enter)
{
currentState = GameState.StatePlaying;
nextLetterTime = 0.0f;
NextSpeedup = 20; // in 20 points lsets speed it up
iSpeed = 1;
Score = 0;
}
else if (currentState == GameState.StatePlaying)
{
string sName = Enum.GetName(typeof(Keys), k);
if (Letters.Count > 0 &&
String.Compare(Letters.Peek().character.ToString(), sName) == 0)
{
Score += 100;
Letters.Dequeue();
NextSpeedup--;
if (NextSpeedup <= 0)
{
iSpeed++;
NextSpeedup = 20;
}
}
}
if (k == Keys.Escape)
{
this.Exit();
}
}
protected override void Update(GameTime gameTime)
{
// The time since Update was called last
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
kbState = Keyboard.GetState();
Keys[] newKeys = (Keys[])kbState.GetPressedKeys().Clone();
if (pressedKeys != null)
{
foreach (Keys k in newKeys)
{
bool bFound = false;
foreach (Keys k2 in pressedKeys)
{
if (k == k2)
{
bFound = true;
break;
}
}
if (!bFound)
{
OnKeyPressed(k);
}
}
}
pressedKeys = newKeys;
if (currentState == GameState.StatePlaying)
{
foreach (FallingCharacter fc in Letters)
{
if (fc.Pos.Y + 50 > graphics.PreferredBackBufferHeight)
{
currentState = GameState.StateGameOver;
Letters.Clear();
break;
}
else
{
fc.Pos.Y += (elapsed * (float)(iSpeed * 40));
}
}
nextLetterTime -= elapsed;
if (nextLetterTime <= 0 && currentState != GameState.StateGameOver)
{
nextLetterTime = 0.75f - (iSpeed * .03f);
Random r = new Random();
Letters.Enqueue(new FallingCharacter(
r.Next(graphics.PreferredBackBufferWidth - 30), -30,
Color.LightGreen, (char)((int)'A' + r.Next(25))));
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
switch (currentState)
{
case GameState.StateMainMenu:
{
spriteBatch.DrawString(spriteFont,
"Press Enter to begin",
new Vector2(graphics.PreferredBackBufferWidth / 4,
graphics.PreferredBackBufferHeight / 2),
Color.White);
}
break;
case GameState.StatePlaying:
{
spriteBatch.DrawString(spriteFont,
"Score: " + Score.ToString(),
new Vector2(10, 10), Color.White);
foreach (FallingCharacter fc in Letters)
{
fc.Render(spriteBatch, spriteFont);
}
}
break;
case GameState.StateGameOver:
{
spriteBatch.DrawString(spriteFont,
"Score: " + Score.ToString(),
new Vector2(10, 10), Color.White);
spriteBatch.DrawString(spriteFont,
"Game Over",
new Vector2(graphics.PreferredBackBufferWidth / 3,
graphics.PreferredBackBufferHeight / 2),
Color.LightBlue);
spriteBatch.DrawString(spriteFont,
"Press Return to Continue",
new Vector2(graphics.PreferredBackBufferWidth / 6,
graphics.PreferredBackBufferHeight / 2 + 50),
Color.LightBlue);
}
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
and I need change the quque to list().
after I changed Queue Letters = new Queue();
to List Letters = new List();
Can anyone help me what need change next.
I feel really confused about this, thank you very much.
If you want to keep the same behavior of the Queue (First-In-First-Out), you must do the following changes(changes in bold):
if (Letters.Count > 0 &&
String.Compare(Letters.Peek().character.ToString(),
sName) == 0)
changes to
if (Letters.Count > 0 &&
String.Compare(Letters.First().character.ToString(),
sName) == 0)
to keep the behavior where Peek returns the first character.
Letters.Dequeue();
changes to
Letters.RemoveAt(0);
to remove the first element like Dequeue does.
Note that you could do a check to make sure that the List is not empty.
Letters.Enqueue(new FallingCharacter(
r.Next(graphics.PreferredBackBufferWidth
- 30), -30,
Color.LightGreen, (char)((int)'A' + r.Next(25))));
changes to
Letters.Add(new FallingCharacter(
r.Next(graphics.PreferredBackBufferWidth
- 30), -30,
Color.LightGreen, (char)((int)'A' + r.Next(25))));
to append the FallingCharacter to the end of the list.