File: Script.c

package info (click to toggle)
openclonk 8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 169,520 kB
  • sloc: cpp: 180,479; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 145; sh: 101; javascript: 34
file content (177 lines) | stat: -rw-r--r-- 3,920 bytes parent folder | download | duplicates (5)
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
/*
	Crosshair
	Author: Newton
	
	Virtual cursor for gamepad controls
*/

local crew, angle, xpos, ypos, aiming, menu;

static const CURSOR_Radius = 100;
// This is supposed to be a constant, but C4Script doesn't allow constant expressions there.
private func CURSOR_Deadzone() { return PLRCON_MaxStrength / 5; }

protected func Initialize()
{
	SetVisibility(false);
	xpos = ypos = 0;
	aiming = false;
}

public func FxMoveTimer()
{
	if (!crew)
	{
		RemoveObject();
		return FX_Execute_Kill;
	}

	var target_angle = Angle(0,0,xpos,ypos)*10;

	if (!Visible() && !InDeadzone())
	{
		// The player moved the aiming stick while the crosshair wasn't visible: Use angle directly.
		angle = target_angle;
		SetVisibility(true);
	}
	else if (!InDeadzone())
	{
		// Smooth small movements of the stick while the crosshair is visible.
		var angle_diff = Normalize(target_angle - angle, -1800, 10);
		if (Abs(angle_diff) < 450)
			angle = angle + angle_diff / 8;
		else
			angle = target_angle;
	}
	else if (!aiming)
	{
		// The player doesn't touch the stick and no item is using the crosshair right now.
		SetVisibility(false);
		// Aim somewhere useful. Note that this can be overwritten by objects and isn't used for throwing.
		angle = 800*(crew->GetDir()*2-1);
	}
	
	UpdatePosition();
	crew->TriggerHoldingControl();
}

private func AnalogStrength() { return BoundBy(Sqrt(xpos*xpos+ypos*ypos), 0, PLRCON_MaxStrength); }
private func InDeadzone() { return AnalogStrength() < CURSOR_Deadzone(); }
private func Visible() { return this.Visibility != VIS_None; }

// Updates the visibility, returing true if it was changed.
private func SetVisibility(bool visible)
{
	var newvis, oldvis;
	if (visible)
		newvis = VIS_Owner;
	else
		newvis = VIS_None;
	oldvis = this.Visibility;
	this.Visibility = newvis;
	return newvis != oldvis;
}

private func CreateMoveEffect(object clonk)
{
	crew = clonk;
	UpdatePosition();
	RemoveEffect("Move",this);
	AddEffect("Move",this,1,1,this);
}

public func StartAim(object clonk, int default_angle, object GUImenu)
{
	aiming = true;

	// 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;
	}

	// Use the given angle if the player wasn't aiming before.
	if (SetVisibility(true) && default_angle)
		angle = default_angle;

	CreateMoveEffect(clonk);
}

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()
{
	return;
	angle = -Normalize(angle,-1800,10);
}

public func StopAim()
{
	aiming = false;
}

// Aiming means that some object is currently actively using the crosshair.
public func IsAiming()
{
	return aiming;
}

// The crosshair is also active when the player is holding the aiming stick.
public func IsActive()
{
	return aiming || Visible();
}

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

	// aiming with analog pad
	if (status == CONS_Moved &&
		(ctrl == CON_AimAxisUp || ctrl == CON_AimAxisDown || ctrl == CON_AimAxisLeft || ctrl == CON_AimAxisRight))
	{
		if(ctrl == CON_AimAxisUp) ypos = -strength;
		if(ctrl == CON_AimAxisDown) ypos = strength;
		if(ctrl == CON_AimAxisLeft) xpos = -strength;
		if(ctrl == CON_AimAxisRight) xpos = strength;
		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;
}