private void OnEnable()
    {
        stamp           = new GameObject("Stamp");
        stamp.hideFlags = HideFlags.HideAndDontSave;
        bc = target as BlocCreator;
        if (bc.SelectedPrefab != null)
        {
            CreateNewStamp();
        }

        var scriptObject = bc.gameObject;

        editorPS       = PhysicsSceneExtensions2D.GetPhysicsScene2D(scriptObject.scene);
        scriptableProp = serializedObject.FindProperty("blocsScriptable");
        if (bc.currentBlocSelection != null)
        {
            blocYRange             = bc.currentBlocSelection.blocYRange;
            gOffset                = bc.currentBlocSelection.globalOffsetRange;
            rotOff                 = bc.currentBlocSelection.globalRotationOffsetRange;
            selectedBlocDifficulty = (int)bc.currentBlocSelection.blocDifficulty;
            blocArea               = bc.currentBlocSelection.blockArea;
        }

        GetSavedBlocsNames();
        RefreshBlocNameAndDiff();

        EditorUtility.SetDirty(bc.blocsScriptable);
    }
Example #2
0
    void Start()
    {
        //Create simulation scene
        Scene theScene = SceneManager.CreateScene(
            "PhysicsSimulation",
            new CreateSceneParameters(LocalPhysicsMode.Physics2D)
            );

        _physicsScene = theScene.GetPhysicsScene2D();

        //Create object that will be simulated
        _objectToSimulte = GameObject.Instantiate(ObjectToSimulatePrefab);
        SceneManager.MoveGameObjectToScene(_objectToSimulte, theScene);

        //Perform starting simulation
        SimulatedObjectLogic theSimulatedObjectLogic =
            _objectToSimulte.GetComponent <SimulatedObjectLogic>();

        theSimulatedObjectLogic.launch(new Vector2(200.0f, 500.0f));
        for (int theStepIndex = 0; theStepIndex < 50; ++theStepIndex)
        {
            theSimulatedObjectLogic.draw();

            theSimulatedObjectLogic.simulateStep();
            _physicsScene.Simulate(0.02f);
        }
    }
    // get a new pos_rec
    public List <Vector3> get_pred(Vector3 start_pos, Vector3 add_force)
    {
        Debug.Log("Projection Scene: " + gameObject.scene.name);
        PhysicsScene2D phys2d = gameObject.scene.GetPhysicsScene2D();

        Physics2D.autoSimulation = false;

        // init vars
        pos_rec.Clear();
        transform.position = start_pos;
        rb2.velocity       = Vector2.zero;
        rb2.AddForce(add_force);
        pos_rec.Add(transform.position);
        float time_count = 0f;
        int   time_scale = (int)(timestep / sim_step);

        // begin prediction
        while (time_count <= time_max)
        {
            // do simulation
            for (int a = 0; a < time_scale; a++)
            {
                phys2d.Simulate(sim_step);
            }
            // add to list
            time_count += timestep;
            pos_rec.Add(transform.position);
        }
        return(pos_rec);
    }
    void Start()
    {
        Physics2D.autoSimulation = false;
        dots = new GameObject[maxIterations/divider];
        float slope = (1 - (50f / 255f)) / dots.Length;
        for (int i = 0; i < dots.Length; ++i)
        {
            dots[i] = Instantiate(dotPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
            dots[i].transform.parent = gameObject.transform;
            Color temp = dots[i].GetComponent<SpriteRenderer>().color;
            temp.a = 1 - (i * slope);
            dots[i].GetComponent<SpriteRenderer>().color = temp;
            dots[i].SetActive(false);
        }
        currentScene = SceneManager.GetActiveScene();
        currentPhysicsScene = currentScene.GetPhysicsScene2D();

        CreateSceneParameters parameters = new CreateSceneParameters(LocalPhysicsMode.Physics2D);
        predictionScene = SceneManager.CreateScene("Prediction", parameters);
        predictionPhysicsScene = predictionScene.GetPhysicsScene2D();

        //lineRenderer = GetComponent<LineRenderer>();
        Color c1 = Color.white;
        Color c2 = new Color(1, 1, 1, 0);
        //lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
        //lineRenderer.startColor = c1;
        //lineRenderer.endColor = c2;




    }
Example #5
0
        // LIFE-CYCLE

        private void Awake()
        {
            inputCastMask = LayerMask.GetMask("Input");
            entityManager = World.Active.EntityManager;
            physicsScene  = PhysicsSceneExtensions2D.GetPhysicsScene2D(SceneManager.GetActiveScene());
            camera        = Camera.main;

            CreatePlaceholder();
        }
Example #6
0
        public void UpdateColliders(PhysicsScene2D physics)
        {
            var bounds = GetBounds();
            int count  = physics.OverlapCircle(bounds.position, radius, overlapBuffer, Light2dFeature.inst.colliderLayerMask);

            colliders.Clear();
            for (int i = 0; i < count; i++)
            {
                var comp = overlapBuffer[i].GetComponent <LightCollider>();
                if (comp != null)
                {
                    colliders.Add(comp);
                }
            }
        }
Example #7
0
        void Awake()
        {
            if (NetworkServer.active)
            {
                physicsScene         = gameObject.scene.GetPhysicsScene();
                simulatePhysicsScene = physicsScene.IsValid() && physicsScene != Physics.defaultPhysicsScene;

                physicsScene2D         = gameObject.scene.GetPhysicsScene2D();
                simulatePhysicsScene2D = physicsScene2D.IsValid() && physicsScene2D != Physics2D.defaultPhysicsScene;
            }
            else
            {
                enabled = false;
            }
        }
Example #8
0
        private void Start()
        {
            if (IsServer)
            {
                physicsScene         = gameObject.scene.GetPhysicsScene();
                simulatePhysicsScene = physicsScene.IsValid() && physicsScene != Physics.defaultPhysicsScene;

                physicsScene2D         = gameObject.scene.GetPhysicsScene2D();
                simulatePhysicsScene2D = physicsScene2D.IsValid() && physicsScene2D != Physics2D.defaultPhysicsScene;
            }
            else
            {
                enabled = false;
            }
        }
Example #9
0
        public void Init(int simulationBuffer, List <int> playerIDs, ClientGameInfo gameInfo)
        {
            m_localTick         = 0;
            m_reconnectTryTimer = 0;
            m_clientStateBuffer = new ClientState[CLIENT_STATE_BUFFER_SIZE];
            m_inputBuffer       = new InputFrame[CLIENT_STATE_BUFFER_SIZE];

            m_clientPhysics   = SceneManager.GetSceneByName(m_physicsScene).GetPhysicsScene2D();
            m_lastServerState = null;

            Initialized = false;

            m_simulationBuffer = simulationBuffer;

            GameInfo = gameInfo;

            List <PlayerState> playerStates = new List <PlayerState>();

            foreach (int id in playerIDs)
            {
                playerStates.Add(new PlayerState(id));
            }

            foreach (ClientStateUpdater updater in m_updaters)
            {
                updater.Init(playerStates, PlayerID.Value);
            }

            m_UDPClient.Subscribe(this);
            m_TCPClient.Subscribe(this);

            for (ushort i = 0; i < CLIENT_STATE_BUFFER_SIZE; i++)
            {
                m_clientStateBuffer[i]            = new ClientState();
                m_clientStateBuffer[i].PlayerGUID = PlayerID.Value;

                foreach (PlayerState playerState in playerStates)
                {
                    PlayerState player = new PlayerState(playerState);

                    m_clientStateBuffer[i].AddPlayer(player);
                }

                m_inputBuffer[i] = new InputFrame();
            }
            Initialized = true;
            OnInitializationDone?.Invoke();
        }
Example #10
0
    //IEnumerator AirSpin() {
    //    yield return new WaitForSeconds(airSpinningDuration);
    //    isAirSpinning = false;
    //}

    bool IsGrounded()
    {
        int groundLayer = 8;

        colliders = new Collider2D[MAX_GROUNED_COLLIDER_CHECK];
        PhysicsScene2D.OverlapCollider(GetComponent <Collider2D>(), colliders, Physics2D.DefaultRaycastLayers);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i])
            {
                if (colliders[i].gameObject.layer.Equals(groundLayer))
                {
                    isAirSpinning = false;
                    return(true);
                }
            }
        }
        return(false);
    }
