/// <summary> /// Spawns a new player from the data received from the server. /// </summary> /// <param name="reader">The reader from the server.</param> void SpawnPlayer(DarkRiftReader reader) { //Extract the positions Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 rotation = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); //Extract their ID ushort id = reader.ReadUInt16(); //If it's a player for us then spawn us our prefab and set it up if (id == client.ID) { GameObject o = Instantiate( playerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockCharacter character = o.GetComponent <BlockCharacter>(); character.PlayerID = id; character.Setup(client, blockWorld); } //If it's for another player then spawn a network player and and to the manager. else { GameObject o = Instantiate( networkPlayerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockNetworkCharacter character = o.GetComponent <BlockNetworkCharacter>(); characterManager.AddCharacter(id, character); } }
void SpawnPlayer(DarkRiftReader reader) { Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 rotation = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); uint id = reader.ReadUInt32(); if (id == client.ID) { GameObject o = Instantiate( playerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockCharacter character = o.GetComponent <BlockCharacter>(); character.PlayerID = id; character.Setup(client, blockWorld); } else { GameObject o = Instantiate( networkPlayerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockNetworkCharacter character = o.GetComponent <BlockNetworkCharacter>(); characterManager.AddCharacter(id, character); } }
public void Collide() { // tell mario what he collide with by check object's type and tell the object mario collide with it. if (Character1.Type == Sprint5Main.CharacterType.Mario) { MarioCharacter MarioCharacters = (MarioCharacter)Character1; if (Character2.Type == Sprint5Main.CharacterType.Block) { Character2.MarioCollide((Time == yTime) && (relativeVelocity.Y < 0)); } else if (Character2 is PlantEnemyCharacter || Character2 is CloudEnemyCharacter) { Character2.MarioCollide(false); } else { Character2.MarioCollide((Time == yTime) && (relativeVelocity.Y > 0)); } if (!MarioCharacters.IsDied()) { MarioCharacters.OnPipe = false; MarioCharacters.CollideWith(Character2, Time == yTime, relativeVelocity.Y > 0); } } else if (Character1.Type == Sprint5Main.CharacterType.Fireball) { Character1.Parameters.IsHidden = true; // fireball will hide no matter what it hit //only mario has response when collide with fireball if (Character2.Type == Sprint5Main.CharacterType.Enemy) { Character2.MarioCollide(true); } } else //items and enemies { if (Character1.Parameters.Velocity.X * relativeVelocity.X >= 0) { Character1.BlockCollide((Time == yTime) && (relativeVelocity.Y > 0)); } if (Character1 is BossEnemyCharacter && Character2 is BlockCharacter) { BlockCharacter block = (BlockCharacter)Character2; block.BossEnemyCollide(); } } }
public void GetFirstContactTime() { //Get mario's left up coner position and right down coner position. Vector2 marioMin = Character1.GetMinPosition; Vector2 marioMax = Character1.GetMaxPosition; //Get object's left up coner position and right down coner position. Vector2 characterMin = Character2.GetMinPosition; Vector2 characterMax = Character2.GetMaxPosition; //If both mario and the object are moving, we need to use relative velocity instead of mario's velocity. relativeVelocity = Character1.Parameters.Velocity; //Console.WriteLine("relative velocity = " + relativeVelocity); relativeVelocity.X -= Character2.Parameters.Velocity.X; relativeVelocity.Y -= Character2.Parameters.Velocity.Y; #region OverLap Axel if (relativeVelocity.X > 0) { /* * If velocity is right, then mario can collide with obejct only when its right side is * on the left of objects left side. In other words, their difference must be positive. */ xTime = (characterMin.X - marioMax.X) / relativeVelocity.X; } else if (relativeVelocity.X < 0) { /* * If moving left, then mario can collide with object only when its left side is on the right of object's right side. */ xTime = (marioMin.X - characterMax.X) / -relativeVelocity.X; } if (relativeVelocity.Y > 0) { //If moving down, then mario can collide with object only when its bottom is upon object's top. yTime = (characterMin.Y - marioMax.Y) / relativeVelocity.Y; } else if (relativeVelocity.Y < 0) { //If moving up, then mario can collide with object only when its top is upon object's bottom. yTime = (marioMin.Y - characterMax.Y) / -relativeVelocity.Y; } #endregion //Here, I leave three lines of code used to check values in the future. //Console.WriteLine("characterMin = " + characterMin + " characterMax = " + characterMax); //Console.WriteLine("Mario PositionMin: " + marioMin + " marioMax = " + marioMax); //Console.WriteLine("xTime = " + xTime + " yTime = " + yTime); //Console.WriteLine("relativeVelocity = " + relativeVelocity); #region Intersect Time if (xTime < 0 && yTime < 0) { Time = -2; // if both x, y time are negative, then mario is leaving this object } else if (yTime >= 0 && yTime >= xTime) //Mario must collide with object from top or bottom. { marioMax.X += yTime * relativeVelocity.X; marioMin.X += yTime * relativeVelocity.X; // check whether the mario still has a part that is between the left and right side of object after yTime. if ((characterMin.X <= marioMin.X && marioMin.X < characterMax.X) || (characterMin.X >= marioMin.X && marioMax.X >= characterMin.X)) { Time = yTime; } else { Time = -2; // no, the mario didn't collide } if (Character2.Type == Sprint5Main.CharacterType.Block) { BlockCharacter blockCharacter = (BlockCharacter)Character2; if (blockCharacter.BlockType == BlockType.Hidden && relativeVelocity.Y > 0) { Time = -2; } } } else if (xTime >= 0 && xTime > yTime) //Mario must collide with object from left or right. { marioMax.Y += xTime * relativeVelocity.Y; marioMin.Y += xTime * relativeVelocity.Y; // check whether the mario still has a part that is between the top and bottom of object after yTime. if ((marioMin.Y >= characterMin.Y && marioMin.Y <= characterMax.Y) || (characterMin.Y >= marioMin.Y && marioMax.Y > characterMin.Y)) { Time = xTime; } else { Time = -2;// no, the mario didn't collide } if (Character2.Type == Sprint5Main.CharacterType.Block) { BlockCharacter blockCharacter = (BlockCharacter)Character2; if (blockCharacter.BlockType == BlockType.Hidden) { Time = -2; } } } #endregion }