"Tactics" Help

BlitzMax Forums/BlitzMax Beginners Area/"Tactics" Help

Bee(Posted 2012) [#1]
Having a serious challenge with OOP at the moment. I understand Types and Functions quite well, but I've painted myself in a corner here:

My game has a 4x4 grid for 4 "players" that consist of a lot of variables and a 4x4 grid of "monsters" that have a ton of variables. I wrote a basic function that allows me to push 1-4 on the keyboard and focus my party members, but
I feel like its pretty dirty. It just mirrors their varibles as a "focused" variable as such:

If KeyHit(Key_1)
Focused.Health = Player1.Health
Focused.Attack = Player1.Attack
-add about 15 more variables worth of data here-
EndIf

If KeyHit(Key_2)
Focused.Health = Player2.Health
Focused.Attack = Player2.Attack
-add about 15 more variables worth of data here-
EndIf

So now that I've written all my functions, I'm probably going to have to do the same with up to 16 monsters, but...

I know there has to be a better way to make a type "fight" another dynamic type and then select in between them. This feels really "long way" and I can see it causing a few issues later... (also cluttering up the code vastly).
Essentially I need Player 2 to target Monster 8 and swap data. That sounds simple and I'm sure it is, but have yet to wrap my head around it. All my monsters extend from a Type with base stats as they should and they are created
semi-dynamically. I'd rather write my functions as "targtedmonsterhealth-100" instead of "if target = Monster8 then monster8health-100"... if that makes any sense.



Also I'm faced with a challenge of these grids. I really understand that a grid is an "array". My grid SHOULD be something like:

int_PlayerGrid : Int[4,4]
int_MonsterGrid : Int[4,4]

(now that I look maybe I should just write a 4x10 grid and leave the middle two rows covered up with block tiles?)

I'm struggling with the ability to use these dimensions as anything useful. The grid is a chessboard and people need to move and fight on it. Some attacks can hit more than one square and have ranges. I'm just really struggling with
targeting focus and giving the functions awareness to dimensional scope. (ie: a fireball could hit 2x2 based on its original target).

I did figure out how to kind of "emulate" this by storing the info in a varible with a psuedo position value (A1, A2, A3, A4, B1) etc, but it felt as sloppy as the above selection method and required my methods to check a lot more than I feel was nessicary.


So.. here I am. Ate up all my demo time and bought the thing :D


I was REALLY looking for some kind of game tutorial to build where I was making checkers or chess. All of these games and books I've read have you making some pretty basic concept games that are very "player" centric. I've yet to find a guide or tutorial on anything that shows you how to control a team of stuff fighting another team of stuff or anything that's a bit "i go you go", but I promise once I figure this all out , i WILL write one. Making a spaceship shooter
or breakout really isn't realitve to this project past displaying lives and score.

For now, I'd appreciate any advice people were willing to give. I'm not looking for code or handouts, just some pointers. After 10 years of music production I know when I'm at "banging head on desk-ask for help stage" is and "I could probably do this myself", and head has been on the desk for a couple days now. Thanks!

-bee


Jesse(Posted 2012) [#2]
I am having a hard time trying to understand your project and don't worry and that's just my inability to comprehend what I read. Anyway, logic coding is one of my biggest challenges and sometimes it works really well for me and other times I have a hard time trying to have objects interact with each other. I am going to try to explain to you how I do it, not necessarily the best way but the way it works for me.

I usually create all of my objects extend from a base object everything that is going to interact with each other. an example would be as you have it: baddies and player. My typical base entity looks something like this:
Type Tpoint
	Field x:Float
	Field y:Float
End Type

Type Tentity
	'position
	Field position:TPoint
	
	' movement:
	Field movement:Tmovement
	
	' all of the entity's graphics representation
	Field animation:Tanimation
	
	Field activeWeapon:Tweapon
	Field weaponsPosetion:Tweapon[]
	
	'
	Field helth:Float
	Field damage:Float
	
	Method New()
		position = New Tpoint
	End Method
	
End Type

this is common to all objects and If I have a bullet that collides with an entity I pass the object to a function that takes care of bullets colliding with an entity than I don't have to specify what type of entity it I pass to the collision function it will only check the common properties. a little example:

lets say you have a type hero and a type Dragon:

Type Thero Extends Tentity

	Function Create:Thero(image:TImage,x:Float,y:Float,spd:Float,dirx:Float,diry:Float)
		Local h:Thero = New Thero
		h.position.x = x
		h.position.y = y
		h.movement = Tlinear.Create(h.position,dirx,diry,speed)
		h.animation = Tanimation.Create(image,h.position,)
		h.activeWeapon = Tgun.Create(h.position)
		h.health = 100
		Return h
	End Function
	
	Method Update()
		movement.update()
		animation.update()
		weapon.update()	
	End Method
	
	Method Render()
		animation.Render()
		weapon.render()
	End Method
End Type
	

Type TDragon Extends Tentity
	
	Function Create:Tdragon(image:TImage,x:Float,y:Float)
		Local d:Tdragon = New Tdragon	
		d.position.x = x
		d.position.y = y
		d.movement = TSpline.create(d.position)
		d.animation = Tanimation.Create(image,h.position)
		h.activeWapon = Tclaw.Create(h.position)
		h.health = 20
		Return d
	End Function

	Method Update()
		movement.Update()
		animation.Update()
		weapon.update()
	End Method
	
	Method Render()
		animation.Render()
		weapon.Render()
	End Method

End Type


Then I want a bullet to interact with an object, all I have to do is pass the object to the bullet collision. The collision function would look something like this:
Function bulletCollided:Int(bullet:TBullet,entity:Tentity)
	Local distancex = bullet.position.x - entity.position.x
	Local distancey = bullet.position.y - entity.position.y
	If Abs(distancex) < 10 And Abs(distancey) < 10 
		entity.health :- bullet.damage
		Return True
	EndIf
	Return False
End Function



Now if you want to check if a bulled collided with the dragon or the hero you can do this:


If bulletCollided(bullet,Hero) = True
	If hero.health <= 0
		'process death
	EndIf
EndIf	
	
If bulletCollided(bullet,Dragon) = True
	If Dragon.health <= 0 
		'process death
	EndIf
EndIf


notice that the same collision function worked with the dragon and the hero.

I don't know how acquainted you are with polymorphism but that is what happening in the collision function.

if you want to be able to select an entity you don't have to copy every variable in the object to another object all you have to do is this:

Field selectedEntity:Tentity

If KeyHit(Key_1)
	selectedEntity = dragon
ElseIf KeyHit(Key_2)
	selectedEntity = hero
EndIf

the above code doesn't copy the variables just assign the object to another variable so both variables have the same object address.
this way I can assign a dragon, hero1 or hero2 to the selected entity with out a problem and the collision can still be performed:
   if bulletCollided(bullet,selectedEntity)
      if selectedEntity.health <= 0 
         'process entity's death
     endif



this doesn't tie precisely to the previous and to answer your first question:


If KeyHit(Key_1)
Focused.Health = Player1.Health
Focused.Attack = Player1.Attack
-add about 15 more variables worth of data here-
EndIf

If KeyHit(Key_2)
Focused.Health = Player2.Health
Focused.Attack = Player2.Attack
-add about 15 more variables worth of data here-
EndIf



you can do this instead:

If KeyHit(Key_1)
	Focused = Player1
	-add about 15 more variables worth of data here-
EndIf

If KeyHit(Key_2)
	Focused = Player2
	-add about 15 more variables worth of data here-
EndIf


I am not saying this is the best as there are still other ways that can make the examples more OO.

I tried to do what I could and I hope you understand this but actually this is a topic for an OOP class or at least deep research.

Last edited 2012

Last edited 2012


Bee(Posted 2012) [#3]
Thank you so much for this explanation and your time! I understand my concepts weren't very clear but you made perfect sense of what I needed help with.

I was close!.. Just needed the Entity aspect as a general target to aim stuff at.

I think I understand now and I was over thinking the "targeting" mechanism into it's own box (everything in AS2 was just it's own box). This ideal makes a lot more sense and it would actually allow me to pass data like "grid position" into and out of them easier.

The "bullet" thing is exactly what I was looking for too. My way works but I knew it could be done easier.

As far as the "grid" mechanic I got something that's going to work now based on this knowledge.. (maybe!)

Thank you ,again
-bee

Last edited 2012

Last edited 2012