Example #11
0
        protected virtual void OnPhysics2D(PhysicsScene2D physicsScene)
        {
            if (!physicsScene.IsValid())
            {
                return; // do nothing if the physics Scene is not valid.
            }
            _physicsTimer += Time.deltaTime;

            // Catch up with the game time.
            // Advance the physics simulation in portions of Time.fixedDeltaTime
            // Note that generally, we don't want to pass variable delta to Simulate as that leads to unstable results.
            while (_physicsTimer >= Time.fixedDeltaTime)
            {
                _physicsTimer -= Time.fixedDeltaTime;
                physicsScene.Simulate(Time.fixedDeltaTime);
            }

            // Here you can access the transforms state right after the simulation, if needed...
        }
Example #12
0
    public void PreparePhysicsScene()
    {
        SceneManager.SetActiveScene(simScene);
        simPhysicsScene2D = simScene.GetPhysicsScene2D();

        simPlayer = Instantiate(mainPlayer, mainPlayer.transform.position, Quaternion.identity);
        simPlayer.transform.name = "simPlayer";

        simPlayerRenderer       = simPlayer.transform.GetChild(0).GetComponent <SpriteRenderer>();
        simPlayerRenderer.color = new Color(0f, 0.31f, 0.29f);

        levelGeometry = Instantiate(levelToCopy, levelToCopy.transform.position, Quaternion.identity);
        levelGeometry.GetComponentInChildren <SpriteRenderer>().enabled = false;

        levelGeometry.transform.name = "simLevel";

        simStar = Instantiate(star, star.transform.position, Quaternion.identity);
        //simStar.transform.SetParent(levelGeometry.transform);
        simStar.transform.name = "simStar";
    }
