public bool InProximity(GameObject aOtherObj, float proximity) { float dist; Vector2.Distance(ref Location, ref aOtherObj.Location, out dist); if (dist < (Radius + aOtherObj.Radius + proximity)) return true; return false; }
public bool CollidesWith(GameObject aOtherObj) { float dist; Vector2.Distance(ref Location, ref aOtherObj.Location, out dist); if (dist < (Radius + aOtherObj.Radius)) return true; return false; }
private void updateAllNearby() { _NearbyFoodSources.Clear(); _CollidingObject = null; GameObjectManager manager = GameObjectManager.Instance; for (int i = 0; i < manager.Count; ++i) { GameObject currObj = manager[i]; if (this != currObj) { // Can only collide with person or person flock. if (currObj.CollidesWith(this) && (currObj is Person || currObj is PersonFlock)) _CollidingObject = currObj; if (currObj is FoodSource) { FoodSource mySource = (FoodSource)currObj; if (mySource.CanEat && mySource.InProximity(this, FOOD_PROXIMITY)) _NearbyFoodSources.Push((FoodSource)currObj); } } } }
public override void Drop() { _eMyState = State.Alone; if (_CollidingObject != null) { GameObjectManager manager = GameObjectManager.Instance; if (_CollidingObject is PersonFlock) { manager.RemoveObject(this); ((PersonFlock)_CollidingObject).AddPerson(this); } else if (_CollidingObject is Person) { // Create a flock from the two people who just collided manager.RemoveObject(this); manager.RemoveObject(_CollidingObject); _eMyState = State.Limbo; ((Person)_CollidingObject)._eMyState = State.Limbo; PersonFlock flock = new PersonFlock(this.Location); flock.AddPerson(this); flock.AddPerson((Person)_CollidingObject); flock.Location = this.Location; manager.AddObject(flock); GameObjectManager.Instance.NumGroupsFormed++; if (_CollidingObject.EatingObject != null) { _CollidingObject.EatingObject.StopEating(_CollidingObject); _CollidingObject.EatingObject = null; } } _CollidingObject = null; } }
public void RemoveObject(GameObject aObject) { _RemoveObject.Add(aObject); }
public void AddObject(GameObject aObject) { _AddObject.Add(aObject); }
private void processMouseEvents(GameTime aTime) { MouseState state = Mouse.GetState(); if (lastMouseState == null) lastMouseState = state; Vector2 mouseLoc = new Vector2(state.X, state.Y); if (state.LeftButton == ButtonState.Pressed) { if (this.inTransitByUser == null) { GameObjectManager manager = GameObjectManager.Instance; for (int i = 0; i < manager.Count; ++i) { if(manager[i].RadiusCheck(ref mouseLoc, 0.0f)) { if (manager[i].CanBeHeld) { this.inTransitByUser = manager[i]; manager[i].Hold(); SoundState.Instance.PlayPickupSound(aTime); } else if(_bPrintDebugInfo) { Console.WriteLine(manager[i].GetDebugInfo()); } } } } } if (state.RightButton == ButtonState.Pressed && lastMouseState.RightButton != ButtonState.Pressed) { if (Keyboard.GetState().IsKeyDown(Keys.LeftControl)) { GameObjectManager manager = GameObjectManager.Instance; for (int i = 0; i < manager.Count; ++i) { if (manager[i].RadiusCheck(ref mouseLoc, 0.0f)) { if (manager[i] is Person) { manager[i]._Hunger += 200; } } } } } if (this.inTransitByUser != null) { int x = (int)Math.Max(this.inTransitByUser.Radius, Math.Min(mouseLoc.X, GameBoundaries.Width - this.inTransitByUser.Radius)); int y = (int)Math.Max(this.inTransitByUser.Radius, Math.Min(mouseLoc.Y, GameBoundaries.Height - this.inTransitByUser.Radius)); Vector2 loc = new Vector2(x, y); this.inTransitByUser.MoveTo(ref loc); } if (Mouse.GetState().LeftButton == ButtonState.Released) { if (this.inTransitByUser != null) { inTransitByUser.Drop(); this.inTransitByUser = null; } } lastMouseState = state; }
public override void Update(GameTime aTime) { _fCurrentRadius = 0.0f; _CurrentCenterOfMass = CalculateCenterOfMass(); Location = _CurrentCenterOfMass; for (int i = 0; i < _People.Count; ++i) { float fdistCoM = Vector2.Distance(_CurrentCenterOfMass, _People[i].Location) + _People[i].Radius; _fCurrentRadius = Math.Max(fdistCoM, _fCurrentRadius); _People[i].Update(aTime); if (_eMyState != State.Held) { float fdist; Vector2.Distance(ref _People[i].Location, ref _CurrentCenterOfMass, out fdist); if (fdist > 25.0f * _People.Count) RemovePerson(_People[i]); } } for (int i = 0; i < _ExternalForces.Count; ++i) { _ExternalForces[i].iLifeLeft -= aTime.ElapsedGameTime.Milliseconds; if (_ExternalForces[i].iLifeLeft <= 0) _ExternalForces.RemoveAt(i); } // Flocks can move on their own accord. Stop eating if we're too far away from // our food source. if (EatingObject != null && !this.InProximity(EatingObject, FOOD_PROXIMITY)) { EatingObject.StopEating(this); EatingObject = null; _eMyState = State.Normal; } updateAllNearby(); switch (_eMyState) { case State.Held: break; case State.Eating: if(!EatingObject.Eat(aTime)) { EatingObject.StopEating(this); EatingObject = null; _eMyState = State.Normal; } break; case State.Normal: if (_CollidingObject is PersonFlock) { // Add all of us into them. PersonFlock theFlock = ((PersonFlock)_CollidingObject); for (int i = 0; i < _People.Count; ++i) { RemovePerson(_People[i]); theFlock.AddPerson(_People[i]); } } _CollidingObject = null; startEatingIfPossible(); break; } for (int i = 0; i < _AddPeople.Count; ++i) AddToFlock(_AddPeople[i]); _AddPeople.Clear(); for (int i = 0; i < _RemovePeople.Count; ++i) RemoveFromFlock(_RemovePeople[i]); _RemovePeople.Clear(); if (_People.Count == 1) RemoveFromFlock(_People[0]); if (_People.Count == 0) { if (EatingObject != null) EatingObject.StopEating(this); GameObjectManager.Instance.RemoveObject(this); } }
public void StopEating(GameObject aEater) { Debug.Assert(aEater == _Eater); _Eater = null; }
public void StartEating(GameObject aEater) { _Eater = aEater; }