~melchizedek6809/WolkenWelten

ref: 19a5ae3cc7ddc83c7cd41598efcaf8af9dfff1f7 WolkenWelten/common/src/game/hook.c -rw-r--r-- 5.2 KiB
19a5ae3cBen (X13/Arch) Increased melee range for chars/animals 7 months ago
                                                                                
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
/*
 * Wolkenwelten - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../game/hook.h"

#include "../game/being.h"
#include "../game/blockType.h"
#include "../game/character.h"
#include "../game/entity.h"
#include "../misc/effects.h"
#include "../misc/mesh.h"
#include "../misc/misc.h"
#include "../misc/sfx.h"
#include "../world/world.h"
#include "../../../common/src/game/rope.h"

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

hook hookList[128];
uint hookCount = 0;


hook *hookNew(character *p){
	hook *ghk = NULL;
	for(uint i=0;i<hookCount;i++){
		if(hookList[i].parent == NULL){
			ghk = &hookList[i];
			break;
		}
	}
	if(ghk == NULL){
		ghk = &hookList[hookCount++];
	}
	u32 flags = ROPE_DIRTY;
	if(characterGetMaxHookLen(p) > 128.f){flags |= ROPE_TEX_CHAIN;}

	ghk->parent       = p;
	ghk->ent          = entityNew(vecAdd(vecNew(0,1,0),p->pos),vecNew(-p->rot.yaw,-p->rot.pitch-90.f,p->rot.roll),3000.f);
	ghk->ent->vel     = vecAdd(vecMulS(vecDegToVec(p->rot),1.3f),p->vel);
	ghk->ent->eMesh   = meshHook;
	ghk->ent->flags   = ENTITY_NOREPULSION;
	ghk->rope         = ropeNew(hookGetBeing(ghk),characterGetBeing(p),flags);
	ghk->rope->length = -1.f;
	ghk->attached     = 0;

	ghk->hooked       = false;
	ghk->returning    = false;
	ghk->goalLength   = 0;

	return ghk;
}

void hookFree(hook *ghk){
	if(ghk == NULL){return;}
	entityFree(ghk->ent);
	ghk->ent    = NULL;
	ghk->parent = NULL;
	ropeFree(ghk->rope);
}

float hookGetLength(const hook *ghk){
	return vecMag(vecSub(ghk->ent->pos,ghk->parent->pos));
}

float hookGetRopeLength(const hook *ghk){
	return ropeGetLength(ghk->rope);
}

bool hookReturnToParent(hook *ghk,float speed){
	vec dist = vecSub(ghk->ent->pos,ghk->parent->pos);
	float d  = vecMag(dist);
	speed = d / 8.f;
	if(speed < 1.f){speed = 1.f;}
	if(d <= 2){return true;}
	ghk->ent->vel = vecMulS(vecNorm(dist),-speed);

	if(ghk->ent->flags & ENTITY_COLLIDE){
		ghk->ent->pos = vecAdd(ghk->ent->pos,ghk->ent->vel);
	}
	return false;
}

void hookReturnHook(hook *ghk){
	ghk->returning    = true;
	ghk->hooked       = false;
	ghk->ent->vel     = vecZero();
	ghk->rope->a      = hookGetBeing(ghk);
	ghk->rope->length = -1.f;
	ghk->rope->flags |= ROPE_DIRTY;
	hookReturnToParent(ghk,0.1f);
}

bool hookUpdate(hook *ghk){
	entityUpdate(ghk->ent);

	if(ghk->returning && hookReturnToParent(ghk,0.01f)){
		return true;
	}else if(ghk->hooked && !ghk->returning){
		ghk->ent->vel = vecZero();
		if(!(ghk->ent->flags & ENTITY_COLLIDE) && (ghk->attached == 0)){
			hookReturnHook(ghk);
		}
		if((ghk->attached != 0) && (ghk->rope->a == 0)){
			hookReturnHook(ghk);
		}
	}else{
		if(!ghk->hooked && !ghk->returning && (ghk->ent->flags & ENTITY_COLLIDE)){
			ghk->ent->vel     = vecZero();
			ghk->ent->flags  |= ENTITY_NOCLIP;
			ghk->hooked       = true;
			ghk->attached     = 0;
			ghk->goalLength   = hookGetLength(ghk);
			ghk->rope->length = ghk->goalLength;
			ghk->rope->flags |= ROPE_DIRTY;
			sfxPlay(sfxHookHit,1.f);
			sfxLoop(sfxHookRope,0.f);
			unsigned char b  = worldGetB(ghk->ent->pos.x,ghk->ent->pos.y,ghk->ent->pos.z);
			if(b){ fxBlockBreak(vecFloor(ghk->ent->pos),b,0);}
		}else{
			being closest = beingClosest(ghk->ent->pos,1.f);
			if((closest != 0) && (closest != characterGetBeing(ghk->parent)) && (closest != ghk->attached)){
				ghk->ent->vel     = vecZero();
				ghk->ent->flags  |= ENTITY_NOCLIP;
				ghk->hooked       = true;
				ghk->goalLength   = hookGetLength(ghk);
				ghk->rope->length = ghk->goalLength;
				ghk->attached     = closest;
				ghk->rope->a      = closest;
				ghk->ent->pos     = beingGetPos(closest);
				ghk->rope->flags |= ROPE_DIRTY;
				sfxPlay(sfxHookHit,1.f);
				sfxPlay(sfxUngh,1.f);
				fxBlockBreak(ghk->ent->pos,2,0);
				sfxLoop(sfxHookRope,0.f);
			}else if(!ghk->hooked && !ghk->returning){
				sfxLoop(sfxHookRope,1.f);
				if(hookGetLength(ghk) > characterGetMaxHookLen(ghk->parent)){
					hookReturnHook(ghk);
				}
			}
		}
	}
	if(ghk->ent->pos.y < -256.f){
		return true;
	}
	return false;
}

bool hookGetHooked(const hook *ghk){
	if(ghk == NULL){return false;}
	return ghk->hooked;
}
float hookGetGoalLength(const hook *ghk){
	if(ghk == NULL){return false;}
	return ghk->goalLength;
}
void hookSetGoalLength(hook *ghk, float len){
	ghk->goalLength = len;
	if(ghk->hooked && !ghk->returning){
		ghk->rope->length = ghk->goalLength;
		ghk->rope->flags |= ROPE_DIRTY;
	}
}

hook *hookGetByBeing(being b){
	const uint i = beingID(b);
	if(beingType(b) != BEING_HOOK){ return NULL; }
	if(i >= hookCount)            { return NULL; }
	return &hookList[i];
}

uint hookGetBeing(const hook *c){
	if(c == NULL){return 0;}
	return beingHook(c - &hookList[0]);
}