コード例 #1
0
        public static GameObject Instantiate(string name, Vector3 position, Quaternion rotation, Vector3 linearVelocity)
        {
            GameObject tocopy = Prefabs.GetPrefab(name);

            if (tocopy == null)
            {
                Debug.LogError("Prefab not found");
                return(null);
            }

            GameObject result = (GameObject)tocopy.Clone();

            result.transform.position = position;
            result.transform.rotation = rotation;
            //if (tocopy.transform.isStatic) result.transform.SetStatic();

            result.Awake();
            result.Start(); // because instantiated at runtime

            // If it is a dynamic rigid body it cna have some linear velocity when instantiated
            if (result.HasComponent <DynamicRigidBody>())
            {
                DynamicRigidBody dc = result.GetComponent <DynamicRigidBody>();
                dc.RigidBody.LinearVelocity = Conversion.ToJitterVector(linearVelocity) * 5;
                dc.RigidBody.AddTorque(Conversion.ToJitterVector(linearVelocity) * 5);
            }

            return(result);
        }
コード例 #2
0
        //==============================================================================================
        // create all prefabs - PUT YOUR CODE HERE
        public static void BuildPrefabs()
        {
            Debug.Assert(!builtAlready, "Calling buildprefab a second time");
            if (builtAlready)
            {
                return;
            }
            builtAlready = true;

            InitializeHardwareInstancing();
            InitializeParticleSystems();

            //-------------------VALUABLES-------------------
            //cash
            GameObject cashPrefab = new GameObject("cashPrefab", ModelType.Cash, false);

            cashPrefab.transform.Scale(2.5f);
            cashPrefab.AddComponent(new Money(100, 1, Money.Type.Cash));
            cashPrefab.AddComponent(new DynamicRigidBody(shapeType: ShapeType.Box, pureCollider: true, size: new Vector3(1.5f, 3.0f, 3.0f)));
            Prefabs.AddPrefab(cashPrefab);

            //gold
            GameObject goldPrefab = new GameObject("goldPrefab", ModelType.Gold, false);

            goldPrefab.transform.Scale(2f);
            goldPrefab.AddComponent(new Money(500, 1, Money.Type.Gold));
            goldPrefab.AddComponent(new DynamicRigidBody(shapeType: ShapeType.Box, pureCollider: true, size: new Vector3(3.0f, 3.0f, 1.5f)));
            Prefabs.AddPrefab(goldPrefab);

            //diamond
            GameObject diamondPrefab = new GameObject("diamondPrefab", ModelType.Diamond, false);

            diamondPrefab.transform.Scale(1.5f);
            diamondPrefab.AddComponent(new Money(2000, 1, Money.Type.Diamond));
            diamondPrefab.AddComponent(new DynamicRigidBody(shapeType: ShapeType.BoxUniform, pureCollider: true, size: 1.5f));
            Prefabs.AddPrefab(diamondPrefab);



            //-------------------POWERUPS-------------------
            //expand these two arrays to add new powerups with a particular name and a powerup script

            /*var colorMapping = new Dictionary<string, Color>()
             *  {
             *      { "bomb", Color.Red},
             *      { "capacity", Color.Green},
             *      { "stamina", Color.Green},
             *      { "key", Color.Yellow},
             *      { "health", Color.Green},
             *      { "shield", Color.Green},
             *      { "speed", Color.Yellow},
             *      { "trap", Color.Red},
             *      { "explodingbox", Color.Red},
             *      { "weight", Color.Red},
             *      { "magnet", Color.Purple},
             *  };*/

            for (int i = 0; i < powerupName.Length; i++)
            {
                ModelType modelType = (ModelType)Enum.Parse(typeof(ModelType), powerupName[i], true);

                GameObject powerupPrefab = new GameObject(powerupName[i] + "Prefab", modelType, false);
                powerupPrefab.transform.Scale(powerupScale);
                powerupPrefab.AddComponent(powerupcomponents[i]);
                powerupPrefab.AddComponent(new DynamicRigidBody(shapeType: ShapeType.Sphere, pureCollider: true));
                powerupPrefab.AddComponent(new PowerUpRay(powerupcomponents[i].ParticleRay));
                powerupPrefab.AddComponent(new PowerUpStar(powerupcomponents[i].ParticleStar));
                Prefabs.AddPrefab(powerupPrefab);
            }

            //-------------------ELEMENTS-------------------

            //police car
            GameObject police = new GameObject("policePrefab", ModelType.Police, false);

            police.tag = ObjectTag.Police;
            police.transform.Scale(1f);
            police.AddComponent(new Police());
            police.AddComponent(new AnimatedRigidBody(shapeType: ShapeType.Box, pureCollider: true));
            police.AddComponent(new DynamicShadow());
            police.AddComponent(new AlarmLight(FollowerType.LightRed, new Vector3(-0.2f, 0.850f, 0.035f),
                                               FollowerType.LightBlue, new Vector3(0.2f, 0.851f, 0.035f)));
            police.AddComponent(new FrontLight(FrontLight.Type.FrontAndBack, new Vector3(0.27f, 0.35f, -0.97f), new Vector3(-0.27f, 0.35f, 0.93f)));
            police.AddComponent(new AnimatedWheels(AnimatedWheels.Type.FrontOnly, 20, 3));
            Prefabs.AddPrefab(police);

            //crate
            GameObject cratePrefab = new GameObject("cratePrefab", ModelType.Crate, false);

            cratePrefab.transform.Scale(1.5f);
            cratePrefab.AddComponent(new Crate());
            cratePrefab.AddComponent(new DynamicRigidBody(shapeType: ShapeType.BoxUniform, pureCollider: false));
            cratePrefab.AddComponent(new DynamicShadow());
            Prefabs.AddPrefab(cratePrefab);

            //oil
            GameObject oilPrefab = new GameObject("oilPrefab", ModelType.Oil, false);

            oilPrefab.transform.Scale(1f);
            oilPrefab.transform.SetStatic();
            oilPrefab.AddComponent(new OilTrap());
            oilPrefab.AddComponent(new StaticRigidBody(shapeType: ShapeType.BoxUniform, pureCollider: true));
            Prefabs.AddPrefab(oilPrefab);

            //planted bomb
            GameObject plantedBombPrefab = new GameObject("plantedBombPrefab", ModelType.Bomb, false);

            plantedBombPrefab.transform.Scale(powerupScale);
            plantedBombPrefab.transform.SetStatic();
            plantedBombPrefab.AddComponent(new PlantedBomb());
            plantedBombPrefab.AddComponent(new DynamicRigidBody(shapeType: ShapeType.BoxUniform, pureCollider: true));
            Prefabs.AddPrefab(plantedBombPrefab);

            //falling weight
            GameObject fallingWeight = new GameObject("fallingWeightPrefab", ModelType.Weight, false);

            fallingWeight.transform.Scale(powerupScale);
            fallingWeight.AddComponent(new FallingWeight());
            fallingWeight.AddComponent(new DynamicRigidBody(shapeType: ShapeType.BoxUniform, pureCollider: true));
            fallingWeight.AddComponent(new DynamicShadow());
            Prefabs.AddPrefab(fallingWeight);

            //planted magnet
            GameObject plantedMagnet = new GameObject("plantedMagnetPrefab", ModelType.Magnet, false);

            plantedMagnet.transform.Scale(powerupScale);
            plantedMagnet.AddComponent(new PlantedMagnet());
            plantedMagnet.AddComponent(new DynamicRigidBody(shapeType: ShapeType.BoxUniform, pureCollider: true));
            Prefabs.AddPrefab(plantedMagnet);

            //speed boost
            GameObject speedpadPrefab = new GameObject("speedpadPrefab", ModelType.Speedpad, false);

            speedpadPrefab.transform.Scale(1f);
            speedpadPrefab.transform.SetStatic();
            speedpadPrefab.AddComponent(new SpeedPad());
            speedpadPrefab.AddComponent(new StaticRigidBody(shapeType: ShapeType.BoxUniform, pureCollider: true));
            Prefabs.AddPrefab(speedpadPrefab);


            //-------------------DYNAMIC OBJECTS-------------------
            foreach (string s in dynamicElements)
            {
                ModelType modelType = (ModelType)Enum.Parse(typeof(ModelType), s, true);

                GameObject dynamicElement = new GameObject(s, modelType, false);
                dynamicElement.AddComponent(new DynamicRigidBody());
                dynamicElement.AddComponent(new DynamicShadow());
                Prefabs.AddPrefab(dynamicElement);
            }

            // cash-stacks for the base
            GameObject cashStack = new GameObject("cashStack", ModelType.Stack, false);

            cashStack.transform.Scale(2f);
            Prefabs.AddPrefab(cashStack);


            // Tracks of the player
            GameObject tracksOil = new GameObject(ModelType.TracksOil.GetDescription(), ModelType.TracksOil, false);

            tracksOil.transform.Scale(0.1f);
            tracksOil.tag   = ObjectTag.Lighting;
            tracksOil.Alpha = 0.5f;
            tracksOil.AddComponent(new Tracks());
            Prefabs.AddPrefab(tracksOil);

            GameObject tracksSpeed = new GameObject(ModelType.TracksSpeed.GetDescription(), ModelType.TracksSpeed, false);

            tracksSpeed.transform.Scale(0.1f);
            tracksSpeed.tag   = ObjectTag.Lighting;
            tracksSpeed.Alpha = 0.1f;
            tracksSpeed.AddComponent(new Tracks());
            Prefabs.AddPrefab(tracksSpeed);


            // dynamic shadow
            GameObject dynamicShadow = new GameObject(FollowerType.DynamicShadow.GetDescription(), ModelType.Shadow, false);

            dynamicShadow.tag = ObjectTag.Lighting;
            Prefabs.AddPrefab(dynamicShadow);


            // dynamic lights
            GameObject lightPlayer = new GameObject(FollowerType.LightYellow.GetDescription(), ModelType.YellowLight, false);

            lightPlayer.tag = ObjectTag.Lighting;
            Prefabs.AddPrefab(lightPlayer);

            GameObject lightBlue = new GameObject(FollowerType.LightBlue.GetDescription(), ModelType.BlueLight, false);

            lightBlue.tag = ObjectTag.Lighting;
            Prefabs.AddPrefab(lightBlue);

            GameObject lightRed = new GameObject(FollowerType.LightRed.GetDescription(), ModelType.RedLight, false);

            lightRed.tag = ObjectTag.Lighting;
            Prefabs.AddPrefab(lightRed);


            // Wheels for the player-models
            GameObject wheelType1 = new GameObject("wheelType1", ModelType.WheelFl, false);

            Prefabs.AddPrefab(wheelType1);

            GameObject wheelType2 = new GameObject("wheelType2", ModelType.WheelBz, false);

            Prefabs.AddPrefab(wheelType2);

            GameObject wheelPolice = new GameObject("wheelPolice", ModelType.WheelPolice, false);

            Prefabs.AddPrefab(wheelPolice);
        }