private void OnAllFramesReady(object sender, AllFramesReadyEventArgs eventArgs)
        {
            KinectSensor kinectSensor = sender as KinectSensor;

            if (kinectSensor == null) {
                return;
            }

            using (SkeletonFrame skeletonFrame = eventArgs.OpenSkeletonFrame()) {
                if (skeletonFrame != null) {
                    if ((this.mSkeletonBuffer == null) ||
                        (this.mSkeletonBuffer.Length != skeletonFrame.SkeletonArrayLength)) {
                        this.mSkeletonBuffer = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    }

                    skeletonFrame.CopySkeletonDataTo(this.mSkeletonBuffer);

                    int playerId = 0;

                    foreach (Skeleton skeleton in this.mSkeletonBuffer) {
                        if (skeleton.TrackingState != SkeletonTrackingState.Tracked) {
                            continue;
                        }

                        Player player = null;

                        if (this.mPlayers.ContainsKey(playerId)) {
                            player = this.mPlayers[playerId];
                        } else {
                            player = new Player(playerId);
                            player.SetPlayerBounds(this.mPlayerBounds);
                            this.mPlayers.Add(playerId, player);
                        }

                        player.TimeLastUpdated = DateTime.Now;

                        if (skeleton.Joints.Count > 0) {
                            player.IsAlive = true;
                            player.State = PlayerState.Alive;

                            // 衝突判定を行う部位
                            // 頭, 手, 足
                            player.UpdateJointPosition(skeleton.Joints, JointType.Head);
                            player.UpdateJointPosition(skeleton.Joints, JointType.HandLeft);
                            player.UpdateJointPosition(skeleton.Joints, JointType.HandRight);
                            player.UpdateJointPosition(skeleton.Joints, JointType.FootLeft);
                            player.UpdateJointPosition(skeleton.Joints, JointType.FootRight);

                            // ボーン
                            // 手, 肩
                            player.UpdateBonePosition(skeleton.Joints, JointType.HandRight, JointType.WristRight);
                            player.UpdateBonePosition(skeleton.Joints, JointType.WristRight, JointType.ElbowRight);
                            player.UpdateBonePosition(skeleton.Joints, JointType.ElbowRight, JointType.ShoulderRight);

                            player.UpdateBonePosition(skeleton.Joints, JointType.HandLeft, JointType.WristLeft);
                            player.UpdateBonePosition(skeleton.Joints, JointType.WristLeft, JointType.ElbowLeft);
                            player.UpdateBonePosition(skeleton.Joints, JointType.ElbowLeft, JointType.ShoulderLeft);

                            // 頭, 肩
                            player.UpdateBonePosition(skeleton.Joints, JointType.ShoulderCenter, JointType.Head);
                            player.UpdateBonePosition(skeleton.Joints, JointType.ShoulderLeft, JointType.ShoulderCenter);
                            player.UpdateBonePosition(skeleton.Joints, JointType.ShoulderCenter, JointType.ShoulderRight);

                            // 足
                            player.UpdateBonePosition(skeleton.Joints, JointType.HipLeft, JointType.KneeLeft);
                            player.UpdateBonePosition(skeleton.Joints, JointType.KneeLeft, JointType.AnkleLeft);
                            player.UpdateBonePosition(skeleton.Joints, JointType.AnkleLeft, JointType.FootLeft);

                            player.UpdateBonePosition(skeleton.Joints, JointType.HipRight, JointType.KneeRight);
                            player.UpdateBonePosition(skeleton.Joints, JointType.KneeRight, JointType.AnkleRight);
                            player.UpdateBonePosition(skeleton.Joints, JointType.AnkleRight, JointType.FootRight);

                            player.UpdateBonePosition(skeleton.Joints, JointType.HipLeft, JointType.HipCenter);
                            player.UpdateBonePosition(skeleton.Joints, JointType.HipCenter, JointType.HipRight);

                            // 胴体
                            player.UpdateBonePosition(skeleton.Joints, JointType.HipCenter, JointType.ShoulderCenter);
                        }
                        playerId++;
                    }
                }
            }

            using (ColorImageFrame colorImageFrame = eventArgs.OpenColorImageFrame()) {
                if (colorImageFrame != null) {
                    colorImageFrame.CopyPixelDataTo(this.mPixelBuffer);
                    this.mMainImageBuffer.Lock();
                    this.mMainImageBuffer.WritePixels(
                        new Int32Rect(0, 0, colorImageFrame.Width, colorImageFrame.Height),
                        this.mPixelBuffer, colorImageFrame.Width * 4, 0);
                    this.mMainImageBuffer.Unlock();
                }
            }
        }
