66 - Start of crew combat
So started working on a new section today, crew combat. Basically you will board the other ship and try to take it over. I spent the first part of the stream trying to figure out how to pass data from one scene to another, I figured it out after a while. You can keep data by making something static, and you can keep gameobjects by using a DontDestroyOnLoad or something similar. So let's go into how I spawned dudes on the ship. void Start () { var combatField = Instantiate(ShipPreFab).GetComponent(); combatField.Create(3, 3); combatField.AddGuys(ShipCombatDriver.Player.DudeList, true); combatField.AddGuys(ShipCombatDriver.Player.OtherShip.DudeList, false); } This is just the start code in a driver class. The driver script sits on an object in the scene, so it is called when the scene starts up. It's jump is to keep track of the game state. While it's pretty empty now if it will very useful for saving and switching scenes. But right now, we are create a new ship. Then we are just calling some methods to set up the size and populate it. public void Create(int x, int y) { X = x; Y = y; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { var temp = Instantiate(SquareObject); var pos = temp.transform.localPosition; pos.x = i; pos.y = j; temp.transform.localPosition = pos; temp.transform.parent = this.transform; } } var collider = GetComponent(); var size = collider.size; size.x = x-.05f; size.y = y-.05f; collider.size = size; var offset = collider.offset; offset.x = .5f * (x -1); offset.y = .5f * (y -1); collider.offset = offset; } So this code, as it says, creates the ship. Well I guess it creates the different squares that make up the ship. Pretty easy, we just do a nested for loop and loop over the x and y value. Then just instantiate the squares and set them to the position dictated by the for loops. Then we adjust the collider so it's over the entire ship. public void AddGuys(List dudes, bool isPlayer) { var random = new System.Random(); var newDudes = new List(); foreach (var item in dudes) { var temp = Instantiate(DudeObject); var collider = temp.GetComponent(); var ShouldntBreak = false; while (!ShouldntBreak) { ShouldntBreak = true; var y = Y; if (isPlayer) y = 0; GetRandomPlacement(temp.transform, X, y); foreach (var dude in newDudes) { if (dude.Intersects(temp.GetComponent().bounds)) { ShouldntBreak = false; break; } } } if(!isPlayer) temp.transform.FindChild("DudeSprite").GetComponent().color = Color.red; newDudes.Add(temp.GetComponent().bounds); temp.GetComponent().init(isPlayer, item.name, item.MaxHP, item.CurrentHP, newDudes.Count); } } This code adds the dudes from the two ships in ship combat. We loop over the list of the old dudes and create new objects. Then we have a while loop for placing the guys. There should not be any overlap so I check if a new guy intersects with an old crew member. If they do overlap I just pick a new random area. Then we call the init method on all the new dudes. This just lets us copy over data we need. So it's not much but it's a start, I think I have everything planned out on what I want to do with this next game mode. Hopefully it takes less time then the last one.
2/2/2017 10:59:13 PM

Add Comment Auther