# 创建事件
///Setup
/*
You can also set all these variables in the creation code of the
instance in the room editor for more customized water bodies.
*/
color = merge_color(c_blue,c_aqua,.3); //The color of the water.
image_alpha = .4; //The density/see-thoughness of the water.
glimmer = true; //If you want the top waves to glimmer.
nodes = ceil((sprite_width)/4); //More nodes = more realistic, but also slower. smoothness = 3; //How much to smooth out the waves.
disturbance = 1; //How disturbed the water should be.
wave_max = sprite_height; //The maximum wave height and depth.
solids = oParSolid; //Set to -1 if you have no solid objects (preferably a parent). damp = 0; //Damping of the oscillation
freq = 8; //Oscillations per second
time = .2; //Time step
//__pos = 0 /*
Every node needs a y-position (wY) and a vertical speed (vspd). These will be put in an array.
*/
for(var i=nodes;i>0;i--){
ypos[i-1] = 0; vspd[i-1] = 0; }
# 步前
///Update node count
if nodes != array_length_1d(ypos) {
ypos = 0;
vspd = 0;
for(var i=nodes;i>0;i--){
ypos[i-1] = 0;
vspd[i-1] = 0;
}
}
# 步事件
///Update water
var __ndist = (sprite_width/max(1,nodes-1)); //The distance between the nodes. //Toggle off mask to interact with other bodies of water
with obj_water {
mask_index = -1;
}
//Here we add random disturbance to the water, by adding to random nodes' vertical speed.
repeat(disturbance*image_xscale) {
vspd[irandom_range(1,nodes-1)] += choose(.1,-.1);
}
//Smooth out vspd values
vspd = array_smooth_1d(vspd,(smoothness-1)/2)
//Updating the nodes' vertical speed and y position through a loop.
for ( var i=0; i {
//If we are working with the end nodes, or if the water nodes is below other water or
solids, we want them to stand still.
if i == nodes-1 || i == 0 || position_meeting(x+i*__ndist,y-1,obj_water) {
ypos[i] = 0;
} else {
var __r = numeric_springing(ypos[i],vspd[i],0,damp,freq,time);
ypos[i] = __r[0];
vspd[i] = __r[1];
}
ypos[i] = median(min(wave_max,sprite_height),ypos[i],-wave_max); //We limit this to be withing out wave_max range (and no deeper than our sprite is tall).
//We { force down water touching with solids or water above itself
while ypos[i] < 0 && (collision_line(x+i*__ndist,y+ypos[i],x+i*__ndist,y,obj_water,false,true) || collision_line(x+i*__ndist,y+ypos[i],x+i*__ndist,y,solids,false,true)) { ypos[i]++;
} }
//Toggle on mask again with obj_water { mask_index = mask_water;
///Collisions
var __ndist = (sprite_width/max(1,nodes-1)), __id, __array = 0; //The distance between the nodes.
//Collisions with the water surface
for(var i=1;i do {
//Find instance of collision
__id = collision_line(x+(i-1)*__ndist,y+ypos[i-1],x+i*__ndist,y+ypos[i],all,false,true); with __id {
is_water_surface_collision = true; //A special accessor variable
//Add it to the array
if is_array(__array) {
__array[array_length_1d(__array)] = id;
} else {
__array[0] = id;
}
//Move away temporarily
y -= 99999;
}
} until __id = noone;
}
//Collisions with the water body
for(var i=1;i do {
//Find instance of collision
__id = collision_rectangle(x+ (i-1)*__ndist,y+mean(ypos[i-1],ypos[i]),x+i*__ndist,y+sprite_height,all,false,true); with __id {
is_water_surface_collision = false; //A special accessor variable
//Add it to the array
if is_array(__array) {
__array[array_length_1d(__array)] = id;
} else {
__array[0] = id;
}
//Move away temporarily
y -= 99999;
}
} until __id = noone;
}
//Move instances back and perform collision events
if is_array(__array) {
for(var i=0;i with __array[i] {
y += 99999;
event_perform(ev_collision,other.object_index);
}
}
}
}
# 绘制
///Draw water
var __ndist = (sprite_width/max(1,nodes-1)); //The distance between the nodes.
//We draw the water as a primitive, which allows us to make a more complex shape.
draw_set_color(color);
draw_primitive_begin(pr_trianglestrip);
for(var i=0;i draw_set_alpha(image_alpha);
draw_vertex(x+i*__ndist,y+sprite_height); //Here we are making trangles going from the bottom..
draw_vertex(x+i*__ndist,y+ypos[i]); //..to the wave tops!
}
draw_primitive_end();
//If we want a glimmering surface, { we need to draw this as well! (almost in the same way).
if glimmer {
draw_set_color(c_white);
draw_primitive_begin(pr_trianglestrip);
//We draw a 1 pixel thik line, with fading alpha depending on the wave height.
for(var i=0;i draw_set_alpha(0);
draw_vertex(x+i*__ndist,y+ypos[i]+1);
draw_set_alpha(-ypos[i]*10);
draw_vertex(x+i*__ndist,y+ypos[i]);
}
draw_primitive_end();
}
//And always reset the alpha!
draw_set_alpha(1);
# 碰撞玩家
这个是贴在玩家身上 碰撞-水事件(刚刚的代码组成一个 水obj)
///Splash water
if is_water_surface_collision {
var __vspd = (vx)/1.618, //This instance's vertical speed
__pos = mean(bbox_left,bbox_right); //And it's horizontal center
with other {
__pos -= x; //Compared to the water's placement
__pos = clamp(round(__pos/sprite_width*nodes),0,nodes-1); //Converted to placement in array
vspd[__pos] += __vspd;
}
} else {
//Just for visually slowing down in the water
x = mean(x,xprevious);
y = mean(y,yprevious);
}
素材
mask_water
和
spr_water
是一个 32*32的正方形 中点在左上角
oParSolid 是游戏中墙体的obj 自己找个代替品 反正方的就行(32*32)


请问能否贴一张spr_water的图片呢
@熊的力量:一个32*32的蓝色方块 我还给你干啥
这段代码里有好几个自定义函数,请问能不能贴出来
@GreatGuida:你这一讲我也不知道是那部分 https://github.com/DrakSong/DrakSongGameProject/tree/%E6%88%91%E5%B0%B1%E4%B8%80%E6%9D%A1%E7%BA%BF%E6%9B%B4%E6%96%B0%E4%B8%8D%E8%A1%8C%E5%98%9B/DarkSong4/objects/obj_water
反正工程开园了 你剥离吧