File: Script.c

package info (click to toggle)
openclonk 7.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 145,828 kB
  • ctags: 33,094
  • sloc: cpp: 163,891; ansic: 82,846; xml: 29,876; python: 1,203; php: 767; makefile: 138; sh: 77
file content (194 lines) | stat: -rwxr-xr-x 4,138 bytes parent folder | download | duplicates (2)
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
/*
	Crosshair
	Author: Newton
	
	Virtual cursor for gamepad controls
*/

local crew, angle, dirx, diry, xpos,ypos, analogaim, aiming, menu;

static const CURSOR_Radius = 100;

protected func Initialize()
{
	this["Visibility"] = VIS_None;
	dirx = diry = xpos = ypos = 0;
	aiming = false;
}

public func FxMoveTimer()
{
	var speed = 0;
	var dpad_rotatespeed = 35;

	// dpad mode
	if(diry)
	{
		if (diry < 0) speed = -Sin(angle,100,10);
		else if (diry > 0) speed = +Sin(angle,100,10);
		angle += dpad_rotatespeed*speed/100;
		UpdateAnalogpadPos();
	}
	if(dirx)
	{
		if (dirx < 0) speed = -Cos(angle,100,10);
		else if (dirx > 0) speed = +Cos(angle,100,10);
		angle += dpad_rotatespeed*speed/100;
		UpdateAnalogpadPos();
	}
	// analog pad mode
	if(!dirx && !diry)
	{
		var target_angle = Angle(0,0,xpos,ypos)*10;
		var analog_strength = BoundBy(Sqrt(xpos*xpos+ypos*ypos),0,100);

		var angle_diff = Normalize(target_angle - angle, -1800, 10);
		if (angle_diff == 0) angle_diff = 1;
		
		angle = angle + angle_diff * analog_strength / 100 / 8;
	}
	
	UpdatePosition();
	if(aiming) crew->TriggerHoldingControl();
}

private func UpdateAnalogpadPos()
{
	xpos = Sin(angle/10,100);
	ypos = Cos(angle/10,-100);
}

public func StartAim(object clonk, bool stealth, object GUImenu)
{
	// only reinitialize angle if the crosshair hasn't been there before
	if(!GetEffect("Move",this))
	{
		// which should basically be only the case on the first time aiming
		angle = 800*(clonk->GetDir()*2-1);
	}
	
	// gui or landscape mode:
	if (GUImenu)
	{
		SetCategory(C4D_StaticBack | C4D_IgnoreFoW | C4D_Foreground | C4D_Parallax);
		menu = GUImenu;
		this["Parallaxity"] = [0,0];
	}
	else
	{
		SetCategory(C4D_StaticBack | C4D_IgnoreFoW);
		menu = nil;
	}
	
	// set starting position for analog pad
	UpdateAnalogpadPos();
	
	crew = clonk;
	UpdatePosition();
	RemoveEffect("Move",this);
	AddEffect("Move",this,1,1,this);

	if(!stealth)
	{
		this["Visibility"] = VIS_Owner;
		crew->SetComDir(COMD_Stop);
		aiming = true;
		EnableKeyAimControls(true);
	}
}

private func UpdatePosition()
{
	var x = +Sin(angle,CURSOR_Radius,10);
	var y = -Cos(angle,CURSOR_Radius,10);
	
	if (menu)
		SetPosition(menu->GetX()+x,menu->GetY()+y);
	else
		SetPosition(crew->GetX()+x,crew->GetY()+y);
		
	crew->UpdateVirtualCursorPos();
}

private func MirrorCursor()
{
	angle = -Normalize(angle,-1800,10);
	UpdateAnalogpadPos();
}

public func StopAim()
{
	RemoveEffect("Move",this);
	this["Visibility"] = VIS_None;
	dirx = 0;
	diry = 0;
	EnableKeyAimControls(false);
	analogaim = false;
	aiming = false;
}

private func EnableKeyAimControls(bool enable)
{
	SetPlayerControlEnabled(GetOwner(), CON_AimUp, enable);
	SetPlayerControlEnabled(GetOwner(), CON_AimDown, enable);
	SetPlayerControlEnabled(GetOwner(), CON_AimLeft, enable);
	SetPlayerControlEnabled(GetOwner(), CON_AimRight, enable);
}

public func IsAiming()
{
	return aiming;
}

public func Aim(int ctrl, object clonk, int strength, int repeat, int release)
{
	// start (stealth) aiming
	if(!GetEffect("Move",this))
		StartAim(clonk,true);

	// aiming with analog pad
	if (ctrl == CON_AimAxisUp || ctrl == CON_AimAxisDown || ctrl == CON_AimAxisLeft || ctrl == CON_AimAxisRight)
	{
		dirx = diry = 0;
		
		if(ctrl == CON_AimAxisUp) ypos = -strength;
		if(ctrl == CON_AimAxisDown) ypos = strength;
		if(ctrl == CON_AimAxisLeft) xpos = -strength;
		if(ctrl == CON_AimAxisRight) xpos = strength;
		analogaim = true;
		return true;
	}
	// stop
	else if (release && !analogaim)
	{
		if(ctrl == CON_AimUp || ctrl == CON_AimDown) diry = 0;
		else if(ctrl == CON_AimLeft || ctrl == CON_AimRight) dirx = 0;
		return true;
	}
	else if(!release /*&& !repeat */ && !analogaim)
	{
		if(ctrl == CON_AimUp) diry = -1;
		else if(ctrl == CON_AimDown) diry = 1;
		else if(ctrl == CON_AimLeft) dirx = -1;
		else if(ctrl == CON_AimRight) dirx = 1;
		return true;
	}
	return false;
}

public func Direction(int ctrl)
{
	if(!crew) return;
	
	angle = Normalize(angle,-1800,10);
	//Message("%d, %d",this,angle,ctrl);
	if(ctrl == CON_Left)
		if(angle > 0)
			MirrorCursor();
		
	if(ctrl == CON_Right)
		if(angle < 0)
			MirrorCursor();		
	
	return;
}