61 - Weapons Finished
Good news guys, I finally finished all the work on weapon systems! Well at least for right now, plus I still need to tweak some numbers. Later on I'll add more weapons that do different things. Figure out how crew damage should be dealt with. But tomorrow I start working on AI, and once that is finished I just need to clean up anything else I need, and than the first public build will be out! So let's go over some of the things I did today. One thing I did was combine some methods from both the connector and segment class into a parent class. There was a lot of code copied between the two plus I needed that whole thing with holding onto a target type, this get's rid of all of those problems. For this new class, that I ended up just calling ShipPart, I won't bother going into the code. I have already shown a good chunk of it here, and instead I'll just list off the methods I've moved over. AddParent, LowerHP, OnMouseDown, UnTargeted, Targeted, OnMouseEnter, OnMouseOver, OnMouseExit. A few of these methods had to be overridden but for the most part I still used them, and in the class I would just call like base.LowerHP(dmg) and let the shared code do as much work as I could. So I created another new class, called ShipDisplay. This shows the name and the health of the ship. I was able to copy a good chunk of code and just tweak it slightly from CrewDisplay. The main difference for the health is every 5 I make the Health Unit have a thicker border so you can get a better count by just looking at it really quick. public void SetUpHp(int max, string name) { MaxHP = max; var canvasWidth = ((RectTransform)HealthArea.transform).rect.width; var canvasHeight = ((RectTransform)HealthArea.transform).rect.height; Health = new List(); var healthWidth = canvasWidth / MaxHP - 1; for (int i = 0; i < MaxHP; i++) { var tempGO = new GameObject(); var healthImage = tempGO.gameObject.AddComponent(); healthImage.rectTransform.sizeDelta = new Vector2(healthWidth, canvasHeight); if (i % 5 == 4) healthImage.sprite = Resources.Load(@"Sprites/CrewHealth5"); else healthImage.sprite = Resources.Load(@"Sprites/CrewHealth"); healthImage.color = Color.green; healthImage.transform.SetParent(HealthArea.transform); var pos = tempGO.transform.localPosition; pos.y = 0; pos.x = i * (healthWidth + 1) + (healthWidth / 2); tempGO.transform.localPosition = pos; var temp = ((RectTransform)tempGO.transform); temp.anchorMin = new Vector2(0, .5f); temp.anchorMax = new Vector2(0, .5f); Health.Add(healthImage); } Name.text = name; } So if you read the post about setting up crew display this will look pretty familiar. If you didn't read it you can read it here. Anyways I want to focus on the if else in the middle, that's what let's me have a thicker boarder. In programming % is an operator that let's you do some cool math stuff. So something like 10%3=1 it's basicly the remainder, 3 fits into 10 3 times then there is 1 left over. It is really useful for getting the x number out of a list you are looping over. I added some code for being able to tell what HP a connector or a segment is at. You can still mouse over it and it will show a number, but I added a way to tell without having to do that. public override void LowerHP(int dmg) { base.LowerHP(dmg); var color = sprite.color; color.a = (float)HP / MaxHP; sprite.color = color; if(HP <= 0) { Destroy(ToolTipGui); Ship.RemoveConnector(this); } } This is for the connector. Basicly I start a connector at an alpha value of 1, that means you can not see though it. As it takes more damage it goes down to 0, that means you can see all the way though it. Granted at 0 that means it has been destroyed. The thing for segments was a bit more complex. public override void LowerHP(int dmg) { base.LowerHP(dmg); if (HP <= 0) { Destroy(ToolTipGui); Ship.DisableSegment(this); } var width = HealthSprite.transform.localScale; width.x = (float)HP / MaxHP; HealthSprite.transform.localScale = width; var pos = HealthSprite.transform.localPosition; pos.x = ((HealthSprite.rectTransform.rect.width * (1-width.x))/2 ) * -1; HealthSprite.transform.localPosition = pos; } So if you noticed on the image, one of the segments has a smaller green bar. Because segments are bigger it is easier to put a health bar that is readable on it. One thing I need to do however is make it transparent so it doesn't cut off the connectors, that or move it somewhere. Maybe I'll come up with another way to do it, but for now it works. I think that's it. I'm happy to finally be done with weapons and everything that entails. Join in tomorrow when I get confused while working on AI!
1/30/2017 11:54:40 PM

Add Comment Auther