Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

27.12.2011, 18:42

Hilfe Movecommands, wie soll ich das nur coden

Hi leute,
ich hab hier ein Flugobjekt der meine Befehle per Mausklicks befolgt.
Die Funktionen heißen z.B.
Entity:moveToPos(FuncPos:vector, Distance,DistanceHöhe)
Entity:attackEnt(FuncTarget:entity)
Da gibst noch einige Befehle.

So würds ausgeführt: Flugobject1:moveToPos(Target:pos() oder Aimpos:pos(), 100,100) usw.
Doch mein Problem ist hier. Ich muss ja quasi die Funktion z.B. moveToPos() solange ausführen bis das Entity sein Ziel erreicht hat.. oder nicht? Ich habe entweder ein Denkfehler gemacht oder das ist einfach so.

Aber ich hätte gern das Verfahren etwas anders und zwar besser. Und zwar in dem ich die Funktion moveToPos() EINMAL aufrufe und das Flugobject bewegt sich dann von selbst solange bis das ziel erreicht wurde.

Hinweise: Kein Multithreading, keine Struktur oder Klassen.

bis jetzt ist meine Methode.... einfach schlecht. Ich führe die Befehle solange aus, bis es ein True Wert kommt.
Und zwar in der Form: moveToPos(Position, Distance - wann es erkennen sollange das zielerreicht wurde, Distance die höhe ok ist)


Ich kann euch mein Code zeigen aber ich weiß es bringt nicht viel, da die Sprache "ingame" Sprache ist. Abgeleitet von Lua. Mir gehts aber nicht darum, mein Code zu verbessern(naja eigentlich schon), sondern wie ihr sowas regelt. Tipps und Tricks

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
@name holoHeliVorlage
@persist [E O HoloMainEnt TargetEnt Projectile]:entity  [HoloMainEntPos AimPos]:vector
@persist H:table 
@persist Switch Flex_VertValue StopMe
@persist Speed UpSpeed MaxSpeed UpMaxSpeed

if(first() | duped()) {
    runOnTick(1)
    runOnLast(1)
    runOnChat(1)
    
    E = entity()
    O = owner()
    
    MaxSpeed = 50
    UpMaxSpeed = 2
    
    HoloMainEnt = holoCreate(1, E:toWorld(vec(0,0,150)))
    holoScaleUnits(1, vec(5,5,5))
    holoModel(1, "models/gunship.mdl")
    holoAnim(1, "prop_turn")
    HoloMainEnt:soundPlay("engine", 0, "/npc/combine_gunship/engine_whine_loop1.wav")
    soundVolume("engine", 1)
    
    function entity:targetEnt(FuncTarget:entity) {
        Idx = holoIndex(This)
        DirectionAng = (FuncTarget:pos() - This:pos()):toAngle()
        SAng = This:angles() 
        SAng += angnorm(DirectionAng - SAng)*0.05
        print(4, ""+SAng:yaw())
        holoAng(Idx, SAng)
    }   
    
    function entity:attackEnt(FuncTarget:entity) {
        if(FuncTarget) {
        #Nachher code mit schiessen
                Distance = This:pos():distance(FuncTarget:pos())
                if(FuncTarget:isPlayer()) {
                    Direction = (FuncTarget:boxCenterW() - This:pos())
                } else {
                    Direction = (FuncTarget:boxCenterW() - This:pos())
                }
                
                DirectionAng = Direction:toAngle()
                Ranger = rangerOffset(Distance-300, This:pos() + vec(0,0,20), Direction)
                if(!Ranger:hit()) {
                    HoloMainEnt:soundPlay("shoot", 0.1, "/weapons/airboat/airboat_gun_energy2.wav") 
                    soundPitch("shoot", 150)
                    Idx = holoIndex(This)
                    holoPos(Idx, This:pos() + -This:right()*3)
                    if(Projectile) {
                        if(clk("shootdelay")) {
                            Rdy = 1
                        }
                    } else {
                        Rdy = 1
                    }
                    if(Rdy) {
                        if(!Projectile) {
                            propSpawnEffect(0)
                            Projectile = propSpawn("models/hunter/plates/plate.mdl", This:pos(), DirectionAng, 0)
                            Projectile:setAlpha(0)
                            Projectile:setMass(10)
                        } else {
                            Projectile:removeTrails()
                            Projectile:setPos(This:pos())
                            Projectile:setAng(DirectionAng)
                            Projectile:setTrails(50,50,0.01,"trails/laser",vec(155,155,0),120)
                        }
                        Projectile:applyForce(Direction*Projectile:mass()*100000)
                    }           
                    timer("shootdelay", 200)
                }
        } else {
            Projectile:propDelete()
        }
        
                
    }
    
    function entity:stopAttack() {
            soundStop("shoot")
    }
    
    function entity:stop() {
        TargetEnt = noentity()
        AimPos = vec()
    }
    
    function entity:rotateGerade() {
        Idx = holoIndex(This)
        SAng = This:angles() 
        SAng += angnorm(This:angles():setPitch(0):setRoll(0) - SAng)*0.05
        holoAng(Idx, SAng)
    }
    
    function number entity:stopAnything() {
        Idx = holoIndex(This)
        Speed -= clamp(Speed, -0.01, 0.01)
        Forward = This:forward()*Speed
        UpSpeed -= clamp(UpSpeed, -0.01, 0.01)
        holoPos(Idx, This:pos() + Forward + vec(0,0,UpSpeed))
        AimPos = vec()
        TargetEnt = noentity()
        if(Speed == 0 & UpSpeed == 0) {
            return 1
        }
        return 0
    }
    
    function number entity:stopMove() {
        Idx = holoIndex(This)
        Speed -= clamp(Speed, -0.1, 0.1)
        Forward = This:forward()*Speed
        UpSpeed -= clamp(UpSpeed, -0.1, 0.1)
        holoPos(Idx, This:pos() + Forward + vec(0,0,UpSpeed))
        if(Speed == 0 & UpSpeed == 0) {
            return 1
        }
        return 0
    }

    function number entity:moveToPos(FuncPos:vector, FuncTopdistance,FuncStopdistance) {
        FuncPos += vec(0,0,FuncTopdistance)
        Distance = This:pos():distance(FuncPos)
        DistanceZ = This:pos():setX(0):setY(0):distance(FuncPos:setX(0):setY(0))
        if(Distance < FuncStopdistance) {
            return 0
        }
        Idx = holoIndex(This)
        Speed = clamp(Speed + 0.1, 0, MaxSpeed)
        TrueSpeed = clamp((Distance+100)/100, 0.01, Speed)
        Forward = This:forward()*clamp((Distance+100)/100, 0.01, Speed)
        if(Speed != TrueSpeed) {
            Speed = TrueSpeed
        }
        Diff = This:pos():z() - FuncPos:z()
        if(Diff > 0) {
            UpSpeed = clamp(UpSpeed - 0.05, -UpMaxSpeed, UpMaxSpeed)
        } elseif(Diff < 0 ) {
            UpSpeed = clamp(UpSpeed + 0.05, -UpMaxSpeed, UpMaxSpeed)
        }
        Up = This:up()*UpSpeed
        holoPos(Idx, This:pos() + Forward + vec(0,0,UpSpeed))
        DirectionVec = (FuncPos) - This:pos()
        DirectionAng = DirectionVec:toAngle()
        SAng = This:angles() 
        SAng += angnorm(DirectionAng - SAng)*clamp(10/Distance,0.01,1)
        holoAng(Idx, SAng)
        return 1
    }
}
if(!Switch) {
    Flex_VertValue += 0.1
    if(Flex_VertValue > 5) {
        Switch = !Switch
    }
} else {
    Flex_VertValue -= 0.1
    if(Flex_VertValue < -5) {
        Switch = !Switch
    }
}
holoSetPose(1, "flex_vert", Flex_VertValue)
AnimationForwardBackVector = HoloMainEnt:forward()*Flex_VertValue/100
AnimationUpDownVector = HoloMainEnt:up()*Flex_VertValue/100
holoPos(1, HoloMainEnt:pos() + AnimationForwardBackVector + -AnimationUpDownVector)

