private void InitializeBackgroundImage(ref BgSectionStruct _bgSection, Transform _parent, GameObject _newRoad) { // Set parent _newRoad.transform.parent = _parent; // ----- CREATE BACKGOUND SECTION ----- // Get rigidbody Rigidbody2D rb = _newRoad.GetComponent <Rigidbody2D>(); // Get image dimensions float width = _newRoad.GetComponent <SpriteRenderer>().bounds.size.x; float height = _newRoad.GetComponent <SpriteRenderer>().bounds.size.y; // Create bg section _bgSection = new BgSectionStruct(_newRoad, rb, width, height); }
private void PlaceRoad_AlignedToAnother(BgSectionStruct _road1, BgSectionStruct _road2, bool _alignHorizontally, int _roadDirection) { // Align horizontally if (_alignHorizontally) { float distanceToMove = (_road1.width + _road2.width) / 2; float positionX = _road2.obj.transform.position.x + distanceToMove * heroDirectionX * _roadDirection; //float positionY = bgDefaultPositionY; float positionY = _road2.obj.transform.position.y; _road1.obj.transform.position = new Vector2(positionX, positionY); } // Align vertically else { // The distance to move is not calculated here, because I'm asuming same height for every background float positionX = _road2.obj.transform.position.x; float positionY = _road2.obj.transform.position.y + _road2.height * _roadDirection; _road1.obj.transform.position = new Vector2(positionX, positionY); } }
// ---------- MAIN INITIALIZATIONS ----------------------------------------------------+ #region INITIALIZE public void Initialize(float _speed, Vector2 _deathDragForce, GameObject _camera) { // ----- SPEED, FORCE & PARENTS ----- // Set game speed speed = _speed; // Set death drag force deathDragForce = _deathDragForce; // Create parent of road backgrounds bgParent = new GameObject("Roads Backgrounds"); // ----- INITIALIZE INTRO ROADS ----- // Intro road 1 GameObject intro1 = Instantiate(IntroRoad1Prefab); introRoad1 = new BgSectionStruct(); InitializeBackgroundImage(ref introRoad1, bgParent.transform, intro1); // Intro road 2 GameObject intro2 = Instantiate(IntroRoad2Prefab); introRoad2 = new BgSectionStruct(); InitializeBackgroundImage(ref introRoad2, bgParent.transform, intro2); // Intro road 3 GameObject intro3 = Instantiate(IntroRoad3Prefab); introRoad3 = new BgSectionStruct(); InitializeBackgroundImage(ref introRoad3, bgParent.transform, intro3); // Intro road 4 GameObject intro4 = Instantiate(IntroRoad4Prefab); introRoad4 = new BgSectionStruct(); InitializeBackgroundImage(ref introRoad4, bgParent.transform, intro4); // ----- INITIALIZE STANDARD ROADS ----- int totalStandardRoads = 7; // Initialize Array standardRoads = new BgSectionStruct[totalStandardRoads]; // Loop array for (int i = 0; i < standardRoads.Length; i++) { GameObject standardObj = Instantiate(standardRoadPrefab); standardRoads[i] = new BgSectionStruct(); InitializeBackgroundImage(ref standardRoads[i], bgParent.transform, standardObj); } // ----- INITIALIZE CURVED ROADS ----- // Initialize curved road 1 GameObject curved1 = Instantiate(curvedRoad1Prefab); curvedRoad1 = new BgSectionStruct(); InitializeBackgroundImage(ref curvedRoad1, bgParent.transform, curved1); // Initialize curved road 2 GameObject curved2 = Instantiate(curvedRoad2Prefab); curvedRoad2 = new BgSectionStruct(); InitializeBackgroundImage(ref curvedRoad2, bgParent.transform, curved2); // ----- INITIALIZE OTHER BACKGROUND OBJECTS ----- // Create parent of extra elements GameObject bgParent2 = new GameObject("Background Extra Elements"); // Initialize visible gameplay area visibleGameplayArea = Instantiate(visibleGameplayAreaPrefab); // Set parent visibleGameplayArea.transform.parent = bgParent2.transform; // Set position visibleGameplayArea.transform.position = Vector2.zero; // Initialize ground GameObject invisibleGround = Instantiate(invisibleGroundPrefab); // Set parent invisibleGround.transform.parent = bgParent2.transform; // Initialize landing area landingArea = Instantiate(landingAreaPrefab); // Set parent landingArea.transform.parent = bgParent2.transform; // Set position landingArea.transform.position = new Vector2(0, -8f); // ----- INITIALIZE LOOP CHECKER (ROAD BACKGROUNDS) ----- // Initialize loopCheckerRoadBgs = Instantiate(loopCheckerRoadBgsPrefab); // Set parent loopCheckerRoadBgs.transform.parent = bgParent2.transform; // Initialize collision system loopCheckerRoadBgs.GetComponent <CollisionSystem>().InitializeMapManager(this); // Deactivate? loopCheckerRoadBgs.SetActive(true); // ----- ???? ----- SetIntroRoadStartPoint_AbsoluteValue(); SetCurvedRoadStoppingPoint_AbsoluteValue(); // ----- INITIALIZE ROUTE FOLLOWERS ----- // Get "route follower" introRouteFollower = bgParent.AddComponent <IntroRouteFollower>(); // Initialize script introRouteFollower.Initialize(roadIntroRoute_TurningLeft, roadIntroRoute_TurningRight, _speed, _camera); // Get "route follower" curvedRouteFollower = bgParent.AddComponent <CurvedRouteFollower>(); // Initialize script curvedRouteFollower.Initialize(roadCurvedRoute_TurningLeft, roadCurvedRoute_TurningRight, standardRoads[0].height, _speed, _camera); // Subscribe to events curvedRouteFollower.DirectionHasChanged += RespondTo_DirectionHasChanged_Event; curvedRouteFollower.TurningAnimationIsOn += RespondTo_TurningAnimationIsOn_Event; curvedRouteFollower.TurningAnimationIsOff += RespondTo_TurningAnimationIsOff_Event; introRouteFollower.TurningAnimationIsOff += RespondTo_TurningAnimationIsOff_Event; }