Example #2
0
        public HitType CheckHits(Player player)
        {
            int playerId = player.Id;
            Dictionary<Bone, BoneData> segments = player.Segments;

            DateTime currentTime = DateTime.Now;

            foreach (var pair in segments) {
                for (int i = 0; i < this.mFallingItems.Count; i++) {
                    FallingItem fallingItem = this.mFallingItems[i];

                    if (fallingItem.mState == FallingItemState.Falling) {
                        Point hitCenterPosition = new Point(0.0, 0.0);
                        double lineHitLocation = 0.0;
                        Segment segment = pair.Value.GetEstimatedSegment(currentTime);

                        if (fallingItem.Hit(segment, ref hitCenterPosition, ref lineHitLocation)) {
                            switch (this.mGameMode) {
                                case GameMode.TwoPlayer:
                                case GameMode.OnePlayer:
                                    fallingItem.mState = FallingItemState.Dissolving;
                                    fallingItem.mDissolve = 0.0;
                                    fallingItem.mVelocityX = 0.0;
                                    fallingItem.mVelocityY = 0.0;
                                    player.Score += this.GetItemScore(fallingItem);

                                    if ((player.Score / 2000) > 0) {
                                        player.Level = player.Score / 2000;
                                    } else {
                                        player.Level = 1;
                                    }

                                    this.mFallingItems[i] = fallingItem;

                                    switch (fallingItem.mType) {
                                        case FallingItemType.Cherry:
                                        case FallingItemType.BigCherry:
                                        case FallingItemType.Spike:
                                            --player.LifeCount;

                                            if (player.LifeCount <= 0) {
                                                player.IsAlive = false;
                                                player.State = PlayerState.GameOver;
                                            }

                                            break;
                                        case FallingItemType.OneUp:
                                            ++player.LifeCount;
                                            break;
                                    }

                                    switch (fallingItem.mType) {
                                        case FallingItemType.Cherry:
                                            return HitType.HitCherry;
                                        case FallingItemType.BigCherry:
                                            return HitType.HitBigCherry;
                                        case FallingItemType.Spike:
                                            return HitType.HitSpike;
                                        case FallingItemType.Coin500Yen:
                                        case FallingItemType.Coin100Yen:
                                        case FallingItemType.Coin50Yen:
                                        case FallingItemType.Coin10Yen:
                                        case FallingItemType.Coin5Yen:
                                        case FallingItemType.Coin1Yen:
                                            return HitType.CoinGet;
                                        case FallingItemType.OneUp:
                                            return HitType.OneUp;
                                    }
                                    return HitType.None;
                            }
                        }
                    }
                }
            }
            return HitType.None;
        }
        private void CheckPlayers()
        {
            // プレイヤーの削除
            foreach (var player in this.mPlayers) {
                if (player.Value.IsAlive == false) {
                    this.mGameState |= GameState.GameOver;
                    this.mGameOverStartTime = DateTime.Now;
                    this.mLastGameOverPlayer = player.Value;
                    this.mHighScores.Add(new KeyValuePair<int, TimeSpan>(this.mLastGameOverPlayer.Score,
                        DateTime.Now.Subtract(this.mFallingItemsManager.GameStartTime)));
                    this.mPlayers.Remove(player.Value.Id);
                    break;
                }
            }

            // ゲームモードの設定
            // 有効なプレイヤー数をカウント
            int playersCount = this.mPlayers.Count(player => player.Value.IsAlive);

            if (playersCount != this.mAlivePlayersCount) {
                switch (playersCount) {
                    case 2:
                        this.mGameState |= GameState.Main;
                        this.mGameState &= ~GameState.Title;
                        this.mFallingItemsManager.SetGameMode(GameMode.TwoPlayer);
                        break;
                    case 1:
                        this.mGameState |= GameState.Main;
                        this.mGameState &= ~GameState.Title;
                        this.mFallingItemsManager.SetGameMode(GameMode.OnePlayer);
                        break;
                    case 0:
                        this.mGameState &= ~GameState.Main;
                        this.mGameState |= GameState.Title;
                        this.mFallingItemsManager.SetGameMode(GameMode.NoPlayer);
                        break;
                }
                this.mAlivePlayersCount = playersCount;
            }
        }