52 - Weapon Targeting
Alright, I tried to get some stuff with weapon systems going today, and I was partly successful. While I got to it after a while, if you watched the stream or saw the VODs I spent two hours doing other stuff. It needed to be done at some point, so why not now while I notice it. So how about we jump right into some code for moving around the UI elements for when one is removed? public void ReorderWeaponUI() { var systems = new List(); for (int i = 0; i < SegmentSize; i++) { for (int j = 0; j < SegmentSize; j++) { var temp = Segments[i, j]; if(temp.isActiveAndEnabled) { if(temp.System != null) { systems.Add(temp.System); } } } } systems = systems.OrderBy(x => x.PosOfSystem).ToList(); for (int i = 0; i < systems.Count; i++) { if(systems[i].PosOfSystem != i +1) systems[i].UpdateUI(IsPlayer, i + 1); } } So the first big chunk is just for looping through all the segments to find out what systems need to be removed. There could be a better way of doing this, such as when a segment is removed if it has a system on it, it will call updateUI there, but whatever, this works nicely. After we get everything we just need to loop through and update the UI. I'm not going to show the UpdateUI code for either this or the crew display, basically I'm just grabbing the position and moving it based on how many position chances there have been. Now, let's get into some of the weapon stuff I did. Let's sum up what I did before showing any code. I made it so you could click on the weapon UI to select them, or you can hit the number key to do the same thing. Then there's code that once it's selected, you can't select units till you right click or target something. Once you target something it will change the color of the segment. Finally if you target something then select the same weapon and right click, it won't be targted anymore. private void WeaponSelected() { if (IsPlayer) { Ship.WeaponSelecting = true; Ship.Targeting = this; } } This sits in the Weapons class, it doesn't seem like much, and that's whats cool about it. All I need to do is set a boolean and pass what weapon is currently targeting. void OnMouseDown() { if (transform.tag == "Player") //DO STUFF else { if (Ship.OtherShip.WeaponSelecting) { var weapon = Ship.OtherShip.Targeting; if (weapon.Target != null) { weapon.Target.ChangeColor(Color.red); } weapon.Target = this; if (!Ship.OtherShip.Targetings.Contains(weapon)) { Ship.OtherShip.Targetings.Add(weapon); } ChangeColor(Color.black); Ship.OtherShip.Targeting = null; Ship.OtherShip.WeaponSelecting = false; } } } Alright this is running on the segment, more specifically, on the enemy ship. Also, both ships have access to each other. While this kinda breaks some coding rules, I don't really care that much. It might become a pain letter and I'll move things over to a method that makes more sense. Anyways, we figure out what's targeting the segment and add it to a list so we can keep track of what everything is targeting. Then we just change the color and set the ship state back to what it was so you can go back to selecting things. void Update() { if (Input.GetMouseButtonDown(1)) { CrewSelecting = false; Selected = false; if (WeaponSelecting) { if (Targetings.Contains(Targeting)) { Targetings.Remove(Targeting); } if (Targetings.Where(x => x.Target == Targeting.Target).Count() == 0) Targeting.Target.ChangeColor(Color.red); Targeting.Target = null; } Targeting = null; WeaponSelecting = false; DudeList.ForEach(x => x.DeSelect()); } } This runs on the player ship, also the Update method does other things, but no reason to show them here. Really all we are doing here is that if you right click while targeting you can use a different weapon, or select units. Also we need to make sure if something else is targeting that segement or not. If there is something targeting it then keep it targeted. Now this code will change, because I will most likely slap an image on the segment when you target it. But till then this is something nice. Well I think that pretty much covers what I did today. Obviously there's more to it, like always. I've said this before, but writing these blogs helps me out too. I spotted a few things that I should change. Mainly cleaning up some things into methods so they aren't so crazy. Like I said yesterday, let me know if you want me to go into more or less detail on things.
1/25/2017 8:50:20 PM

Add Comment Auther