ПроЭктировщик
Регистрация: 17.07.2009
Сообщений: 182
Написано 51 полезных сообщений (для 71 пользователей)
|
Ответ: (ИИ)RTS... передвижение юнитов по клику
Итак продолжаю ... Передвижение юнитов по клику.
Код подтомаживает ,но вполне рабочий.
Код:
Const wait =0 ,move = 1 ,attack =2
Dim formX(200) : Dim formY(200)
Global formationX ,formationY
Global selectionFlag =0 , groupFlag = 0 , TargetFlag = 0
Global mx#,my#,oldx# ,oldy# ,group ,sel
Global bx0, by0, bx1, by1 , middleX# , middleY# ,tx#,ty#
Type Unit
Field x# ,tx# ,vx# , vx2#, angl# , distance#
Field y# ,ty# ,vy# , vy2# ,speed# , size
Field name$
Field group
Field isSelected =0
Field action =wait ; wait =0 move = 1 attack =2
End Type
Function Dis(X1,Y1, xx,yy)
Return Abs(((xx-X1)*(xx-X1)+(yy-Y1)*(yy-Y1))^0.5)
End Function
Function NewUnit.Unit(name$ , x#, y# )
this.Unit = New unit
this\x# = x#
this\y# = y#
this\name$ = name$
this\group = -1
this\speed = 5
this\size = 10
End Function
Function SelectionMove()
If MouseDown(2)=True And TargetFlag = 0 Then
tx# = MouseX()
ty# = MouseY()
TargetFlag = 1
EndIf
If TargetFlag = 1
For Un.Unit = Each Unit
If un\isSelected = 1
;un\tx# =tx
;un\ty# = ty
EndIf
Next
j1 = 0
For Un.Unit = Each Unit
If un\isSelected = 1
j1 = j1+1
MathMove2(un,j1)
d2# = dis( un\x, un\y,un\tx,un\ty)
If d2<>0
un\vx# = (un\tx -un\x)/d2
un\vy# = (un\ty - un\y )/d2
EndIf
un\action = move
EndIf
Next
EndIf
If MouseDown(2)=False And TargetFlag = 1 Then
TargetFlag = 0
EndIf
End Function
Function MathMove2(this.Unit, j)
blockFormation(j)
ss = this\size *2
this\tx= tx+formationX * ss
this\ty = ty+formationY * ss
End Function
Function MoveUnit(this.Unit)
this\x = this\x + this\vx*this\speed
this\y = this\y + this\vy*this\speed
Color 120,120,120
; Line this\x ,this\y, this\tx ,this\ty
; ss# = (this\size+this\size)*2
Oval this\x-ss/2 ,this\y-ss/2 ,ss,ss,0
End Function
Graphics 800,600,32,2
SetBuffer BackBuffer()
cyr = LoadFont("ArialCyr",14)
SetFont cyr
For d=0 To 15
NewUnit("sold" , Rnd(800),Rnd(400))
Next
While Not KeyHit(1)
Cls
MiddleSelect()
SelectionMove()
For Un.Unit = Each Unit
Selection_Mouse(un)
If un\isSelected = 1
Color 120,120,120
ss# = (Un\size+Un\size)
Oval Un\tx-ss/2 ,Un\ty-ss/2 ,ss,ss,0
Line un\x ,un\y, un\tx ,un\ty
EndIf
If un\action = move Then
If Dis(un\x ,un\y , un\tx , un\ty) <= un\speed+0.1 Then un\action = wait
MoveUnit(un)
EndIf
;Text un\x#+12,un\y#+12, un\tx +" "+ un\ty +" "+ un\x +" "+ un\y
Next
DrawUnit()
Flip
Wend
Delete Each Unit
FreeFont cyr
End
Function Selection_Mouse(this.Unit)
If MouseDown(1) = True And selectionFlag = 0 Then
selectionFlag = 1
mx = MouseX()
my = MouseY()
EndIf
If MouseDown(1) = True And selectionFlag = 1 Then
this\isSelected = 0
oldx= MouseX()
oldy= MouseY()
EndIf
If MouseDown(1) = False Then selectionFlag = 0
If MouseHit (1) And RectsOverlap(mx#,my#,1,1,500,400,40,40) Then NewUnit("sold" , Rnd(600),Rnd(400))
If selectionFlag = 1 Then
If mx < oldx
bx0 = mx
bx1 = oldx
Else
bx0 = oldx
bx1 =mx
EndIf
If my < oldy
by0 = my
by1 = oldy
Else
by0 = oldy
by1 = my
EndIf
EndIf
If this\x# > bx0 And this\x# < bx1 And this\y# > by0 And this\y# < by1
this\isSelected = 1
ElseIf Dis(mx,my, this\x#,this\y#)<this\size
this\isSelected = 1
EndIf
End Function
Function DrawUnit()
For Un.Unit = Each Unit
If un\isSelected = 1
Text un\x#,un\y#-18,"S"
Color 2,200,200
Oval middleX#-3 , middleY# -3 , 6 ,6
Color 255,255,255
If un\group >-1
Text un\x#-4,un\y#+8, un\group
EndIf
EndIf
; If un\action = move Then Text un\x#+4,un\y#+8,"M"
; If un\action = wait Then Text un\x#+4,un\y#+8,"W"
Rect un\x#-5,un\y#-5,10,10,0 ; Unit
Color 2,200,200
If selectionFlag = 1 Then Rect bx0, by0, bx1-bx0, by1-by0,0
Color 255,255,255
; -- button --
Rect 500,400,40,40,0
; -- info --
Text 20,20,"mx = "+mx+" my = "+my+" oldx = "+oldx+" oldy = "+oldy+" group "+group
Text 20,40,"Selected : "+ sel
Next
End Function
Function MiddleSelect()
lowX# = 999999
lowY# = 999999
hightX# = 0
hightY# = 0
sel =0
For Un.Unit = Each Unit
If un\isSelected = 1
sel = sel +1
If un\x <= lowX Then lowX = un\x
If un\y <= lowY Then lowY = un\y
If un\x > hightX Then hightX = un\x
If un\y > hightY Then hightY = un\y
EndIf
Next
middleX = lowX + (hightX-lowX )/2
middleY = lowY + (hightY-lowY )/2
End Function
Function blockFormation(n )
ring = Sqr(n)
idx = n - (ring*ring)
If idx >= ring
formationY = ring
formationX = 2*ring - idx
Else
formationX = ring
formationY = idx
EndIf
End Function
__________________
Мой проект здесь
|