if(changed(HoloMainEnt:pos())) {
    HoloMainEntPos = HoloMainEnt:pos()
    Length = +abs(round($HoloMainEntPos:length(),2))
    soundPitch("engine", 75+Length)
}
#Commands
if(changed(O:keyAttack1()) & O:keyAttack1() & O:weapon():type() == "weapon_crowbar") { HoloMainEnt:stop() 
findIncludeClass("prop*")
findIncludeClass("npc*")
findIncludeClass("player")
findInCone(O:shootPos(), O:eye(), 5000, 10)
TargetEnt = find()
StopMe = 0}
if(changed(O:keyAttack2()) & O:keyAttack2() & O:weapon():type() == "weapon_crowbar") { HoloMainEnt:stop() 
findIncludeClass("prop*")
findIncludeClass("npc*")
findIncludeClass("player")
findInCone(O:shootPos(), O:eye(), 5000, 10)
TargetFollowEnt = find()
if(!TargetEnt) { AimPos = O:aimPos() }
 StopMe = 0}
if(changed(O:keyReload()) & O:keyReload() & O:weapon():type() == "weapon_crowbar") { StopMe = 1 }

if(StopMe == 1) {
    Result = HoloMainEnt:stopAnything()
    if(Result) {
        StopMe = 0
    }
} else {
if(StopMe == 2) {
    Result = HoloMainEnt:stopMove()
    if(Result) {
        StopMe = 0
    }
}
    #Commands execute
    if(TargetEnt) {
        HoloMainEnt:targetEnt(TargetEnt)
        if(!HoloMainEnt:moveToPos(TargetEnt:pos(), 500,1500)) { 
            HoloMainEnt:attackEnt(TargetEnt) 
            StopMe = 2 
        } else { 
            StopMe = 0
        }
    } else {
        if(Projectile) {
            Projectile:propDelete()
        }
    }
    
    if(AimPos) {
        if(!HoloMainEnt:moveToPos(AimPos, 150,100)) { StopMe = 1 }
    }
}
HoloMainEnt:rotateGerade()

E:setText(""+Speed + " " + UpSpeed)

if(chatClk(O)) {

}
Nichts ist unmöglich.

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »MaZy« (27.12.2011, 19:14)


BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

2

27.12.2011, 18:56

Stichwort Statemachine oder Animations-Klassen.

PS:
http://de.wikipedia.org/wiki/Translation_%28Physik%29
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]