Example #13
0
    public void PreparePhysicsScene()
    {
        // There must always be one Scene marked as the active Scene.
        SceneManager.SetActiveScene(physicsScene);

        go = GameObject.Instantiate(ballToSpawn, ballToSpawn.transform.position, Quaternion.identity);
        go.transform.name = "ReferenceBall";

        // cache physics scene references
        ballSpriteRenderer                   = go.GetComponent <SpriteRenderer>();
        referenceBallRigidbody2D             = go.GetComponent <Rigidbody2D>();
        referenceBallRigidbody2D.constraints = RigidbodyConstraints2D.None;
        activePhysicsScene2D                 = physicsScene.GetPhysicsScene2D();
        ballSpriteRenderer.color             = Color.red;
        //ballSpriteRenderer.enabled = false;

        GameObject slopes = GameObject.Instantiate(slopesToSpawn, slopesToSpawn.transform.position, Quaternion.identity);

        slopes.transform.name = "ReferenceLevel";
    }
Example #14
0
    void performSimpleFlyingInGravityTest()
    {
        //Create simulation scene
        Scene theScene = SceneManager.CreateScene(
            "PhysicsSimulation",                                  /*Scene name*/
            new CreateSceneParameters(LocalPhysicsMode.Physics2D) /*Type of physics on scene*/
            );
        PhysicsScene2D thePhysicsScene = theScene.GetPhysicsScene2D();

        //Create object that will be simulated
        GameObject theGameObject = new GameObject();
        var        theCollider   = theGameObject.AddComponent <BoxCollider2D>();
        var        theRigidBody  = theGameObject.AddComponent <Rigidbody2D>();

        //Object is created in scene that is currently active.
        // So we should move object to simulation scene using
        // SceneManager.MoveGameObjectToScene(...) or
        // set simulation scene as active using
        // SceneManager.SetActiveScene(...)
        SceneManager.MoveGameObjectToScene(theGameObject, theScene);

        //Perform simulation
        //-Give starting force
        theRigidBody.AddForce(new Vector2(200.0f, 500.0f));

        for (int theStepIndex = 0; theStepIndex < 50; ++theStepIndex)
        {
            //-Apply gravity (instead of default created physics
            //- we need to apply it manually)
            theRigidBody.AddForce(new Vector2(0.0f, -9.8f));

            //-Draw current object position in simulation
            drawSquare(theGameObject.transform.position, theCollider.size, Color.green, 10.0f);

            //-Perform scene simulation step with delta time 0.02 seconds
            thePhysicsScene.Simulate(0.02f);
        }
    }
Example #15
0
 void Start()
 {
     PhysicsScene     = gameObject.scene.GetPhysicsScene();
     PhysicsScene2D   = gameObject.scene.GetPhysicsScene2D();
     LocalPhysicsMode = Neutron.Server.LocalPhysicsMode;
 }
Example #16
0
 public PhysicsManager2D(Scene scene) : base()
 {
     _world = scene.GetPhysicsScene2D();
 }
Example #17
0
        public void Init(Dictionary <int, common.serialization.types.String> clients, int simulationBuffer)
        {
            m_tickAccumulator    = 0;
            m_masterTick         = 0;
            m_bufferedMasterTick = 0;
            m_simulationBuffer   = simulationBuffer;
            m_clientStates       = new Dictionary <int, ClientState>();
            m_clients            = new HashSet <int>(clients.Keys);

            m_connectedClients = new Dictionary <int, bool>();

            m_toRemoveCache = new List <int>();

            m_serverPhysics = UnityEngine.SceneManagement.SceneManager.GetSceneByName(m_physicsSceneName).GetPhysicsScene2D();

            m_clientInputBuffers = new Dictionary <ClientState, Dictionary <int, InputFrame> >();

            // add each player to client states
            foreach (int id in m_clients)
            {
                PlayerState player = new PlayerState();
                player.GUID.Value  = id;
                m_clientStates[id] = new ClientState(id);
                m_clientStates[id].AddPlayer(player);
                m_connectedClients.Add(id, true);
            }

            // add each player to each other client state
            foreach (ClientState baseState in m_clientStates.Values)
            {
                m_clientInputBuffers[baseState] = new Dictionary <int, common.data.InputFrame>();
                foreach (ClientState otherState in m_clientStates.Values)
                {
                    PlayerState currentPlayer = otherState.GetPlayer();
                    if (baseState.PlayerGUID != otherState.PlayerGUID)
                    {
                        baseState.AddPlayer(currentPlayer);
                    }
                }
            }

            foreach (ServerGameplayStateUpdater updater in m_updaters)
            {
                updater.Setup();
            }

            foreach (ClientState state in m_clientStates.Values)
            {
                foreach (ServerGameplayStateUpdater updater in m_updaters)
                {
                    updater.InitClient(state);
                }
            }

            foreach (ClientState state in m_clientStates.Values)
            {
                foreach (ServerGameplayStateUpdater updater in m_updaters)
                {
                    updater.InitPlayer(state.GetPlayer());
                }
            }

            m_UDPServer.Subscribe(this);
            m_TCPServer.Subscribe(this);
        }