Example #1
0
    public BuddyState state;     //reference to the enum above

    // Use this for initialization
    void Start()
    {
        player = GameObject.Find("First Person Controller");
        agent  = GetComponent <NavMeshAgent>();
        state  = BuddyState.FOLLOWING_PLAYER;
        anim   = GetComponent <Animator>();
    }
Example #2
0
        public async Task<IActionResult> Edit(int id, [Bind("FirstID,SecondID,Status")] BuddyState buddyState)
        {
            if (id != buddyState.FirstID)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(buddyState);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BuddyStateExists(buddyState.FirstID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["FirstID"] = new SelectList(_context.Profiles, "ProfileID", "FirstName", buddyState.FirstID);
            ViewData["SecondID"] = new SelectList(_context.Profiles, "ProfileID", "FirstName", buddyState.SecondID);
            return View(buddyState);
        }
Example #3
0
 public async Task<IActionResult> Create([Bind("FirstID,SecondID,Status")] BuddyState buddyState)
 {
     if (ModelState.IsValid)
     {
         _context.Add(buddyState);
         await _context.SaveChangesAsync();
         return RedirectToAction(nameof(Index));
     }
     ViewData["FirstID"] = new SelectList(_context.Profiles, "ProfileID", "FirstName", buddyState.FirstID);
     ViewData["SecondID"] = new SelectList(_context.Profiles, "ProfileID", "FirstName", buddyState.SecondID);
     return View(buddyState);
 }
Example #4
0
    void Update()
    {
        Debug.DrawLine(transform.position, _destination, Color.magenta);

        if (IsPlayerVisibile())
        {
            _agent.speed             = normalSpeed;
            _lastKnownPlayerPosition = _player.position;

            if (_playerInfo.IsIdle && _state != BuddyState.Lost)
            {
                if (_agent.remainingDistance < 0.1f && _lastWanderTimer <= 0)
                {
                    var target = GetWanderDestination();
                    if (IsValidWanderDestination(target))
                    {
                        _destination = target;
                        _agent.SetDestination(_destination);
                        _lastWanderTimer = Random.Range(wanderMinIntervalTime, wanderMaxIntervalTime);

                        if (_state != BuddyState.Wander)
                        {
                            _state = BuddyState.Wander;
                            OnStateChangeListener?.Invoke(_state);
                        }
                    }
                }
                _lastWanderTimer -= Time.deltaTime;
            }
            else //if (_playerInfo.IsMoving)
            {
                _lastWanderTimer = 0;
                var target = GetFollowDestination();
                if (IsValidFollowDestination(target))
                {
                    _destination = target;
                    _agent.SetDestination(_destination);

                    if (_state != BuddyState.Follow)
                    {
                        _state = BuddyState.Follow;
                        OnStateChangeListener?.Invoke(_state);
                    }
                }
            }
        }
        else
        {
            _agent.speed = lostSpeed;

            if (_state != BuddyState.Lost)
            {
                _destination = _lastKnownPlayerPosition;
                _agent.SetDestination(_destination);
                _state = BuddyState.Lost;
                OnStateChangeListener?.Invoke(_state);
            }
            else if (_agent.remainingDistance < followingDistance * 0.5f)
            {
                _destination = _player.position;
                _agent.SetDestination(_destination);
            }
        }
    }
Example #5
0
        public static void Initialize(MatchContext context)
        {
            context.Database.EnsureCreated();

            // Look for any users.
            //If Any users, only create users for first run (seed AspNetUusers)
            if (context.Users.Any())
            {
                //If users exists, check if profiles also exist, if they do then data is already seeded.
                if (context.Profiles.Any())
                {
                    return;
                }

                var users = new Profile[]
                {
                    // UserID = 1
                    new Profile {
                        ProfileID = 1, FirstName = "John", LastName = "Smith", CreationDate = DateTime.Parse("2019-09-24"), UserID = 1
                    },
                    new Profile {
                        ProfileID = 2, FirstName = "Susan", LastName = "Meyers", CreationDate = DateTime.Parse("2019-05-07"), UserID = 2
                    },
                    new Profile {
                        ProfileID = 3, FirstName = "Michael", LastName = "Boylan", CreationDate = DateTime.Parse("2019-06-17"), UserID = 3
                    },
                    new Profile {
                        ProfileID = 4, FirstName = "Jeff", LastName = "Mililer", CreationDate = DateTime.Parse("2020-01-12"), UserID = 4
                    },
                    new Profile {
                        ProfileID = 5, FirstName = "Karen", LastName = "Filippelli", CreationDate = DateTime.Parse("2018-03-23"), UserID = 5
                    },
                    // UserID = 5
                    new Profile {
                        ProfileID = 6, FirstName = "Jim", LastName = "Halpert", CreationDate = DateTime.Parse("2019-07-05"), UserID = 6
                    },
                    new Profile {
                        ProfileID = 7, FirstName = "Dwight", LastName = "Schrute", CreationDate = DateTime.Parse("2020-03-09"), UserID = 7
                    },
                    new Profile {
                        ProfileID = 8, FirstName = "Pam", LastName = "Beesly", CreationDate = DateTime.Parse("2020-02-15"), UserID = 8
                    },
                    new Profile {
                        ProfileID = 9, FirstName = "Kelly", LastName = "Kapoor", CreationDate = DateTime.Parse("2019-04-09"), UserID = 9
                    },
                    new Profile {
                        ProfileID = 10, FirstName = "Ryan", LastName = "Howard", CreationDate = DateTime.Parse("2019-11-25"), UserID = 10
                    }
                };
                using (var profileTransaction = context.Database.BeginTransaction())
                {
                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Profiles ON");
                    foreach (Profile u in users)
                    {
                        context.Profiles.Add(u);
                    }
                    context.SaveChanges();
                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Profiles OFF");
                    profileTransaction.Commit();
                }

                var racerecords = new RaceRecord[]
                {
                    //RaceRecordID = 1
                    new RaceRecord {
                        ProfileID = 1, RaceType = 1, RaceTime = 450, MileTime = ToMile(450, 1)
                    },
                    new RaceRecord {
                        ProfileID = 2, RaceType = 2, RaceTime = 1300, MileTime = ToMile(1300, 2)
                    },
                    new RaceRecord {
                        ProfileID = 3, RaceType = 4, RaceTime = 5875, MileTime = ToMile(5875, 4)
                    },
                    new RaceRecord {
                        ProfileID = 4, RaceType = 3, RaceTime = 3100, MileTime = ToMile(3100, 3)
                    },
                    new RaceRecord {
                        ProfileID = 5, RaceType = 2, RaceTime = 1400, MileTime = ToMile(1400, 2)
                    },
                    //RaceRecordID = 5
                    new RaceRecord {
                        ProfileID = 6, RaceType = 5, RaceTime = 12500, MileTime = ToMile(12500, 5)
                    },
                    new RaceRecord {
                        ProfileID = 7, RaceType = 5, RaceTime = 11775, MileTime = ToMile(11775, 5)
                    },
                    new RaceRecord {
                        ProfileID = 8, RaceType = 4, RaceTime = 5550, MileTime = ToMile(5550, 4)
                    },
                    new RaceRecord {
                        ProfileID = 9, RaceType = 3, RaceTime = 2735, MileTime = ToMile(2735, 3)
                    },
                    new RaceRecord {
                        ProfileID = 10, RaceType = 2, RaceTime = 1500, MileTime = ToMile(1500, 2)
                    },
                    //RaceRecordID = 10
                    new RaceRecord {
                        ProfileID = 2, RaceType = 5, RaceTime = 12750, MileTime = ToMile(12750, 5)
                    },
                    new RaceRecord {
                        ProfileID = 3, RaceType = 5, RaceTime = 11875, MileTime = ToMile(11875, 5)
                    },
                    new RaceRecord {
                        ProfileID = 7, RaceType = 4, RaceTime = 5600, MileTime = ToMile(5600, 4)
                    },
                    new RaceRecord {
                        ProfileID = 1, RaceType = 3, RaceTime = 2850, MileTime = ToMile(2850, 3)
                    },
                    new RaceRecord {
                        ProfileID = 5, RaceType = 2, RaceTime = 1600, MileTime = ToMile(1600, 2)
                    },
                    //RaceRecordID = 15
                    new RaceRecord {
                        ProfileID = 2, RaceType = 5, RaceTime = 13000, MileTime = ToMile(13000, 5)
                    },
                    new RaceRecord {
                        ProfileID = 6, RaceType = 5, RaceTime = 12300, MileTime = ToMile(12300, 5)
                    },
                    new RaceRecord {
                        ProfileID = 4, RaceType = 4, RaceTime = 5300, MileTime = ToMile(5300, 4)
                    },
                    new RaceRecord {
                        ProfileID = 8, RaceType = 3, RaceTime = 2800, MileTime = ToMile(2800, 3)
                    },
                    new RaceRecord {
                        ProfileID = 3, RaceType = 2, RaceTime = 1650, MileTime = ToMile(1650, 2)
                    }
                };
                foreach (RaceRecord r in racerecords)
                {
                    context.RaceRecords.Add(r);
                }
                context.SaveChanges();


                var buddies = new BuddyState[]
                {
                    // 1 = Matched, 2 = Requested, 3 = Blocked

                    new BuddyState {
                        FirstID = 1, SecondID = 8, Status = 1
                    },
                    new BuddyState {
                        FirstID = 1, SecondID = 4, Status = 3
                    },
                    new BuddyState {
                        FirstID = 2, SecondID = 5, Status = 2
                    },
                    new BuddyState {
                        FirstID = 3, SecondID = 10, Status = 1
                    },
                    new BuddyState {
                        FirstID = 3, SecondID = 5, Status = 2
                    }
                };

                foreach (BuddyState b in buddies)
                {
                    context.BuddyList.Add(b);
                }
                context.SaveChanges();



                var messages = new Message[]
                {
                    new Message
                    {
                        MessageID   = 8,
                        DateSent    = DateTime.Parse("2019-10-26"),
                        MsgSenderID = 2,
                        MsgHeader   = "Hi",
                        MsgBody     = "Hey, I saw you atteneded the same race in the same area the other day.  You live around there?  Train regularly?"
                    },
                    new Message
                    {
                        MessageID   = 7,
                        DateSent    = DateTime.Parse("2019-10-23"),
                        MsgSenderID = 7,
                        MsgHeader   = "All in",
                        MsgBody     = "So what do I do here?  I'm not looking to put anything less than 100% into my training."
                    },
                    new Message
                    {
                        MessageID   = 6,
                        DateSent    = DateTime.Parse("2019-10-17"),
                        MsgSenderID = 3,
                        MsgHeader   = "Short Time",
                        MsgBody     = "Yes, absolutely, we meet at 6 AM over in this area Mondays, Wednesdays and Saturdays if that works."
                    },
                    new Message
                    {
                        MessageID   = 5,
                        DateSent    = DateTime.Parse("2019-10-13"),
                        MsgSenderID = 10,
                        MsgHeader   = "Short Time",
                        MsgBody     = "Hi, I'm in the area for work for a month or two and will be in the same area.  Would you like to pair up for some runs?"
                    },
                    new Message
                    {
                        MessageID   = 4,
                        DateSent    = DateTime.Parse("2019-10-11"),
                        MsgSenderID = 5,
                        MsgHeader   = "Really?!",
                        MsgBody     = "We met at the race the other day and I saw your finish time, it was amazing.  How often do you train?"
                    },
                    new Message
                    {
                        MessageID   = 3,
                        DateSent    = DateTime.Parse("2019-10-04"),
                        MsgSenderID = 8,
                        MsgHeader   = "Hello",
                        MsgBody     = "Awesome!  I've been trying for a while to find someone, when's a good time to meet up?"
                    },
                    new Message
                    {
                        MessageID   = 2,
                        DateSent    = DateTime.Parse("2019-09-27"),
                        MsgSenderID = 1,
                        MsgHeader   = "Hello",
                        MsgBody     = "Yes, I'm already running with 2 others but we would love to have more.  We usually go in the morning but lately we've been having trouble synching up.  If you're up for it, we're trying in the early evening right after we all get off from work."
                    },
                    new Message
                    {
                        MessageID   = 1,
                        DateSent    = DateTime.Parse("2019-09-24"),
                        MsgSenderID = 8,
                        MsgHeader   = "Hello",
                        MsgBody     = "Hey, I see you're in the same area.  Are you already running with a group or would you like to start one?"
                    },
                };
                using (var messageTransaction = context.Database.BeginTransaction())
                {
                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Messages ON");
                    foreach (Message m in messages)
                    {
                        context.Messages.Add(m);
                    }
                    context.SaveChanges();
                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Messages OFF");
                    messageTransaction.Commit();
                }


                var threads = new Thread[]
                {
                    new Thread {
                        ThreadID = 1, InitiatorID = 8, ReceiverID = 1
                    },
                    new Thread {
                        ThreadID = 2, InitiatorID = 5, ReceiverID = 3
                    },
                    new Thread {
                        ThreadID = 3, InitiatorID = 3, ReceiverID = 10
                    },
                    new Thread {
                        ThreadID = 4, InitiatorID = 7, ReceiverID = 4
                    },
                    new Thread {
                        ThreadID = 5, InitiatorID = 2, ReceiverID = 9
                    }
                };

                using (var threadTransaction = context.Database.BeginTransaction())
                {
                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Threads ON");
                    foreach (Thread t in threads)
                    {
                        context.Threads.Add(t);
                    }
                    context.SaveChanges();
                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Threads OFF");
                    threadTransaction.Commit();
                }



                var conversations = new Conversation[]
                {
                    new Conversation {
                        ThreadID = 1, MessageID = 1, ReadFlag = 1, DateRead = null
                    },
                    new Conversation {
                        ThreadID = 1, MessageID = 2, ReadFlag = 1, DateRead = null
                    },
                    new Conversation {
                        ThreadID = 1, MessageID = 3, ReadFlag = 1, DateRead = null
                    },
                    new Conversation {
                        ThreadID = 2, MessageID = 4, ReadFlag = 0, DateRead = null
                    },
                    new Conversation {
                        ThreadID = 3, MessageID = 5, ReadFlag = 1, DateRead = null
                    },
                    new Conversation {
                        ThreadID = 3, MessageID = 6, ReadFlag = 0, DateRead = null
                    },
                    new Conversation {
                        ThreadID = 4, MessageID = 7, ReadFlag = 0, DateRead = null
                    },
                    new Conversation {
                        ThreadID = 5, MessageID = 8, ReadFlag = 0, DateRead = null
                    }
                };
                foreach (Conversation c in conversations)
                {
                    context.Conversations.Add(c);
                }
                context.SaveChanges();
            }   // DB has been seeded
            return;
        }
Example #6
0
    // Update is called once per frame
    void Update()
    {
        //At all times I keep a reference to the distance from the player.
        distanceToPlayer = Vector3.Distance(transform.position, player.transform.position);

        /*
         * Here I search for all enemies in the level. I then draw a raycast to each enemy. This way, I now have a reference
         * to every enemies position in relation to the dinosaur. I can now always record the distance to each enemy, and
         * whether or not the raycast is hitting them.
         */
        foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("enemy"))
        {
            Vector3 lineOfSightToEnemy   = enemy.transform.position - transform.position;
            Vector3 raycastStartPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
            distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);

            if (Physics.Raycast(raycastStartPosition, lineOfSightToEnemy, out hit))
            {
                /*
                 * Here I check to see if the raycast has hit an enemy, and if it has, I then check the distance to
                 * that enemy. If the enemy is in range, then the dinosaur goes and attacks.
                 */
                if (hit.collider.gameObject.tag == "enemy")
                {
                    if (distanceToEnemy < 10)
                    {
                        currentEnemy = enemy;
                        state        = BuddyState.ATTACKING_ENEMY;
                    }
                }

                /*
                 * I draw this ray to each enemy for debugging purposes
                 */
                Debug.DrawRay(raycastStartPosition, lineOfSightToEnemy, Color.red);
            }
        }

        if (attackingAnEnemy)
        {
            anim.SetBool("AttackEnemy", true);
        }

        if (!attackingAnEnemy)
        {
            anim.SetBool("AttackEnemy", false);
        }

        /*
         * This state machine controls the dinosaurs states.
         */
        switch (state)
        {
        /*
         * If the dinosaur is IDLE, I check the distance to the player. If the player is more than 4 metres away,
         * the dino will change state and follow the player. If the dino is less than 4 metres from the player, then
         * he will stand idle. To give the dinosaue some personality, I make it play an animation if the player
         * stands still for too long
         */
        case BuddyState.IDLE:
            anim.SetFloat("distanceToPlayer", distanceToPlayer);             //Here I pass the distance to the player to the animator
            attackingAnEnemy = false;

            idleTimer += Time.deltaTime;

            if (idleTimer >= 10.0f)
            {
                anim.SetTrigger("complain");
                idleTimer = 0.0f;
            }

            if (distanceToPlayer > 4.0f)
            {
                state = BuddyState.FOLLOWING_PLAYER;
            }

            if (distanceToPlayer <= 4.0f && state != BuddyState.ATTACKING_ENEMY)
            {
                state = BuddyState.IDLE;
            }

            break;

        /*
         * When FOLLOWING_PLAYER, the dinosaur plays the run animation, and its target position is set to the players
         * position. To stop the dinosaur actually standing directly on the player, I applied a dampner to the
         * NAVMESH component, so it actually stops slighty away from the player.
         */
        case BuddyState.FOLLOWING_PLAYER:
            anim.SetFloat("distanceToPlayer", distanceToPlayer);
            attackingAnEnemy = false;

            if (distanceToPlayer <= 4)
            {
                state = BuddyState.IDLE;
            }

            agent.SetDestination(player.transform.position);
            break;

        /*
         * When ATTACKING_ENEMY, the dino sets its target position to be the position of the enemy. Once it is
         * in range of the dinosaur, this triggers the "attackingEnemy" animation, and I send damge to the enemy
         * via the SendMessage feature. When the enemy is destroyed, it will return NULL, and I then set the state
         * of the dinosaur to IDLE. Once it goes to IDLE, it will straight away check whether it is near the player or
         * not, in which case it will either run after the player, or stand beside the player if the player is in
         * range.
         */
        case BuddyState.ATTACKING_ENEMY:

            if (currentEnemy != null)
            {
                agent.SetDestination(currentEnemy.transform.position);

                if (Vector3.Distance(transform.position, currentEnemy.transform.position) <= 4)
                {
                    if (currentEnemy == null || currentEnemy.Equals(null))
                    {
                        //state = BuddyState.IDLE;
                        //	Debug.Log("null enemy");
                    }
                    else
                    {
                        agent.SetDestination(currentEnemy.transform.position);
                        transform.LookAt(currentEnemy.transform.position);
                        currentEnemy.gameObject.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
                        //anim.SetTrigger("attackingEnemy");
                        attackingAnEnemy = true;
                    }
                }
                else
                {
                    state = BuddyState.IDLE;
                }
            }
            else
            {
                state = BuddyState.IDLE;
            }

            break;

        /*
         * If buddy gets killed, I stop him moving, and then play the death animation. When this state is triggered,
         * it calls the DIE() function below
         */
        case BuddyState.DEAD:
            agent.SetDestination(transform.position);
            collider.enabled = false;
            anim.SetTrigger("Die");
            StartCoroutine("Die");
            break;
        }
    }
    private void Update()
    {
        Debug.DrawLine(transform.position, _agent.destination, Color.magenta);

        if (_state == BuddyState.Healing)
        {
            if (!_playerInfo.IsCriticalHealth)
            {
                _state = BuddyState.Hiding;
                OnStateChangeListener?.Invoke(_state);
            }
            else
            {
                if ((_player.position - _lastTargetPlayerPosition).sqrMagnitude > 0.1f)
                {
                    _lastTargetPlayerPosition = _player.position;
                    _agent.SetDestination(_lastTargetPlayerPosition);
                }

                var distSq = (_player.position - transform.position).sqrMagnitude;
                if (distSq < minDistanceToPlayer * minDistanceToPlayer)
                {
                    var playerHealth = _player.GetComponent <HealthBehavior>();
                    playerHealth.health += healAmount;

                    FindNewPosition();
                }
            }
        }
        else if (_state == BuddyState.Attacking)
        {
            if (_playerInfo.IsCriticalHealth)
            {
                _state = BuddyState.Healing;
                OnStateChangeListener?.Invoke(_state);
                _lastTargetPlayerPosition = _player.position;
                _agent.SetDestination(_lastTargetPlayerPosition);
                _agent.updateRotation = true;
            }
            else if (_targetEnemy != null && IsVisibile(_targetEnemy.position))
            {
                var desiredRot = Quaternion.LookRotation(_targetEnemy.position - transform.position);
                var angle      = Quaternion.Angle(transform.rotation, desiredRot);
                transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRot, _agent.angularSpeed * Time.deltaTime);

                if (angle < 5.0f)
                {
                    _gun.Fire();
                }

                if (_agent.remainingDistance < 0.2f)
                {
                    FindNewPosition(false);
                }
            }
            else
            {
                _state = BuddyState.Hiding;
                OnStateChangeListener?.Invoke(_state);
                _agent.updateRotation = true;
            }
        }
        else if (_state == BuddyState.Hiding)
        {
            if (_playerInfo.IsCriticalHealth)
            {
                _state = BuddyState.Healing;
                OnStateChangeListener?.Invoke(_state);
                _lastTargetPlayerPosition = _player.position;
                _agent.SetDestination(_lastTargetPlayerPosition);
            }
            else
            {
                var visibleEnemies = GetVisibileEnemies();
                if (visibleEnemies.Count > 0)
                {
                    _state = BuddyState.Attacking;
                    OnStateChangeListener?.Invoke(_state);
                    _targetEnemy          = FindEnemyToAttack(visibleEnemies).transform;
                    _agent.updateRotation = false;
                }

                if (!IsWithinProximityOfPlayer(transform.position))
                {
                    _lastTargetPlayerPosition = _player.position;
                    FindNewPosition();
                }
            }
        }
    }